The "fork function" of Linux Learning

Source: Internet
Author: User
Tags glob stdin

Nreturn Value:The fork function is called once, but returns two times: 0 is returned in the child process, the child process ID is returned in the parent process, and the error returns-1. By returning a value, you can determine whether it is in a parent process or a child process. Nthe child process and the parent process continue to execute the instruction after the fork call. A child process is a copy of the parent process:1. The child process obtains a copy of the parent process data space, heap, and stack, and the parent-child process does not share the storage space. 2. Parent-Child process shared body segment (read-only);3. To improve efficiency, the parent process space is not copied immediately after fork, Cow (copy-on-write) is used, andwhen one of the parent-child processes is modified, the data segment, heap, and stack are copied, but only the modified region is copied ; See a program:
 #include <iostream> 
#include <unistd.h>
#include <stdio.h>

using namespace std;

int glob = 6;
Char buf[] = "A write to stdout\n";

int main (void)
{
int var;
pid_t pid;

var = n;

if (write (Stdout_fileno, buf, sizeof (BUF)-1)! = sizeof (BUF)-1)
{
cout &L t;< "Write error" << Endl;
return 0;
}

printf ("before fork\n");

if (pid = fork ()) < 0)
{
cout << "fork Error" << Endl;
return 0;
}
Else if (pid = = 0)
{
glob++;
var++;
}
Else
{
Sleep (2);
}

printf ("pid =%d, Glob =%d, var =%d\n", Getpid (), Glob, Var);

return 0;
}
Direct output to the console:
A write to stdout
Before fork
PID = 1867, Glob = 7, var = 89
PID = 1866, Glob = 6, var = 88
Using the redirect "./a.out>a.txt", the a.txt content is as follows: a write to stdout
Before fork
PID = 1939, Glob = 7, var = 89
Before fork
PID = 1938, Glob = 6, var =
Why is there such a difference? First Lookat the difference between "Stdout_fileno" and "FILE *stdout": Stdin/stdout/stderr is a file* type, which is used by the standard C + + class of file operation function libraries, defined in header file <stdio.h>.Stdin_fileno/stdout_fileno/stderr_fileno is the int type, which is essentially a file descriptor (value of 0,1,2), defined in header file <unistd.h>. File descriptor (FD) is Stdin_fileno (0)/Stdout_fileno (1)/Stderr_fileno (2), respectively. Stdin/stdout/stderr. The difference is that the standard I/O is buffered (see below for details), and the Stdin_fileno/stdout_fileno/stderr_fileno is not buffered. We just need to remember: The main functions of using Stdin/stdout/stderr are: Fread, fwrite, fclose, and so on, basically starting with F. functions that use Stdin_fileno/stdout_fileno/stderr_fileno are: Read, write, close, and so on. For a more detailed description of both: 1. " The difference between FILE *stdout and Stdout_fileno "(http://hi.baidu.com/_%C2%B7_%C8%CB_%BC%D7_/blog/item/2d84a816882fdbd4c2fd78e1.html) 2." Learning of Stdin,stdout and Stdout_fileno,stdin_fileno "(http://www.cnblogs.com/hoys/archive/2011/05/11/2043044.html) Here's a description of "printf"/"Write" and buffering: printf is a function declared in stdio.h, and standard IO is buffered, so printf is buffered. Write is not buffered. Standard IO is buffered when it is input or output to an end device, otherwise they are fully buffered. The standard error stream stderr does not use buffering. A more accurate description is:They are fully buffered when and only if the standard input and standard output do not involve interactive devices. Standard error streams do not use buffering. The following conditions cause a flush of the buffer (emptying the buffer): 1, when the buffer is full;
2, execute FLUSH statement;
3, execute Endl statement (printf is "\ n");
4. Close File。 In summary, the contents of write are output directly to the device in the parent process, "before fork "after the main thread is output to the terminal, the buffer is emptied because of a newline character, so it is only output once. When redirected to "A.txt", printf uses a full buffer, so "before fork "is not output to the device, but with the fork () is copied to a sub-process space, so output two times. Note: The child process is also redirected when the parent process output is redirected.

The "fork function" of Linux Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.