What happened after the Linux fork operation?

Source: Internet
Author: User

Today, I was reading "UNIX network Programming" when I encountered a problem: accept the return of the CONNFD, is the parent-child process shared between? I did not understand that the open file descriptor should not be independent of each other in the parent-child process? Why is it shared? What is shared between the parent and child processes after fork? Are the variables on the heap shared?

The following code tests were done to create a file before fork, write the string "Shenlei" in the child process, and the parent process reads the file contents and discovers "Shenlei". Description The open file descriptor is shared between the parent and child processes.

It seems that in UNIX network programming is right, when close a file descriptor, the file descriptor reference count-1. In normal file IO operations, only if the reference count is 0 to actually close the file descriptor, when the socket operation, only when the reference count is 0 o'clock will send fin, four times to wave off the corresponding socket.

#include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <fcntl.h> #include <iostream>using namespace std;static int global_val = 0;int Main () {int *p = (int*) malloc (sizeof (int)); *p=0;int m = 2;pid_t pid; int fd = open ("MyTest", O_RDWR | O_creat, 0666), if (PID = fork ()) < 0) {cout << "fork Error" << Endl;} else {if (PID = = 0) {char buf[20]= "n"; int res = read (fd,buf,20);cout<< "pid is" <<getpid () << "Res is" &lt ;<res<< "FD is" <<fd<< "BUF is" << buf<<endl;close (FD)//sleep (1); Char bufs[8]= " Shenlei "; Lseek (FD, 0, Seek_set); write (Fd,bufs,strlen (BUFS)); global_val++;m++;(*p) + +;} Else{sleep (1); char buf[20]= "n"; Lseek (FD, 0, seek_set); int res = read (fd,buf,20);cout<< "pid is" <<getpid () << ' res is ' <<res<< ' fd is ' <<fd<< ' buf is ' << buf<<endl;cout << *p &LT;&L T "" << m << "<< global_val<< Endl;}} return 0;}

It then tests whether a heap object in a process can be shared, as shown in the preceding code, and the conclusion is not possible. Global variables, static variables, and global static variables are also not possible. Description The stack information is completely copied to the child process memory space after the fork is created, and the parent-child process is independent of each other.

A company's written question: When the parent process calls fork () to create a child process, which of the following variables are modified in the child process, the parent process will also make changes accordingly?

A. Global variables
B. Local Variables
C. Static variables
D. File pointers

Read the above analysis is very simple, the answer is D.

What happened after the Linux fork operation?

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.