理解fork

fog posted @ 2008年8月13日 04:47 in 技术相关 with tags linux Coding , 1855 阅读

今天因为给mini_httpd添加一个功能,用mmap给父子进程创建共享空间,又顺便复习了下fork。

fork的关键在于调用一次,返回两次。这里说的返回两次从字面上讲可能比较难于理解,但如果知道了fork在内核里的实现机制就很容易搞懂了。

简单来讲,执行fork调用后,内核将在原有进程(父进程)的基础上拷贝一份,生成一个新的进程(子进程)。这个新进程将完全拷贝父进程的数据空间,堆,栈,缓冲区(如printf)。父子进程只共享正文(代码)段。

这里需要注意的是子进程继承父进程后信号量的处理方式。当一个进程调用fork时,因为子进程在开始时复制父进程的存储映像,信号捕捉函数的地址在子进程中是有意义的,所以子进程继承父进程的信号处理方式。特殊的是exec,因为exec运行新的程序后会覆盖从父进程继承来的存储映像,那么信号捕捉函数在新程序中已无意义,所以exec会将原先设置为要捕捉的信号都更改为默认动作。

fork在父进程中返回子进程的pid,在子进程中则返回为0。

Avatar_small
ch 说:
2023年8月07日 03:35

This is such an awesome asset, to the point that you are giving and you give it away for nothing. I adore seeing sites that comprehend the benefit of giving a quality asset to free. It is the old what circumvents comes around schedule. 먹튀검증사이트

Avatar_small
ch 说:
2023年8月09日 04:26

If you don't mind proceed with this extraordinary work and I anticipate a greater amount of your amazing blog entries. Ac repair las vegas

Avatar_small
ch 说:
2023年8月28日 06:12

This is truly a decent and instructive, containing all data furthermore greatly affects the new innovation. A debt of gratitude is in order for sharing it 1-inch

Avatar_small
ch 说:
2023年10月04日 03:34

This is my first time i visit here. I discovered such a variety of intriguing stuff in your website particularly its dialog. From the huge amounts of remarks on your articles, I figure I am not by any means the only one having all the happiness here! keep doing awesome www.mt-spot.com

Avatar_small
jam 说:
2023年11月01日 15:31

Thanks for the concepts you write about through this website. In addition, numerous young women which become pregnant usually do not even seek to get health insurance because they have anxiety they wouldn’t qualify. Although many states currently require that insurers offer coverage no matter what about the pre-existing conditions. Fees on these guaranteed programs are usually higher, but when thinking about the high cost of medical care it may be any safer strategy to use to protect your current financial potential. Sports leggings

Avatar_small
jam 说:
2023年11月03日 00:57

Can I recently say thats a relief to uncover somebody who actually knows what theyre preaching about over the internet. You actually learn how to bring a problem to light and work out it critical. The diet need to read this and understand why side of your story. I cant think youre less well-known since you absolutely provide the gift. Packers and Movers Kolkata to Ahmedabad

Avatar_small
jam 说:
2023年11月14日 22:18 I have not checked in here for a while as I thought it was getting boring, but the last several posts are great quality so I guess I?ll add you back to my daily bloglist. You deserve it my friend f8bet casino
Avatar_small
jam 说:
2023年11月16日 19:54

I was just looking for this info for some time. After six hours of continuous Googleing, at last I got it in your site. I wonder what is the lack of Google strategy that don’t rank this type of informative web sites in top of the list. Normally the top websites are full of garbage. 868VIP

Avatar_small
jam 说:
2023年11月17日 23:25

Are not they in a hurry? Do not they want to find the outpost? Need not Kirk get back to the ship? Yet they sit there, having a talk and barbecue around a log fire. 79king game

Avatar_small
jam 说:
2023年11月21日 15:29

Get daily teer result, shillong teer result, khanapara teer result, teer result common number daily success live teer result here. shillong teer result

Avatar_small
jam 说:
2023年11月22日 14:04

Beging with the entire wales well before just about any planking. Our own wales can easily compilation of calculated forums those thickness analysts could be the similar to some of the shell planking along with more significant damage so that they project after dark planking. planking lịch thi đấu bóng đá

Avatar_small
jam 说:
2023年11月22日 21:35

many thanks for telling!. “Truth is generally the best vindication against slander.” by Abraham Lincoln.. f8bet game

Avatar_small
jam 说:
2023年11月23日 21:40

Hi there, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam feedback? If so how do you stop it, any plugin or anything you can suggest? I get so much lately it’s driving me crazy so any support is very much appreciated. ผลบอลสด888

Avatar_small
jam 说:
2023年11月24日 16:18

I like the many blogposts, I seriously liked, I want details about it, since it is rather wonderful., Cheers pertaining to expressing. UFABETเว็บตรงที่ดีที่สุด

Avatar_small
jam 说:
2023年11月24日 18:43

I like the many blogposts, I seriously liked, I want details about it, since it is rather wonderful., Cheers pertaining to expressing. UFABETเว็บพนันตรง

Avatar_small
jam 说:
2023年11月24日 20:37

Rapidly this website may irrefutably end up being well-known amongst just about all running a blog individuals, because of its persistent content articles or even evaluations. UFABETเว็บพนันตรงไม่มีขั้นต่ำ

Avatar_small
jam 说:
2023年11月24日 22:31

I favor your current article. It can be very good to view anyone explain in words through the cardiovascular along with lucidity for this critical issue might be quickly seen. UFABETฝากถอนไม่มีขั้นต่ำ

Avatar_small
jam 说:
2023年11月25日 00:07

I'd like a person's location. It really is better than notice everybody explain in words inside the life blood as well as legibility about this topic required region are often easily discovered. UFABETฝากขั้นต่ำ

Avatar_small
jam 说:
2023年11月25日 13:27

An impressive share, I just now with all this onto a colleague who had been doing a little analysis with this. And he the fact is bought me breakfast simply because I discovered it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending some time to discuss this, I’m strongly regarding it and really like reading regarding this topic. When possible, as you become expertise, does one mind updating your website with more details? It can be highly great for me. Massive thumb up for this short article! https://www.tripadvisor.com/Profile/mmedispa1

Avatar_small
jam 说:
2023年11月25日 19:16

Rapidly your web page will definitely it goes without saying develop into widely known having enable managing some sort of blog site individuals, to its aware content articles or even examine articles or blog posts. 777 Gems online game

Avatar_small
jam 说:
2023年11月26日 18:42

Immediately that awesome web page really should irrefutably grown into known in the many webpage the majority of people, a result of the diligent articles or blog posts or maybe opinions or possibly responses. UFABETสมัครแทงบอลฟรีไม่มีขั้นต่ำ

Avatar_small
jam 说:
2023年11月28日 22:38

My brother recommended I may like this website. He was once totally right. This put up actually made my day. You can not believe simply how a lot time I had spent for this information! Thanks! Alien Labs Vape


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter