Unix环境高级编程—进程控制(二)
一、函数wait和waitpid
今天我们继续通过昨天那个死爹死儿子的故事来讲(便于记忆),现在看看wait和waitpid函数。
#include<sys/wait.h>
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid , int *statloc , int options); 若成功,返回进程ID,若出错,返回0或-1
wait系列函数的作用就是通知父亲儿子的死讯的,父进程一般接收到SIGCHLD信号而调用wait,(等待终止),可能有三种情况发生:
(1).子进程都在Running,则阻塞
(2).如果一个子进程终止,则父进程获取其终止状态(statloc指针指向的空间),然后立即返回
(3).如果它没有任何子进程,则立即出错返回
wait可以返回的四个终止状态的互斥的宏,分别为:
WIFEXITED(status) 正常终止
WIFSIGNALED(status) 异常终止
WIFSTOPPED(status) 暂停子进程
WIFCONTINUED(status) 暂停后已经继续的子进程
例子:
#include "apue.h"
#include <sys/wait.h> void pr_exit(int status)
{
if ( WIFEXITED(status) )
printf("normal termination,exit status = %d\n",
WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("abnormal termination,signal number = %d\n",
WTERMSIG(status));
else if (WIFSTOPPED(status))
printf("child stopped, signal number = %d\n",
WSTOPSIG(status));
}
int
main(void)
{
pid_t pid;
int status; if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
exit(7); if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */ if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
abort(); /* generates SIGABRT */ if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */ if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
status /= 0; /* divide by 0 generates SIGFPE */ if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */ exit(0);
}
waitpid函数,顾名思义,其作用就在于可以等待特定的pid的子进程终止。此外,waitpid的options参数提供了WNOHANG(相当于不阻塞的wait)、WCONTINUED、WUNTRACED(支持作业控制)三个常量选项。
下面是一个非常经典的fork子、孙进程的示例:(两次fork,有效的避免僵死进程)
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
pid_t pid; if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* first child */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid > 0)
exit(0); /* parent from second fork == first child */ /*
* We're the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here's where we'd continue executing, knowing that when
* we're done, init will reap our status.
*/
sleep(60);
printf("second child, parent pid = %ld\n", (long)getppid());
exit(0);
} if (waitpid(pid, NULL, 0) != pid) /* wait for first child */
err_sys("waitpid error"); /*
* We're the parent (the original process); we continue executing,
* knowing that we're not the parent of the second child.
*/
sleep(30);
exit(0);
}
为了便于测试效果明显,我将父进程sleep了30秒,然后退出,将孙进程sleep了60秒,然后退出。在这段代码中,最先退出的是子进程,为了防止孙进程僵死,我们让它sleep的时间更长,而父进程执行完毕退出后,孙进程被init进程收养,孙进程执行自己的任务,执行完毕后正常退出,使得该过程有效的避免了僵死进程。
此外,waitid类似于waitpid,但更为灵活,wait3、wait4的附加参数可以返回所有子进程使用的资源概况,不在此详细说明。
二、函数exec
当我们fork一个进程后,新的进程往往要调用exec执行一个启动例程,如第七章所述图:
因此,调用exec并不创建新的进程,只是用磁盘上的一个新程序替换了当前进程的正文段、数据段、堆段、栈段。
7个不同的exec函数:
#include <unistd.h>
int execl(const char *pathname, const char *arg0, ... /* (char *)0 */ );
int execv(const char *pathname, char *const argv[]);
int execle(const char *pathname, const char *arg0, ... /* (char *)0, char *const envp[] */ );
int execve(const char *pathname, char *const argv[], char *const envp[]);
int execlp(const char *filename, const char *arg0, ... /* (char *)0 */ );
int execvp(const char *filename, char *const argv[]);
int fexecve(int fd, char *const argv[], char *const envp[]);
返回值:成功不返回,出错返回-1
七个exec函数的区别:
(1).前四个取路径名作为参数,后两个取文件名作为参数,最后一个以fd作为参数
(2).参数表的传递不同(l表示列表list,v表示矢量vector)包含l和包含v
(3).向新程序传递环境表不同。包含e和不含e
七个exec直接的关系如下图,只有execve是内核的系统调用,另外6个只是库函数,它们最终都要调用该execve系统调用。
exec函数使用例程:
#include "apue.h"
#include <sys/wait.h> char *env_init[] = { "USER=unknown", "PATH=/tmp", NULL }; int
main(void)
{
pid_t pid; if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* specify pathname, specify environment */
if (execle("/home/webber/test/echoall", "echoall", "myarg1",
"MY ARG2", (char *)0, env_init) < 0)
err_sys("execle error");
} if (waitpid(pid, NULL, 0) < 0)
err_sys("wait error"); if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* specify filename, inherit environment */
if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0)
err_sys("execlp error");
} exit(0);
}
echoall.c文件代码如下:
#include "apue.h"
int
main(int argc, char *argv[])
{
int i;
char **ptr;
extern char **environ; for (i = 0; i < argc; i++) /* echo all command-line args */
printf("argv[%d]: %s\n", i, argv[i]); for (ptr = environ; *ptr != 0; ptr++) /* and all env strings */
printf("%s\n", *ptr);
sleep(100);
exit(0);
}
这里同样是为了测试,我们sleep了100秒,模拟子进程的工作时间。
注意,需要预先把echoall.c文件编程,gcc echoall.c -o echoall ,否则,当我们exec找到该文件后无法执行。这样,我们就完成了启动子进程的一整个流程
Unix环境高级编程—进程控制(二)的更多相关文章
- UNIX环境高级编程——进程控制
一.进程标识符 ID为0的进程是调度进程,常常被称为交换进程.该进程是内核的一部分,它并不执行任何磁盘上的程序,因此也被称为系统进程.进程ID 1通常是init进程,在自举过程结束时由内核调用.ini ...
- Unix环境高级编程—进程控制(三)
一.解释器文件 解释器文件属于文本文件,起始行形式为: #! pathname[optional-argument] 我们创建一个只有一行的文件如下: #!/home/webber/test/echo ...
- unix环境高级编程----进程控制wait()
一.wait()函数 当一个进程中调用wait()函数的时候 (1)假设其全部的子程序都还在执行,则堵塞 (2)假设一个子进程已终止.则等待父进程获取其终止状态. (3)假设没有子进程,则返回错误. ...
- UNIX环境高级编程——进程管理和通信(总结)
进程管理与通信 进程的管理 进程和程序的区别: 进程: 程序的一次执行过程 动态过程,进程的状态属性会发生变化 程序:存放在磁盘上的指令.数据的有序集合 是个文件,可直观看到 程序program ...
- UNIX环境高级编程——进程基本概述
一.什么是进程 从用户的角度来看进程是程序的一次执行过程.从操作系统的核心来看,进程是操作系统分配的内存.CPU时间片等资源的基本单位.进程是资源分配的最小单位.每一个进程都有自己独立的地址空间与执行 ...
- UNIX环境高级编程——进程关系
一.终端的概念 在UNIX系统中,用户通过终端登录系统后得到一个Shell进程,这个终端成为Shell进程的控制终端(Controlling Terminal),控制终端是保存在PCB中的信息,而我们 ...
- UNIX环境高级编程——进程环境
一.main函数 C程序总是从main函数开始.当内核执行C程序时,在调用main前先调用一个特殊的启动例程.可执行程序文件将此启动例程指定为程序的起始地址--这是由连接编译器设置的,而连接编译器则由 ...
- Unix环境高级编程—进程关系
终端登录 网络登录 进程组 getpgrp(void) setpgid(pid_t pid, pid_) 会话: 是一个或多个进程组的集合,通常由shell的管道将几个进程编成一组. setsid(v ...
- UNIX环境高级编程——进程间通讯方法整理
一.无名管道pipe #include <unistd.h> int pipe(int fd [2]) 二.fifo #include <sys/stat.h> int mkf ...
随机推荐
- java值传递和引用传递的理解
java的基础数据类型有:(byte.short.int.long.float.double.char.boolean)八种 基础数据都是值传递,其他都是引用传递.但是引用传递要特别注意:String ...
- spring-cloud - 服务之间的通信
上文中已经讲述了基本环境搭建,本文基于上文环境https://www.cnblogs.com/xxpandong/p/10485172.html. spring-cloud中微服务之间通信主要有俩种形 ...
- asp.net怎样解决高并发问题
队列+多线程+couchbase缓存 ,解决高并发问题. using System; using System.Collections.Generic; using System.Linq; usin ...
- 维护一套同时兼容 iOS 6 和 iOS 7,并且能够自动适应两个系统的 UI 风格的代码
举例:ios6:test.pngios7:ios7_test.png在ios7Image.plist中添加 "test" PS:如果要统一成ios7风格,可以看看UI7Kit yo ...
- app中获取应用名称,版本等信息的方法
在app中,我们有时候需要显示一些信息,例如名称,版本等等...如果用写死的方式可能不太好,我们可以动态的读取.应用的信息主要是在info.plist这个文件中,实际就是一个xml文件,以源文件的方式 ...
- hdu1874最短路
裸裸的最短路问题,将while(scanf("%d%d", &N, &M)!=EOF)粗心写为while(scanf("%d%d", & ...
- win7 32位用pyinstaller打包Python和相关html文件 成exe
http://tieba.baidu.com/p/3060401749?traceid= 安装 pyinstaller 然后 第一步你的脚本里面要做相应处理,添加一个函数:def resource_p ...
- MFC中 CString转换为char
网上好多方法,比如强制转换: CString strTest = _T(“abcd”); char *buf = (LPSTR)(LPCTSTR)strTest; 可是都只得到了第一个字符. 后来,找 ...
- stretchableImageWithLeftCapWidth气泡拉伸
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...
- 2016.10.18kubernetes 的8080和6443端口的区别与联系
由看过的资料知道,可以使用kubectl,client libraries和REST请求来访问api. 来自官方资料: By default the Kubernetes APIserver se ...