系统级程序设计第三课

1exec函数族

使用fork()函数创建的子进程,其中包含的程序代码完全相同,只能根据fork()函数的返回值,执行不同的代码分支。
由exec函数族中的函数,则可以根据指定的文件名或路径,找到可执行文件。

在这里插入图片描述

  • fork:子进程复制父进程的堆栈段和数据段,子进程一旦开始运行,它继承了父进程的一切数据,但实际上数据却已经分开,相互之间不再影响
  • exec:一个进程调用exec类函数,它本身就"死亡"了,系统把代码段替换成新的程序代码,废弃原有数据段和堆栈段,并为新程序分配新数据段与堆栈段。
  • test_exec.c
  • #include <stdio.h>
  • #include <stdlib.h>
    #include <unistd.h>
    int main(){
        pid_t tempPid;
        tempPid=fork();
        if(tempPid == -1){   
            perror("fork error");
            exit(1);
        } else if(tempPid > 0) {   
            printf("parent process:pid=%d\n", getpid());
        } else {   
            printf("child process:pid=%d\n", getpid());
            //execl("/bin/ls","-a","-l","test_exec.c",NULL);    //①
            //execlp("ls","-a","-l","test_exec.c",NULL);    //②
            char *arg[]={"-a","-l","test_exec.c", NULL};    //③
            execvp("ls", arg);
            perror("error exec\n");
            printf("child process:pid=%d\n", getpid());
        } //of if  
        return 0;
    } //of main
  • 2

    进程退出

    #include <stdlib.h>
    void exit(int status);

 3

wait函数

#include <sys/wait.h>
pid_t wait(int *status);

 

【案例 1】若子进程p 1 p_1p 
1

 是其父进程p pp的先决进程,基于wait函数使得进程同步。

test_wait.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(){
    pid_t tempPid, tempW;
    tempPid = fork();
    if(tempPid == -1){
        perror("fork error");
        exit(1);
    }else if(tempPid == 0){//child
        sleep(3);
        printf("Child process, pid = %d, ppid = %d\n", getpid(), getppid());
    }else{//parent 
        tempW = wait(NULL);
        printf("Catched a child process, pid = %d, ppid = %d\n", tempW, getppid());
    }//of if
    printf("......finish......");
    return 0;
}//of main
案例2:

test_wait2.c
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
    int tempStatus;
    pid_t tempPid, tempW;
    tempPid = fork();
    if(tempPid == -1){
        perror("fork error");
        exit(1);
    } else if(tempPid == 0){//子
        sleep(3);
        printf("Child process: pid=%d\n",getpid());
        exit(5);
     }  else{//父
        tempW = wait(&tempStatus);
        if(WIFEXITED(tempStatus)){
            printf("Child process pid=%d exit normally.\n", tempW );
            printf("Return Code:%d\n",WEXITSTATUS(tempStatus));
        } else {
            printf("Child process pid=%d exit abnormally.\n", tempW);
        }//of if
    }//of if
    return 0;
}//of main
4

waitpid函数

#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *status, int options);

案例1:

test_waitpid.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(){
    pid_t tempPid, tempP, tempW;
    tempPid= fork();                            //创建第一个子进程
    if (tempPid == -1){                            
        perror("fork1 error");
        exit(1);
    } else if (tempPid == 0){                        //子进程沉睡
        sleep(5);
        printf("First child process:pid=%d\n", getpid());
    } else {                        //父进程继续创建进程
        int i;
        tempP = tempPid;
        for (i = 0; i < 3; i++){                    //由父进程创建3个子进程
            if ((tempPid = fork()) == 0){
                break;
            }//of if
        }//of for i
        if (tempPid == -1){                        //出错
            perror("fork error");
            exit(2);
        } else if (tempPid == 0){                    //子进程
            printf("Child process:pid=%d\n", getpid());
            exit(0);
        } else {                    //父进程
            tempW = waitpid(tempP, NULL, 0);            //等待第一个子进程执行
            if (tempW == tempP){
                printf("Catch a child Process: pid=%d\n", tempW);
            }else{
                printf("waitpid error\n");
            }//of if
        }//of if
    }//of if
    return 0;
}//of main
案例2:

test_waitpid2.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
    pid_t tempPid, tempW;
    tempPid = fork();
    if (tempPid == -1){
        perror("fork error");
        exit(1);
    } else if (tempPid == 0){
        sleep(3);
        printf("Child process:pid=%d\n", getpid());
        exit(0);
    } else {
        do{
            tempW = waitpid(tempPid, NULL, WNOHANG);
            if (tempW == 0){
                printf("No child exited\n");
                sleep(1);
            }//of if
        } while (tempW == 0);
        if (tempW == tempPid){
            printf("Catch a Child process:pid=%d\n", w);
        }else{
            printf("waitpid error\n");
        }//of if
    }//of if
    return 0;
}//of main
总结:通过这次可对于进程中的exec函数组和wait函数有了初步的了解

对于进程的程序有了更好的了解。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《JavaScript高程序设计(第3版)》是一本权威的JavaScript编程指南,涵盖了JavaScript语言的各个方面和高特性。本书由Nicholas C. Zakas撰写,是一位知名的JavaScript专家和作者。 该书详细介绍了JavaScript的基本语法、数据类型、控制流程等基础知识,并深入探讨了函数、对象、正则表达式等高特性。此外,本书还介绍了DOM操作、事件处理、Ajax、JSON等Web开发相关的内容。读者可以通过学习本书,系统地掌握JavaScript的各个方面,从而能够编写出可维护、高效、优雅的JavaScript代码。 《JavaScript高程序设计(第3版)》以清晰、易懂的语言讲解了复杂的概念,并配有丰富的示例代码和实际案例,帮助读者理解和应用所学知识。本书不仅适合初学者,也适合有一定JavaScript基础的开发者作为参考手册使用。 随着Web技术的快速发展,JavaScript已经成为构建现代Web应用的重要工具之一。《JavaScript高程序设计(第3版)》为读者提供了全面、深入的学习和理解JavaScript的资源。无论是对于Web开发者还是对于想要了解JavaScript的任何人来说,这本书都是一本不可或缺的经典之作。 总之,《JavaScript高程序设计(第3版)》是一本内容丰富、权威性强的JavaScript编程指南,它深入讲解了JavaScript的各个方面和高特性,对于理解和应用JavaScript具有很高的参考价值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值