Linux system programming process learning

Process related concepts

1. What is a program, what is a process, and what is the difference?

Program is a static concept. The pro file generated by gcc xxx.c -o pro is called a program. A process is a running activity of the program. When the program runs, there is one more process in the system. Each process has a non-negative integer representing a unique ID. , called pid

        pid = 0 swapper process (swapper) Function: process scheduling
        pid = 1 init process Function: system initialization


1. Process creation

1. Create using fork function

        pid_t fork(void);

        The fork function is called successfully and returns twice

        The return value is 0, which means the current process is a child process

        The return value is a non-negative number, indicating that the current process is the parent process, and the return value is the ID number of the child process.

        The call fails and returns -1

#include<stdio.h>

#include <sys/types.h>
#include <unistd.h>



int main()
{
        pid_t pid;
        pid_t pid2;
        pid_t returnpid;
        pid = getpid();
        printf("befer print:%d\n",pid);

        returnpid = fork();

        pid2 = getpid();
        printf("after print:%d\n",pid2);

        if(pid == pid2){

                printf("this father print:%d   returnpid=%d\n",getpid(),returnpid);
        }else{

                printf("this child print:%d    returnpid=%d\n",getpid(),returnpid);
        }


        return 0;
}

operation result

befer pint:8173
after print:8173
this father print:8173   returnpid=8174
after print:8174
this child print:8174    returnpid=0

The code before fork can only be executed by the parent process. The parent process and child processes each have a copy of the code after fork. The parent process and child processes will run once. As for who runs first, it depends on the scheduling of the process.

2. Use the vfork function to create a process

The difference between vfork creation process and fork:

1. vfork ensures that the child process runs first. When the child process calls exit to exit, the parent process is executed.

2.vfork directly uses the storage space of the parent process without copying. The variables of the parent process can be changed by the child process.

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
main()
{
        int cnt = 0;
        pid_t pid;
        pid_t pid2;
        pid = vfork();

        if(pid > 0 ){
                while(1){

                        printf("this father print:%d\n",getpid());
                        sleep(1);
                        printf("cnt=%d\n",cnt);
                }
        }else if(pid == 0){
                while(1){
                        printf("this child print:%d\n",getpid());
                        sleep(1);
                        cnt++;
                        if(cnt == 3){

                                exit(0);
                        }
                }
        }

        return 0;

        Running results: Wait for the child process to exit (use normal exit) before the parent process is executed. The child process uses the parent process storage space to change the value of the parent process cnt.

this child print:8680
this child print:8680
this child print:8680
this father print:8679
cnt=3
this father print:8679
cnt=3
this father print:8679
cnt=3
this father print:8679

2. Purpose of process creation

A parent process wants to duplicate itself so that the parent and child processes execute different code segments at the same time. In the network server process, the parent process waits for the client connection request. When the request arrives, the parent process calls fork, allowing the child process to process the request, and the parent process continues to wait for the next request to arrive. In the early stage, all the code of the child process is copied to the parent process, and in the later stage, the write operation is copied.

 3. Process exit

1.Exit normally

        1.Main function calls return

        2. The process calls exit(), standard c library

        3. The process calls _exit() or _Exit(), which is a system call

        Replenish:

        1. The last thread of the process returns

        2. The last thread calls pthread_exit

2.Exit abnormally

        1. Call abort

        2. When the process receives certain signals, such as ctrl+C

        3. The last thread responds to the cancellation request

4. Wait for the child process to exit

1. Why wait for the child process to exit?

        When creating a child process for execution, you want to know how the child process is performing (whether the work is completed, and why it is not completed). At this time, the parent process needs to wait for the child process to exit and collect the exit status of the child process. If the work is completed, it will exit normally and the status code will be used to determine how the work was done. If the work is not completed, it will exit abnormally and the reason will be determined based on the exit status code. If the exit status of the child process is not collected, it will become a zombie process (zombie process).

The child process calls the exit function, and the parent process calls the wait function to collect the exit status, wait(&status), and then uses the following macro to parse the results.

 In addition to calling the wait function to collect the exit status, you can also use waitpid. The difference is that wait causes the user to block, and waitpid has an option to prevent the caller from blocking. The relevant function prototypes are as follows

status parameter: is an integer pointer
               , non-null: the exit status of the child process is placed in the address it points to.
                Empty: don’t care about exit status

Usually use pid>0

 Usually use WNOHANG, the suspend mode does not block

5. Orphan process

If the parent process does not wait for the child process to exit, it will end its "life" before the child process. At this time, the child process is called an orphan process.

Linux prevents the system from having too many orphan processes. The init process takes in the orphan processes and becomes the parent process of the orphan processes.

Guess you like

Origin blog.csdn.net/qq_44848795/article/details/122738855