I have a C program that creates a child process that I run from linux shell.
My problem is that after forking, the parent process moves to the background of the shell. I would like the parent process to stay in the foreground.
Here is a simple example:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void) { int i; for (i = 0; i < 3; i++) { printf("Before Fork\n"); sleep(1); } printf("forking...\n"); pid_t pid = fork(); if (pid) { // child printf("Child started and now exiting\n"); exit(EXIT_SUCCESS); } // parent wait(NULL); for (i = 0; i < 3; i++) { printf("After Fork\n"); sleep(1); } return 0; }
Conclusion (comment added later)
Before Fork Before Fork Before Fork forking... Child started and now exiting After Fork gregp@gregp-ubuntu :~/fork_example$ After Fork
Note that soon after fork, the user returns to the shell prompt, and the program continues to run in the background. I do not want this to happen. I want the program to continue to work in the foreground.
Desired result (comment added later)
Before Fork Before Fork Before Fork forking... Child started and now exiting After Fork After Fork After Fork
source share