前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >8(进程控制)

8(进程控制)

作者头像
提莫队长
发布2019-02-21 10:45:57
4860
发布2019-02-21 10:45:57
举报
文章被收录于专栏:刘晓杰

本章需要熟练掌握如下几个函数fork,exec族,_exit,wait,waitpid

1 进程标识符

代码语言:javascript
复制
#include <unistd.h>
pid_t getpid(void);
        Returns: process ID of calling process
pid_t getppid(void);
        Returns: parent process ID of calling process
uid_t getuid(void);
        Returns: real user ID of calling process
uid_t geteuid(void);
        Returns: effective user ID of calling process
gid_t getgid(void);
        Returns: real group ID of calling process
gid_t getegid(void);
        Returns: effective group ID of calling process

2 fork函数

代码语言:javascript
复制
#include <unistd.h>
pid_t fork(void);
        Returns: 0 in child, process ID of child in parent, 1 on error

Fork函数执行一次但返回两次。父进程的返回值是子进程的进程ID,子进程的返回值是0(并不代表子进程的进程ID是0) 子进程和父进程并不共享存储空间,仅是父进程的副本。 父子进程调用顺序不确定

代码语言:javascript
复制
#include "apue.h"
int     glob = 6;       /* external variable in initialized data */
char    buf[] = "a write to stdout\n";
Int main(void)
{
    int       var;      /* automatic variable on the stack */
    pid_t     pid;

    var = 88;
    if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1)
        err_sys("write error");
    printf("before fork\n");    /* we don't flush stdout */

    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {      /* child */
        glob++;                 /* modify variables */
        var++;
    } else {
        sleep(2);               /* parent */
    }

    printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
    exit(0);
}
这里写图片描述
这里写图片描述

3 vfork函数

代码语言:javascript
复制
#include "apue.h"
int     glob = 6;       /* external variable in initialized data */
Int main(void)
{
    int     var;        /* automatic variable on the stack */
    pid_t   pid;

    var = 88;
    printf("before vfork\n");   /* we don't flush stdio */
    if ((pid = vfork()) < 0) {
        err_sys("vfork error");
    } else if (pid == 0) {      /* child */
        glob++;                 /* modify parent's variables */
        var++;
        _exit(0);               /* child terminates */
    }
    /*
     * Parent continues here.
     */
    printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
    exit(0);
}

和fork的两个区别: 1.子进程在调用exec之前在父进程空间中运行,共享内存 2.保证子进程先运行,内核使父进程休眠,所以不用sleep函数 exit和_exit就是用来正常终止一个进程的,主要区别是_exit会立刻进入内核,而exit先执行一些清除工作(包括执行各种终止处理程序,关闭所有标准I/O等,一旦关闭了IO,例如Printf等函数就不会输出任何东西了),然后才进入内核。这两个函数会对父子进程有一定的影响,当用vfork创建子进程时,子进程会先在父进程的地址空间运行(这跟fork不一样),如果子进程调用了exit就会把父进程的IO给关掉。

这里写图片描述
这里写图片描述

接下来我们可以做个小测试,将vfork改成fork,预估glob=6,var=88.因为子进程修改的是副本

这里写图片描述
这里写图片描述

4 wait和waitpid函数

代码语言:javascript
复制
#include <sys/wait.h> 
pid_t wait(int *statloc); 
pid_t waitpid(pid_t pid, int *statloc, int options);
成功返回进程ID,出错返回-1

当一个进程正常终止时,内核就向父进程发送SIGCHLD信号。因为子进程终止是个异步事件,所以发送的也是异步信号。 对于调用了Wait和waitpid的进程会发生如下情况: (1)子进程还在运行,则阻塞 (2)如果子进程已终止,正在等待父进程获取其终止状态,则取得该进程的终止状态并立即返回 (3)如果没有子进程,则立即出错返回 两者区别: 1.子进程终止前,wait使其调用者阻塞,而waitpid有一个选项,可使调用者不阻塞 2.Waitpid有若干选项,可以控制它所等待的进程

这里写图片描述
这里写图片描述
代码语言:javascript
复制
#include "apue.h"
#include <sys/wait.h>
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);
}
//修改代码。输出status的同时也让其输出pid。pr_exit(status)改成printf(“%d %d\n”, status, pid);
这里写图片描述
这里写图片描述

调用fork两次来避免僵死进程

代码语言:javascript
复制
#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(2);//保证父进程先执行
        printf("second child, parent pid = %d\n", 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.
     */
    exit(0);
}

第二个子进程调用sleep保证在打印父进程ID时第一个子进程已终止。如果没有sleep,它可能比父进程先执行,那么返回的ID将会是父进程ID。,而不是init进程(值为1)。 运行程序得到

代码语言:javascript
复制
$ ./a.out
$ second child, parent pid = 1

5 exec函数

当进程调用exec函数时,该进程的程序完全替换为新程序,而新程序从main函数开始执行。调用exec并不创建新进程,所以前后的进程ID并未改变。 Fork创建新进程,exec执行新程序,exit和两个wait函数处理终止和等待终止

代码语言:javascript
复制
#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 []);
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
代码语言:javascript
复制
#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/sar/bin/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);
}

在该程序中先调用execle,它要求一个路径名和一个特定的环境。下一个调用的是execlp,它用一个文件名将环境传送给新程序。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年04月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 进程标识符
  • 2 fork函数
  • 3 vfork函数
  • 4 wait和waitpid函数
  • 5 exec函数
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档