I'm trying to fork two children and exec() them both using a nested switch. However, my code doesn't seem to enter the case for my second child.
// fork the first child
switch (player0PID = fork()){
case -1: // error
return -1;
case 0: // in child
// exec ("turn into") a new program
// first child successfully execs
execl("./client", "0", "&", (char *) NULL);
_exit(127); // failed exec
default: // inside parent
// fork second child
switch (player1PID = fork()){
case -1: // error
printf("err spawning of client 1");
return -1;
case 0: // in child
// exec ("turn into") a new program
// below exec does not execute
// second child not exec'd
execl("./client", "1", "&", (char *) NULL);
_exit(127); // failed exec
default: // inside parent
;
// a lot of code the parent executes
break;
break;
Am I doing something wrong with my switch statement? The first child is exec'd fine, and the parent code will execute. However, my second child isn't being forked or exec'd for some reason. The code won't enter into "case 0" for the second switch.
update:
The solution was to correct my use of execl
.
My second exec failed because the two exec
statements were giving &
as an argument to the programs. In order for &
to be processed correctly, I need to run the programs using the sh
program. The second exec
may not have worked because one is not allowed to run two processes concurrently in the foreground?
Example execl usage:
execl("/bin/sh", "sh", "-c", "./client 1 &", (char *) NULL);