• 实验三 进程管理


    1.编写一个C 程序,使用系统调用fork()创建一个子进程,并使用这个子进程调用exec 函数族以执行系统命令ls

    代码:

    #include  
    #include  
    #include  
    #include
    #include  
    int main(){       
        int pid;     
        pid=fork(); /* 创建子进程 */         
        switch(pid){  
            case -1:     /* 创建失败 */        
                printf("fork fail!\n");  
                exit(1);  
            case 0:  /* 子进程*/              
                printf("Child process PID:%d.\n",getpid());  
                execlp("/bin/ls","ls",NULL); /* 装载子进程映像 ls 命令*/  
                printf("exec fail!\n");  
                exit(1);  
            default:  /* 父进程*/                      
                printf("Parent process PID: %4d.\n",getpid());  
                wait(NULL);  /* 父进程等待子进程运行完毕 */        
                printf("ls completed !\n");  
                exit(0);  
        }  
         return 0;  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    结果:
    在这里插入图片描述

    问题1:该程序中一共有几个进程并发?
    两个,父进程和子进程(应该是吧)

    问题2:wait(NULL)起到了什么作用,如果删除会出现什么情况,为什么?

    wait()调用后立即阻塞自己,直到当前进程的某个子进程退出。其参数用来保存被收集进程退出时的一些状态,它是一个指向int类型的指针。但如果我们对这个子进程是如何死掉的毫不在意,只想把这个僵尸进程消灭掉,我们就可以设定这个参数为NULL。

    2.编写一个C程序,使用系统调用fork()创建一个子进程,并使这个子进程在退出时通过exit()给出退出参数,父进程通过wait()收集子进程返回的参数并显示

    代码:

    #include  
    #include  
    #include  
    #include  
    #include  
    int main(){  
        int status;  
        pid_t pc,pr;  
        pc=fork();  
        if(pc<0)       
            printf("error ocurred!\n");   
        else if(pc==0){  
            printf("This is child process with pid of %d.\n",getpid());   
            exit(3);  
        }  
        else{  
            pr=wait(&status);  
            if(WIFEXITED(status)){  
                printf("the child process exit normally.\n");  
                printf("the return code is %d.\n",WEXITSTATUS(status));  
            }  
            else  
                printf("the child process exit abnormally.\n");   
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    结果:
    在这里插入图片描述

    问题1:该程序中的pr变量的值代表的什么含义?
    pr的值代表子进程的pid

    问题2:如果程序中出现出现exit(0)和exit(1)代表什么意思?
    exit(0)表示进程正常终止,exit(1)表示进程运行有错,异常终止

    3.编写程序:创建一对父子进程,实验wait同步函数。要求子进程休眠5秒,父进程不休眠。但需要父进程等待子进程,并在子进程结束后收集子进程用exit返回的参数

    代码:

    #include
    #include
    #include
    #include  
    #include
    int main(){
    	int status;
    	pid_t pid = fork();
    	if(pid<0){
    	    printf("error ocurred!\n");	
    	}
    	else if(pid==0){
    	    printf("This is child process with pid of %d.\n",getpid());	
    	    sleep(5);
    	    exit(0);
    	}
    	else{
    	    printf("Parent process PID: %4d.\n",getpid());
    		wait(&status);
    		printf("the child return code is %d.\n",WEXITSTATUS(status));
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    手撕——排序
    Leetcode—125.验证回文串【简单】
    【MATLAB教程案例19】优化类算法的应用,如何针对不同问题选择不同的优化类算法
    外中断的应用
    【无标题】Gradle学习
    这款键盘你真的要考虑一下!——Keychron K3测评
    正则表达式
    ENVI_IDL: 如何读取HDF5文件再优美地做个均值处理?(以OMI数据为例)
    【C++进阶学习】第六弹——set和map——体会用C++来构建二叉搜索树
    【前端面试必知】圣杯布局和双飞翼布局
  • 原文地址:https://blog.csdn.net/fjdep/article/details/127595600