当前位置:   article > 正文

实验三 进程管理_编写一个c (c++/java)程序,并使用系统调用fork( )创建一个子进程

编写一个c (c++/java)程序,并使用系统调用fork( )创建一个子进程

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

代码:

#include<stdio.h>  
#include<unistd.h>  
#include<stdlib.h>  
#include<sys/types.h>
#include<sys/wait.h>  
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<stdio.h>  
#include<sys/types.h>  
#include<sys/wait.h>  
#include<unistd.h>  
#include<stdlib.h>  
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<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>  
#include<sys/wait.h>
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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/210813
推荐阅读
相关标签
  

闽ICP备14008679号