I'm trying to work with foreground/background processes, but seem to keep getting -1 from waitpid and I'm not sure why
Code:
void handleSignal(int signal) {
printf("terminated by signal %d", signal);
fflush(stdout);
exit(signal);
}
struct sigaction sigAct;
sigAct.sa_handler = handleSignal;
sigAct.sa_flags = 0;
sigfillset(&(sigAct.sa_mask));
pid_t spawnpid = -5;
int childStatus;
spawnpid = fork();
while(1) {
if (spawnpid == -1) {
// perror("ERROR: fork()\n");
printf("failed to start child process\n");
status = 1;
fflush(stdout);
continue;
} else if (spawnpid == 0) {
/*
* inside the child process
*/
printf("inside child process\n");
sigaction(SIGINT, &sigAct, NULL);
// execute the command
execvp(command->cmdArgs[0], command->cmdArgs);
// if reach here then execvp failed
printf("command not found");
fflush(stdout);
status = 1;
} else if (spawnpid > 1) {
/*
* inside the parent process
*/
printf("inside parent process\n");
sigaction(SIGINT, &sigAct, NULL);
if (command->background) {
printf("background pid is %d", spawnpid);
fflush(stdout);
} else {
// wait for child process to finish
pid_t childpid = waitpid(spawnpid, &childStatus, 0);
if (childpid == -1) {
perror("ERROR: waitpid()\n");
status = 1;
continue;
}
...
fflush(stdout);
}
}
}
}
The output I keep getting is something like this:
Code:
: ls
inside parent process
inside child process
csXXX_proj3
ERROR: waitpid()
: Interrupted system call
: