I have read in books and online resources that the fork() system call creates a copy of current process and both the processes start executing from the point after the fork() system call is made. Is it correct?
If it is correct then why does the code below print "Test Test"? It should print "Test" just one time (by the parent process).
#include <sys/types.h> /* pid_t */ #include <sys/wait.h> /* waitpid */ #include <stdio.h> /* printf, perror */ #include <stdlib.h> /* exit */ #include <unistd.h> /* _exit, fork */ int main(void) { int ctr =1; int pc = 1; printf("%s", "Test "); pid_t pidmain = fork(); return EXIT_SUCCESS; }
Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.
What is a Fork()? In the computing field, fork() is the primary method of process creation on Unix-like operating systems. This function creates a new copy called the child out of the original process, that is called the parent. When the parent process closes or crashes for some reason, it also kills the child process.
RETURN VALUE Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.
The syntax of fork() system call in Linux, Ubuntu is as follows: pid_t fork(void); In the syntax the return type is pid_t. When the child process is successfully created, the PID of the child process is returned in the parent process and 0 will be returned to the child process itself.
When you call fork()
the operating system creates a copy of the current processes entire memory (it doesn't actually copy the memory since it can use the MMU to do this efficiently).
Since stdout is buffered by default it will only print the message once a newline characters is written or the stream is flushed. When you fork a new process the current write buffer (containing "Test ") will also be duplicated in the new process. This will then be printed once the process exits since that implicitly closes (and flushes) stdout. If you replace the printf("%s", "Test ");
with printf("%s\n", "Test ");
or add a fflush(stdout);
call before the fork()
you will see the expected output.
At the point where you call fork, both processes have the string "Test " in their buffers for the standard output. When each process exits, they both flush this text to the output (see exit for explanation). You could (as suggested) add a newline to the buffer (and it would happen to come out because of the buffering -- see setbuf for explanation). Or you could call fflush(stdout) before the fork and get exactly what you asked for - the "Test " string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With