Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The behavior of the fork() system call on Linux in this code [duplicate]

Tags:

c

linux

fork

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; } 
like image 598
Akash Mahajan Avatar asked Feb 21 '15 20:02

Akash Mahajan


People also ask

What is fork () system call?

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 does fork do in Linux?

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.

What is the return value of fork () in Linux if executed by 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.

How do I run a fork in Linux?

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.


2 Answers

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.

like image 171
ar31 Avatar answered Sep 30 '22 13:09

ar31


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.

like image 24
Thomas Dickey Avatar answered Sep 30 '22 12:09

Thomas Dickey