experiment-process management

experiment-process creation

1. Purpose of the experiment

  1. Deepen the understanding of the concept of process, and further understand the essence of concurrent execution
  2. Master the process creation and termination operations of the Linux operating system
  3. Master the operation of creating a child process and loading a new image in the Linux system.

2. Experiment content
(1) Write a C program and use the system call fork( ) to create a child process. Requirements: ① In the child process, output the prompt that the current process is a child process, the PID of the current process and the PID of the parent process, determine the return value of the current process according to user input, and exit prompts. ②In the parent process, output: the prompt that the current process is the parent process, the PID of the current process and the PID of the child process, the return value obtained after waiting for the child process to exit, and the exit prompt.
(2) Write a C program, use the system call fork() to create a child process, and the child process calls the exec family function to execute the system command ls.

3. Experimental steps

1. The first experiment writing process, use vim to edit the code and then run

#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. Code for Experiment 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;
}

4. Experimental results

Experiment 1 running results:

insert image description here

Experiment 2 running results

insert image description here

5. Experimental summary and thinking
(1) Summarize the three return situations of calling the fork() function.
When the return value of the fork function is less than 0, it means that the child process was not successfully created. The original process is still executing.
When the return value of the fork function is 0, it means that the child process is successfully created, and the current process is a child process.
When the return value of the fork function is greater than 0, it means that the return value is the return value of the parent process
(2) Summary fork() When used in conjunction with wait(), try to cancel the wait() function in the parent process, and observe the running status of the process. The function
of the wait() function:
once the parent process calls wait, it will block itself immediately, and wait will automatically analyze whether the current process A child process has exited, if it finds such a child process that has become a zombie, wait will collect the information of this child process, destroy it completely and return; if it does not find such a child process, wait will Will block here until one appears.

The Wait() function needs to be used in conjunction with the fork() function. If the wait() function is used before the fork() function, it returns -1; it is normally called in the parent process and returns the PID of the child process

Cancel the wait() function in the parent process. Since there is no wait function, the parent process will execute until the end of the process, and then the child process will become a zombie process when it is executed. The running result is

insert image description here

(3) Summarize and verify the specific use of exec family functions.

Usage of six functions and corresponding parameters
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[])

The function return value is successful: the function will not return
Error: return -1, the reason for failure is recorded in error

Take the execlp function as an example, execlp("/bin/ls", "ls -al", (char *)0); Use "ls -al" to display all the files in the current directory.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45788387/article/details/120815274