2024-02-24 01:29:59 +00:00
|
|
|
|
#include <stdio.h>
|
2024-02-24 02:05:41 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int weak_process(){
|
|
|
|
|
printf("get signal\n");
|
|
|
|
|
}
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
// 获取进程ID
|
|
|
|
|
printf("I am %d \n", getpid());
|
|
|
|
|
printf("My parent is %d\n", getppid());
|
|
|
|
|
// 打印环境变量
|
|
|
|
|
// char **env = environ;
|
|
|
|
|
// while (*env != NULL)
|
|
|
|
|
// {
|
|
|
|
|
// printf("%s\n", *env);
|
|
|
|
|
// *env++;
|
|
|
|
|
// }
|
|
|
|
|
// fock创建进程
|
|
|
|
|
// fock创建的子进程一般用来执行新的程序
|
2024-02-24 02:10:20 +00:00
|
|
|
|
/**
|
|
|
|
|
* 创建一个信号量用于唤醒进程
|
|
|
|
|
* 子进程睡眠10s
|
|
|
|
|
* 父进程睡眠1s后,把子进程叫醒
|
|
|
|
|
* 子进程结束后,父进程结束
|
|
|
|
|
*/
|
2024-02-24 02:05:41 +00:00
|
|
|
|
signal(SIGUSR1,weak_process);
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
if (pid == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("I am child process %d\n", getpid());
|
|
|
|
|
sleep(10);
|
|
|
|
|
_exit(0);
|
|
|
|
|
}
|
|
|
|
|
else if (pid > 0)
|
|
|
|
|
{
|
|
|
|
|
sleep(1);
|
|
|
|
|
kill(pid,SIGUSR1);
|
|
|
|
|
wait(NULL);
|
|
|
|
|
// printf("pit = %d\n",pid);
|
|
|
|
|
printf("I am parent process %d\n", getpid());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("fork error\n");
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 01:29:59 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|