wait函数简单介绍
来源:互联网 发布:淘宝 论坛 编辑:程序博客网 时间:2024/06/05 05:49
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>int main(){ pid_t pid; char *msg; int k; int exit_code; printf("Study how to get exit code!\n"); pid = fork(); switch(pid) { case 0: msg = "Child process is running!\n"; k = 5; exit_code = 37; break; case -1: perror("Process Creation Failed!\n"); exit(1); default: exit_code = 0; break; }//父进程都会执行以下这段代码//子进程的pid为0,父进程的pid为子进程的ID if(pid != 0) { int stat_val; pid_t child_pid; child_pid = wait(&stat_val); printf("Child process has exited,pid = %d\n",child_pid); if(WIFEXITED(stat_val)) printf("Child exited with code %d\n",WEXITSTATUS(stat_val)); else printf("Child exited abnormally!\n"); } else { while(k-- > 0) { puts(msg); sleep(2); } } exit(exit_code);}
执行结果:
[root@localhost code]# ./wait Study how to get exit code!Child process is running!Child process is running!Child process is running!Child process is running!Child process is running!Child process has exited,pid = 2975
父进程遇到wait函数先会挂起,知道子进程执行完成后在执行!
0 0
- wait函数简单介绍
- wait,notify,notifyAll简单介绍
- linux下的wait函数和waitpid函数介绍
- wait函数
- wait 函数
- wait函数
- wait()函数
- java多线程入门:wait、notify、notifyAll函数介绍
- 内联函数简单介绍
- 虚函数 简单介绍
- SQLServer 函数简单介绍
- poll 函数简单介绍
- 函数的简单介绍
- wait()函数,waitpid()函数
- ShowModalDialog函数的简单介绍
- SSL编程- 简单函数介绍
- linuxc fork函数简单介绍
- Ackermann函数的简单介绍
- go 的包安装准备
- orcle profile使用详解
- 同步与异步的区别
- 【hoj】 1017 Joseph's problem II
- Office2010密钥过期怎么办(产品激活失败)
- wait函数简单介绍
- 属性在ios中的使用
- 一个新的App该如何推广?
- .NET源码中的Stream类
- Linux下实现U盘、SD卡自动挂载功能
- 贪心算法
- 马士兵struts2视频教程第七集
- QiQi的排列 - 博创OJ 1129 水题
- 第一个值出现一次的字符(面试题 35)