实验一 进程管理

实验一 进程创建

一、实验目的

  1. 加深对进程概念的理解,进一步认识并发执行的实质
  2. 掌握Linux操作系统的进程创建和终止操作
  3. 掌握在Linux系统中创建子进程后并加载新映像的操作。

二、实验内容
(1)编写一个C程序,使用系统调用fork( )创建一个子进程。要求:①在子进程中分别输出当前进程为子进程的提示、当前进程的PID和父进程的PID、根据用户输入确定当前进程的返回值、退出提示等信息。②在父进程中分别输出:当前进程为父进程的提示、当前进程的PID和子进程的PID、等待子进程退出后获得的返回值、退出提示等信息。
(2)编写C程序,使用系统调用fork()创建一个子进程,子进程调用exec族函数执行系统命令ls。

三、实验步骤

1.第一个实验编写过程,使用vim编辑代码然后运行

#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<sys/wait.h>
#include<stdio.h>

int main(){
    
    

        pid_t childpid; /*variable to store the child's pid*/
        int retval;    /*user-provided return code for child process*/
        int status;    /*child's exit status for parent process*/

        /*create new process*/
        childpid = fork()
        if (childpid >= 0) {
    
    
                if (childpid == 0) {
    
    
                        printf("Child:I am the child process\n");
                        printf("Child:My PID is %d\n",getpid());
                        printf("Child:My parent's PID is %d\n",getppid());
                        printf("Child:The value of fork return is %d\n",childpid);
                        printf("Child:Sleep for one second...\n");
                        sleep(1);
                        printf("Child:Enter an exit value (0~255): ");
                        scanf("%d",&retval);
                        printf("Child:Goodbye! \n");
                        exit(retval);             /*child exits with user-provided return code*/
               }
                else {
    
    
                        printf("Parent:I am the parent process!\n");
                        printf("Parent:My PID is %d\n",getpid());
                        printf("Parent:The value of my child's PID is %d\n", childpid);
                        printf("Parent:I will now wait for my child to exit.\n");
                        wait(&status);
                        printf("Parent:Child's exit code is %d\n",WEXITSTATUS(status));
                        printf("Parent:Goodbye!\n");
                        exit(0);

                }

        }
        else {
    
    
                perror("fork error\n");
                exit(0);
        }

        return 0;
}

2.实验2的代码

#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>

int main() {
    
    

        int rtn;                     /*the return  value of the son process*/
        if (fork() == 0) {
    
    
                /*The son process execution this process */
                execlp("/bin/ls","ls -al",(char *)0);

                exit(21);   /*If the exec function return it's program,
                             that indicate it didn't exec the order normally,
                             so it must print the failure information*/
        }
        else {
    
    
                /*the father process, is waiting the end of the son process and print the return value of the son process*/
                wait(&rtn);
                printf("child process return %d\n", rtn);
        }

        return 0;
}

四、实验结果

实验1运行结果:

在这里插入图片描述

实验2 运行结果

在这里插入图片描述

五、实验总结与思考
(1)总结调用fork()函数的三种返回情况
当fork函数返回值为 小于0,表示没有成功创建子进程。原来的进程仍在执行
当fork函数返回值为 0,表示子进程创建成功,且当前进程为 子进程
当fork函数返回值为 大于0,表示返回值为父进程的返回
(2)总结fork()和wait()配合使用的情况,并尝试在父进程中取消wait()函数,观察进程运行情况
wait()函数的功能:
父进程一旦调用了wait就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出,如果让它找到了这样一个已经变成僵尸的子进程,wait就会收集这个子进程的信息,并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到有一个出现为止。

Wait()函数需要和fork()函数配套使用,如果wait()函数在fork()函数之前使用,返回-1;正常在父进程中调用返回子进程的 PID

在父进程中取消wait()函数,由于没有wait函数,父进程会一直执行到进程结束,然后子进程执行的时候就会变成僵尸进程,运行结果为

在这里插入图片描述

(3)总结、验证exec族函数的具体使用。

六个函数的用法和相应参数
int execl(const char *path, const char *arg, …)
int execv(const char *path, char *const argv[])
int execle(const char *path, const char *arg, …, char *const envp[])
int execve(const char *path, char *const argv[], char *const envp[])
int execlp(const char *file, const char *arg, …)
int execvp(const char *file, char *const argv[])

函数返回值 成功:函数不会返回
出错:返回-1,失败原因记录在error中

以execlp函数为例,execlp("/bin/ls",“ls -al”,(char *)0); 使用 “ls -al”将当前目录下的文件都展示出来。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45788387/article/details/120815274