mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-04-03 02:39:03 +08:00
P169 用 signal 函数进行信号处理
This commit is contained in:
29
ch10/signal.c
Normal file
29
ch10/signal.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
void timeout(int sig) //信号处理器
|
||||
{
|
||||
if (sig == SIGALRM)
|
||||
puts("Time out!");
|
||||
alarm(2); //为了每隔 2 秒重复产生 SIGALRM 信号,在信号处理器中调用 alarm 函数
|
||||
}
|
||||
void keycontrol(int sig) //信号处理器
|
||||
{
|
||||
if (sig == SIGINT)
|
||||
puts("CTRL+C pressed");
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
signal(SIGALRM, timeout); //注册信号及相应处理器
|
||||
signal(SIGINT, keycontrol);
|
||||
alarm(2); //预约 2 秒候发生 SIGALRM 信号
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
puts("wait...");
|
||||
sleep(100);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
37
ch10/wait.c
Normal file
37
ch10/wait.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int status;
|
||||
pid_t pid = fork(); //这里的子进程将在第13行通过 return 语句终止
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Child PID: %d \n", pid);
|
||||
pid = fork(); //这里的子进程将在 21 行通过 exit() 函数终止
|
||||
if (pid == 0)
|
||||
{
|
||||
exit(7);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Child PID: %d \n", pid);
|
||||
wait(&status); //之间终止的子进程相关信息将被保存到 status 中,同时相关子进程被完全销毁
|
||||
if (WIFEXITED(status)) //通过 WIFEXITED 来验证子进程是否正常终止。如果正常终止,则调用 WEXITSTATUS 宏输出子进程返回值
|
||||
printf("Child send one: %d \n", WEXITSTATUS(status));
|
||||
|
||||
wait(&status); //因为之前创建了两个进程,所以再次调用 wait 函数和宏
|
||||
if (WIFEXITED(status))
|
||||
printf("Child send two: %d \n", WEXITSTATUS(status));
|
||||
sleep(30);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
28
ch10/waitpid.c
Normal file
28
ch10/waitpid.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int status;
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
sleep(15); //用 sleep 推迟子进程的执行
|
||||
return 24;
|
||||
}
|
||||
else
|
||||
{
|
||||
//调用waitpid 传递参数 WNOHANG ,这样之前有没有终止的子进程则返回0
|
||||
while (!waitpid(-1, &status, WNOHANG))
|
||||
{
|
||||
sleep(3);
|
||||
puts("sleep 3 sec.");
|
||||
}
|
||||
|
||||
if (WIFEXITED(status))
|
||||
printf("Child send %d \n", WEXITSTATUS(status));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
23
ch10/zombie.c
Normal file
23
ch10/zombie.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
puts("Hi, I am a child Process");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Child Process ID: %d \n", pid);
|
||||
sleep(30);
|
||||
}
|
||||
|
||||
if (pid == 0)
|
||||
puts("End child proess");
|
||||
else
|
||||
puts("End parent process");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user