Some questions and conjecture about "thread and fork"

Source: Internet
Author: User

Today, when I was reading "Advanced Programming in Unix environments", I got stuck in this chapter for a long time. There are many things I don't understand here, so I want to write a blog to sort it out.

As mentioned in the "thread and fork" section, "There is only one thread in the child process, it is composed of copies of threads that call fork in the parent process. If the thread in the parent process occupies the lock, the child process also occupies the lock. The problem is that the sub-process does not contain copies of the thread that occupies the lock. Therefore, the sub-process cannot know which locks it occupies and which locks need to be released .". The book mentions that you can call the pthread_atfork function to create a fork handler to clear the lock status.

I don't quite understand the mechanism introduced by this function here. First, let's quote a passage in the book: "note that the lock will not be unlocked twice at a time, although it may appear. When the child process address space is created, it obtains copies of all locks. Because prepare fork processes and obtains all the locks, the memory in the parent process is the same as the memory content in the child process at the beginning, when the Parent and Child processes unlock copies of their locks, the new memory is allocated to the child process, the memory content of the parent process is copied to the memory of the child process (write-time replication), so it will fall into the illusion that the parent process locks all its lock copies, the child process locks all the copies of the lock. The parent process and child process unlock the duplicate locks at different memory locations, as if the following event sequence occurs:

1. The parent process acquires all locks.

2. The sub-process obtains all the locks.

3. The parent process releases its lock.

4. The sub-process releases its lock.

 

Split line --------------------------------------------------------------------------------------------------------------------------------------------------

It is not quite understandable or clear here. Then, in combination with the example program in the book, I made a conjecture about the mechanism derived from the paragraph in the book. In my own words, I sum up the following:

 

When the pthread_atfork function is called to create three functions to help clear the lock, when a thread of the parent process calls fork, first, the prepare fork processing program is run to let the parent process occupy all the locks. After the prepare fork processing program is run, all the locks of the parent process are locked. After that, run the child fork processing program. when running the child fork, the child process has been created, but the fork does not return. At this time, the address space of the child process is inherited from all the locks of the parent process, because the existence of the parent process and the child process is the same at the beginning. Then, when running child
When the fork handler unlocks the lock inherited by the child process, it will trigger the write-time replication mechanism so that the child process can obtain its own memory space, use this memory space to manage your own locks. Finally, after fork returns, the parent fork processing program runs. The parent process unlocks all the locks obtained, and multiple threads of the parent process continue to use each lock to work normally.

 

Simply put, because after a thread fork of the parent process, the child process cannot know which locks it occupies and which locks need to be released. Therefore, the parent process can first lock all the locks, and then let the child process inherit, so that the child process knows that all the locks it obtains are occupied, when it wants to use these locks, it only needs to run the child fork to unlock these locks. Similarly, after the parent process inherits the locks that it has locked, it only needs to run parent fork to unlock these locks, you can allow multiple threads to continue using these locks. (I am not sure whether the code is correct or not, and I have installed Linux on wood, so I have tried it completely ~~)

However, I still have some questions:

1. Why are copies used to describe the lock of the parent process in the book? For example, "when the parent process and child process interpret the copies of their locks...", isn't the parent process using the original locks? Why is it a copy?

 

2. Do I have to wait until the thread of the parent process releases the lock to obtain all the locks when running the prepare fork processing program?

If this is the case, will the preapare fork processing programs block?

 

 

So far, if you want to update anything in the future ~~

 

 

 

 

 

 

 

 

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.