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 ...
随机推荐
- ef core 2.1 利用Query Type查询视图
ef core新加入的功能“Query Type”可以让我们很方便的查询视图而不需要做任何特殊处理.不过在代码上和普通的查询有些不同. 先贴文档:https://docs.microsoft.com/ ...
- 洛谷 P1031 均分纸牌【交叉模拟】
题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若干张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 ...
- asp.net获取URL方法
方法如下: Request.Url.ToString()获取完整url(协议名+域名+站点名+文件名+参数):https://localhost:44300/WebForm1.aspx?abc=123 ...
- 通信API、使用Web Workers处理线程
1.跨文档消息传输 要想接受从其他的窗口那里发过来的消息,就必须对窗口对象的message事件进行监视. w ...
- 如何让自己的程序支持iPhone5–RetinaImages
我们知道如果想让程序的启动图像能够适应iPhone5(640X1136),那么我们需要把启动的图像命名以“-568h@2x”结尾的图片,那 么是不是程序中用到的所有图片都可以用这样的方式来命名,以适合 ...
- kindeditor编辑器,获取textarea值
在获取textarea值的时候,从数据库读出来的值都能获取到,但是新输入的值就得不到,只要是新输入的都得不到值 答案: 我昨天刚用kindeditor,我是使用ajaxForm提交表单的在360浏览器 ...
- 解决 ultraedit 菜单字体模糊
新装完ultraedit后,使用时菜单里的字体非常小,模糊不清非常不爽. 1.安装好UltraEdit后,到安装目录里,复制一份Uedit32.exe文件用于修改.2.使用UltraEdit打开复制的 ...
- 在PythonAnyWhere上部署Django项目
http://www.jianshu.com/p/91047e3a4ee9 将项目放到git上,然后将pathonanywhere上的ssh传到git上,没有的话先创建,然后从git上把项目拷贝到pa ...
- 【前端GUI】—— 前端设计稿切图通用性标准
前言:公司在前端组和视觉组交接设计稿切图的时候,总会因为视觉组同事们对前端的实现原理不清楚而出现各种问题,在用的时候还得再次返工,前端组同事们一致觉得应该出一份<设计稿切图通用性标准文件> ...
- jQuery ajax 获取信息展示在“下拉列表”中
<link href="${ctxStatic}/jquery-select2/4.0.3/select2.min.css" rel="stylesheet&quo ...