(1)
cd
(2)
acd
(3)
example10_3.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
void error_handling(char *message);
int main(){
pid_t pid;
int sock;
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
error_handling("socket() error!");
pid = fork();
if(pid == -1)
error_handling("fork() error!");
if(pid)
printf("Parent process socket: %d \n", sock);
else
printf("Child process socket: %d \n", sock);
close(sock);
return 0;
}
void error_handling(char *message){
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
(4)
子进程自身终止后,将结束状态值传递给操作系统,而操作系统不会销毁子进程,直到把这些值传递给产生该子进程的父进程,处在这种状态下的进程就是僵尸进程
可以利用wait函数和waitpid函数来主动请求获取子进程的返回值以销毁僵尸进程
(5)
example10_5.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
void query_quit(int sig);
int main(){
int i;
signal(SIGINT, query_quit);
while(1){
static int i = 0;
printf(i > 1?"wait %d seconds...\n":"wait %d second...\n", i);
++i;
sleep(1);
}
return 0;
}
void query_quit(int sig){
char message[30];
puts("Do you want to exit the program?(Y equal yes)");
fgets(message, sizeof(message), stdin);
if(!strcmp(message, "y\n") || !strcmp(message, "Y\n"))
exit(1);
}