第1关:进程等待
创建子进程,创建完后,将 g_i4event 置为 1;
子进程等待 3s ,然后子进程退出;
父进程等待一秒,将 g_i4event 置为 2;然后等待子进程退出,父进程获取子进程退出的状态后将 g_i4event 置为 3;
process_wait采用 wait 函数,process_waitpid 采用 waitpid 函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// 全局变量,用于记录事件状态
int g_i4event;
// 函数:process_wait
// 功能:创建子进程并等待子进程结束
int process_wait(void)
{
// 创建子进程
int rc = fork();
// 设置全局变量 g_i4event 为 1
g_i4event = 1;
// 如果是子进程
if (rc == 0) {