Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waitpid函数为何放在最后?不应该放在send前吗? #16

Open
avalonLZ opened this issue May 30, 2017 · 2 comments
Open

waitpid函数为何放在最后?不应该放在send前吗? #16

avalonLZ opened this issue May 30, 2017 · 2 comments

Comments

@avalonLZ
Copy link

	//waitpid(pid, &status, 0);我觉得应该放在这里啊
	 
    while (read(cgi_output[0], &c, 1) > 0)
        send(client, &c, 1, 0);

    close(cgi_output[0]);
    close(cgi_input[1]);

	/*此处存在疑问,不应该将waitpid放在前面吗?以确保子进程先运行*/
    waitpid(pid, &status, 0);

waitpid放在最后如何保证子进程结束后可以send给client?

@liuyunbin
Copy link

waitpid 是为了回收子进程使用的资源,为了防止出现僵尸进程吧,等子进程结束之后才会调用它吧,
while (read(cgi_output[0], &c, 1) > 0) 主进程还在与子进程交互。

@huanghaoa
Copy link

read带有阻塞,所以read(cgi_output[0])本来就是等待子进程(cgi)已经确定了子进程先执行,至于waitpid就是简单回收子进程防止僵尸进程

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants