Need to know how the plug works?

I am trying to use the following C code:

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

I expected to get output where I get two "text1" and two "text2", for example:

 text1 text1 text2 text2 

But instead, I get:

 text1 text2 text2 

only one "text1" ??? Well, if the child process is executed from fork (), then why do I get two "text1" for the following:

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

Now the conclusion:

 text1text2 text1text2 

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

 text1 text2 text2 
+6
source share
7 answers

fork() creates a new process, copying everything in the current process to the new process. Usually this includes everything in memory and the current values ​​of the processor registers with some minor adjustments. Thus, as a result, the new process receives a copy of the process instruction pointer, so it resumes at the same point where the original process will run (the instruction following fork() ).


To refer to your update, printf() buffered. Typically, a buffer is flushed when it encounters a newline at the end, '\n' . However, since you omitted this, the contents of the buffer remain and are not cleared. In the end, both processes (source and child) will have an output buffer with "text1" in it. When it finally turns red, you will see this in both processes.

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

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

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

  text1text2
 text2
+23
source

The processed process receives a copy of the memory variable, and during the fork, the output buffer is not yet cleared. The console does not output when you use fork, only buffered. Both processes, therefore, continue with the text already in the buffer, and therefore both print it.

+7
source

fork clones the current process. The new process will start when the fork called, and not at the beginning of main , as you seem to expect. So when you print for the first time, there is 1 process, then when you use fork, there are two.

Since you fork after printing "text1" , it only prints once.

In the second example, duplicated output is due to output buffering - printf does not actually display anything until it turns red or gets to a new line ( '\n' ).

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

+6
source

This is because the fork ed process starts after fork , and not from the beginning. exec starts the process from the entry point and prints what you expect.

+2
source

The child process will start from the position of fork (), so you get the correct output.

+1
source

  Problem 1: the output as
       text1
       text2
       text2 

This is due to the fact that fork () creates an exact copy (child) of the parent process, and both processes start their execution immediately after the fork () system call.

  Problem 2: the output as
       text1text2 
       text1text2 

It's all about buffering. Refer to this link and learn about fork () principles. http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
+1
source

from man 2 fork : fork returns 0 to the child process.

 value = fork(); if( value == -1 ) { printf( "fork failed\n" ); exit(1); } if( value ) { printf( "test1\n" ); } else { printf( "test2\n" }; } 
0
source

Source: https://habr.com/ru/post/888164/


All Articles