Shared Memory zone fork ()

Source: Internet
Author: User

The most difficult thing to understand about the fork function is that the fork program executes once, but returns two times. The returned value is the id of the parent process and the child process, and the parent process returns the id of the parent process, the process id returned by the child process is 0. The child process only returns a process id of 0 in the parent process, but not the process id of the child process is zero. This must be clear. The child process shares the code segment of the parent process. The data segment and stack segment are copied to the new child process. Modifying the stack and data of the child process does not affect the code of the parent process. If you need to understand it in depth, the child process also shares the stack segment and data segment with the parent process. It only saves the modified values and undoubtedly optimizes the operating system performance. The following program can be compiled under linux to give you a preliminary understanding of fork.
// Qbforkeg. c
# Include
# Include
Int main (void)
{
Int N = 400;
Int count = 0;
Setbuf (stdout, NULL );
If (fork () = 0)
{
While (N> 0)
Printf ("child The value of N =: % d, count = % d \ n", N --, count ++ );
Return 0;

}
While (N> 0)
Printf ("parent The value of N =: % d, count = % d \ n", N --, count ++ );
Return 0;
} // End
: Cc qbforkeg. c-o qbforkeg
:./Qbforkeg
The above setbuf sets my program to a non-buffer mode to avoid improper output crossover. From the code output, we can see that the N of the Parent and Child processes is not the same, but different values. It indicates that the parent and child processes do not share data segments.
The following procedure uses the shared memory:
// Source code shm. c
# Include
# Include
# Include
# Include
# Include
# Include
Int main (void)
{

Int fd, zero = 0;
Sem_t * mutex;
Int * ptr;
Fd = open ("/dev/zero", O_RDWR | O_CREAT, 0666 );
Ptr = mmap (NULL, sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
Close (fd );
Mutex = sem_open ("shm", O_CREAT | O_EXCL, 0666, 1 );
Sem_unlink ("shm ");
Setbuf (stdout, NULL );
If (fork () = 0)
{
While (* ptr)
}
System ("rm/tmp/test ");
Return 0;
} // END
: Cc shm. c-o shm
:./Shm
The result is displayed.
The above process is explained as follows:
1: Open the device file/dev/zero provided by SVR4, and then investigate the description in mmap. When the device reads, all the bytes returned are 0, any bytes written to the device are discarded.
2: Call mmap to map the opened file to the memory space of the current process. The first parameter is NULL. A system selects its starting address in the process. The length parameter is the size of billions of integers. The read/write access is set for the protection mode parameter. By setting the fourth parameter as MAP_SHARED, the parent process can view any changes made to the child process, the Return Value of the function is the starting address of the memory to be shared. we store it in ptr.
3: Create and initialize a traffic signal. It protects an object that shares a variable and then deletes the name of the traffic signal. However, although the path is deleted, it does not affect the opened signal. In this way, if the program crashes, the path name will also be deleted from the system.

The results of the above program show that * ptr is shared by two processes in the case of shared memory.



Hust_qb http://bbs.chinaunix.net/thread-2020589-1-1.html

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.