一个进程问题,有请高手指点
在收到 accept() > 0
if (fork() == 0)
{
/* 子进程代码段 */
if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1)
{
perror("send出错!");
}
printf("first:\n");
close(client_fd);
printf("second");
exit(0);
}
printf("fourth\n");
printf("fiveth\n");
close(client_fd);
服务端运行结果
fourth
fiveth
first
second
查看 ps -ef|grep user
运行两次后 发现
8422 951 0 12:20 pts/1 00:00:00 ./server.out
8424 8422 0 12:20 pts/1 00:00:00 [server.out <defunct>]
8429 8422 0 12:20 pts/1 00:00:00 [server.out <defunct>]
子进程没有退出这样每连接一次就多产生一个进程,为什么?
怎样做到子进程运行完了,就结束了。是否父进程要wait()?怎样做?