Hello. I'm trying some sample code dealing with process switching, and the code I have keeps stopping the parent when the child exits.
Code snippet:
Code:
int main(int argc, char **argv) {
int ch, rc, exit_loop = 0, round = 0, status;
pid_t pid, pid_who = -1;
initscr(); /* Ncurses */
keypad(stdscr, TRUE);
noecho();
raw();
main_win = add_win(main_win, 20, 80, 0, 0);
update_current(main_win->thr_id);
set_focus();
/* Loop and poll child processes */
while(1) {
if(round == 0) {
round++;
pid = fork();
if(pid == 0) {
main_menu(main_win);
_exit(EXIT_FAILURE);
} else if(pid < 0) {
printf("Fork failure");
exit(EXIT_FAILURE);
} else {
main_win->pid = pid;
setpgid(pid, pid);
tcsetpgrp(STDIN_FILENO, pid);
}
}
pid_who = waitpid(WAIT_ANY, &status, WNOHANG);
if(pid_who == pid) {
setpgid(getpid(), getpid());
tcsetpgrp(STDIN_FILENO, getpid());
break;
}
}
endwin();
exit(0);
}
Please, can anyone see why my parent's getting stopped when I exit the child process? Thank you.