qfedu-linux-advanced-level/day3/t13.c

28 lines
518 B
C
Raw Permalink Normal View History

2023-08-17 09:20:18 +08:00
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
// kill 案例
int main()
{
int pid = fork();
if (pid == 0)
{
while (1)
{
printf("%d 玩一会游戏\n", getpid());
sleep(2);
}
}
else if (pid > 0)
{
printf("%d 子进程正在学习\n", pid);
sleep(5);
kill(pid, SIGKILL);
printf("主进程 %d 干掉了子进程 %d \n", getpid(), pid);
}
return 0;
}