Below program neither load the program to child process nor prints "before" and "after". However ps aux shows the creation of processes (but without loading args0 program). I am using PIPE defined as socketpair. args0[] holds executable name for the child program, args1[] holds the name of the child program. args2 and args3 are predefined values that doesn't change and should be sent to the children as arguments. you can assume char args2[] = "10" --> user input (digit) and converted to string. I just do not understand why at least printf("before") is not printed. i read about fflush and putting \n at every printf, and i did. so everything in my program is printed correctly up to this point.
I would truly appreciate your responses.
char args2[];
char args3[];
//creating pipe
int forkk(pipes *myPipe, server_message *m, char args0[],char args1[]) {
pid_t cpid;
//pipe passed myPipe[i]
if (PIPE(myPipe->fd) == -1) {
perror("pipe error\n");
return -1;
}
fork();
cpid=getpid();
if (cpid == -1) {
perror("fork error\n");
return -1;
}
if (cpid) {
close(myPipe->fd[1]);
return 1;//closing one end of parent
} else {
for (int i = 3; i <= myPipe->fd[0]; i++) {
close(i);
}
dup2(myPipe->fd[1], 0); //redirecting stdin of child
dup2(myPipe->fd[1], 1); //redirecting stdout of child
close(myPipe->fd[1]);
myPipe->cpid = cpid;
char *newargs[3];
newargs[0]=args1;
newargs[1]=args2;
newargs[2]=args3;
printf("before\n");
//fflush(stdout);
execv(args0,newargs);
printf("after execv\n");
write(myPipe->fd[0], &m, sizeof(server_message)); //send the server_msg immediately (pass an array or msg)
}
return 2;
}
void main(){
....
scanf("%d %d", &width, &height);
sprintf(args2,"%d",height); //converting into to string
sprintf(args3,"%d",width);
char *args0 = "./prey";
char *args1 = "prey";
int r= forkk(&myPipes[2], &msg, args0,args1);
}
I cannot post the entire code as it is long and need explanation. I am most probably having a problem with pointer assignment that i wrongly think is the correct way. Thanks a lot for any kind of help