Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to know how fork works?

Tags:

c

fork

process

I am trying the following C code:

int main()
{
    printf("text1\n");
    fork();
    printf("text2\n");
    return 0;
}

I was expecting to get the output where i get two "text1" and two "text2", like:

text1
text1
text2
text2

But, i am, instead, getting:

text1
text2
text2

only one "text1"??? Ok, if child process executes from the fork(), then why do i get two "text1" for following:

int main()  
{  
    printf("text1");  
    fork();  
    printf("text2\n");  
    return 0;  
}  

the output now is:

text1text2  
text1text2 

If the child process starts after the fork, output should be:

text1  
text2  
text2  
like image 278
mandavi Avatar asked May 15 '11 22:05

mandavi


People also ask

How does a fork work?

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 are the uses of fork function?

Fork() is used to create new processes as every body has written. Show activity on this post. fork() system call creates the exact duplicate of parent process, It makes the duplicate of parent stack, heap, initialized data, uninitialized data and share the code in read-only mode with parent process.

What does PID fork () do?

The fork() creates a copy of the process that was executing. The fork() is called once but returns twice (once in the parent and once in the child). The line PID = fork(); returns the value of the fork() system call. The if (PID == 0) evaluates the return value.

What is fork programming?

Forking is to take the source code from an open source software program and develop an entirely new program. Forking is often the result of a deadlock in an open source project that is so insurmountable that all work stops.


4 Answers

fork() creates a new process by copying everything in the current process into the new process. That typically includes everything in memory and the current values of the CPU registers with some minor adjustments. So in effect, the new process gets a copy of the process's instruction pointer as well so it resumes at the same point where the original process would continue (the instruction following the fork()).


To address your update, printf() is buffered. Normally the buffer is flushed when it encounters a newline character at the end, '\n'. However since you have omitted this, the contents of the buffer stays and is not flushed. In the end, both processes (the original and the child) will have the output buffer with "text1" in it. When it eventually gets flushed, you'll see this in both processes.

In practice, you should always flush files and all buffers (that includes stdout) before forking to ensure that this does not happen.

printf("text1");
fflush(stdout);
fork();

The output should look like this (in some order):

text1text2
text2
like image 194
Jeff Mercado Avatar answered Oct 09 '22 08:10

Jeff Mercado


The forked process gets a copy of the variable memory, and at the time of the fork the output buffer has yet to be flushed. No output has been written to the console when you fork, only buffered up. Both processes thus continues with text1 already in the buffer, and thus both print it.

like image 29
Lasse V. Karlsen Avatar answered Oct 09 '22 08:10

Lasse V. Karlsen


fork clones the current process. The new process will "start" at the fork call, not at the start of main as you seem to expect. Thus when you print the first time there is 1 process, then when you fork there are two.

Since you are forking after printing "text1", it is only printed once.

In the second example the duplicated output is due to output buffering - printf doesn't actually output anything to the screen until it is flushed or it hits a newline ('\n').

Consequently the first call to printf actually just wrote data to a buffer somewhere, the data was then copied into the second process' address space, and then the second call to printf would have flushed the buffer, complete with "text1" in both buffers.

like image 6
tobyodavies Avatar answered Oct 09 '22 09:10

tobyodavies


It is because forked process starts after fork, not from very beginning. exec starts process from entry point and will print what you expect.

like image 2
Andrey Avatar answered Oct 09 '22 09:10

Andrey