28 lines
518 B
C
28 lines
518 B
C
#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;
|
|
} |