wait.c

来自「国嵌所有的实验代码」· C语言 代码 · 共 35 行

C
35
字号
#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <math.h>int main(void){	pid_t child;	/* 创建子进程 */	if((child=fork())==-1)	{		printf("Fork Error : %s\n", strerror(errno));		exit(1);	}	else 		if(child==0) // 子进程		{			printf("the child process is run\n");			sleep(1);  //子进程睡眠一秒,但并没有去运行父进程			printf("I am the child: %d\n", getpid());			exit(0);		}		else        //父进程		{			wait(NULL); //等到子进程退出,父进程才会运行			printf("the father process is run\n");			printf("I am the father:%d\n",getpid());			return 0;		}}	

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?