ʵ

来自「清华大学出版社<linux c编程>的配套代码」· 代码 · 共 25 行

TXT
25
字号
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

main()
{
	int status;
	pid_t pc,pr;
	pc=fork();
	if(pc<0)	/* 如果出错 */
		printf("error ocurred!\n");
	else if(pc==0){	/* 子进程 */
		printf("This is child process with pid of %d.\n",getpid());
		exit(3);	/* 子进程返回3 */
	}
	else{		/* 父进程 */
		pr=wait(&status);
		if(WIFEXITED(status)){	/* 如果WIFEXITED返回非零值 */
			printf("the child process %d exit normally.\n",pr);
			printf("the return code is %d.\n",WEXITSTATUS(status));
		}else			/* 如果WIFEXITED返回零 */
			printf("the child process %d exit abnormally.\n",pr);
	}
}

⌨️ 快捷键说明

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