Linux system application programming (2) process

Linux system application programming (2) process

1. Creation of process

1. Understanding of fork()
  • The fork() function is a system call function under the Linux/Unix operating system, which is used to create a new process, which is a child process of the process that calls fork().

  • Features: The child process copies the contents of the parent process, including code segments, data segments, heap and stack data. The parent and child processes run in separate memory spaces. Although the child process copies most of the contents of the parent process, due to the running The address spaces are independent and will not affect each other when performing operations such as file writing and mapping.(man手册所述)

  • In addition, the man manual also introduces some differences and characteristics of parent and child processes:

    ①The child process has its own unique process ID (PID is unique);

    ②The process resource utilization and CPU time counters will be reset to 0 in the child process;

    ③The child process will not inherit the locks and semaphores of the parent process;

    ④The child process will not inherit unfinished asynchronous I/O operations;

    ⑤The termination signal of the child process is always SIGCHLD;

    ⑥The parent and child processes share the same file descriptor (that is, they share the I/O attributes of a file, including file pointer offset, status flags, etc.);

    ⑦Father and child processes share the same message queue.

    (更多详细可自行翻译man手册,这里就记个几点够面试掰扯掰扯就行)

2.Usage of fork()

▲Return value: fork() will return twice , in the parent process and the child process respectively.

  • In the parent process, fork() returns the pid of the child process (a positive integer, the parent process can operate the child process through the pid of the child process, such as: sending the signal kill(), waiting for the child process to end wait(), etc.)

  • In the child process, the return value of fork() is 0 (only to distinguish between parent and child processes, it does not mean that the child process pid=0)

    返回-1表示创建进程失败

3. Understanding of vfork()
  • Features: In the vfork() function, the child process runs in the address space of the parent process. Unlike the fork() function, vfork() does not copy the contents of the parent process. Therefore, vfork() is also more efficient than fork() in terms of operating efficiency, but it is more complicated to use than the fork() function. You need to pay attention to the following:

    ①In the vfork() function, the parent and child processes share the address space, and the child process can modify the variables and function calls of the parent process, which may cause problems;

    ②In the vfork() function, the child process cannot use the stack space of the parent process, and new stack space needs to be reallocated in the child process;

    ③In the vfork() function, the child process runs first, and you need to call the exec function family or the exit() function to end the child process before the parent process can be executed;

    ④In the vfork() function, the child process does not inherit the file descriptor of the parent process, so if the child process needs to use the same file, the file needs to be opened in the child process again.

2. Exit of process

1. How the process exits
  • return in main function
  • The process calls the standard C library function exit()
  • The process calls the system call function _exit( ) or _Exit( )
  • The last thread in the process returns or calls pthread_exit()简单说就是进程的最后一个线程结束
  • Abnormal exit: calling abort(), receiving exit or termination signal, etc.
2. Zombie processes and orphan processes
  • Zombie process: The child process ends before the parent process, and the exit status of the child process is not collected by the parent process. At this time, the child process will become a zombie process.

  • Orphan process: The parent process ends first, and the child process is still running. At this time, the child process will become an orphan process (the orphan process will be adopted by the init process)

3.wait(), waitpid() functions

Insert image description here

3. exec family functions

1.Function

​ Execute another executable program (binary file, shell script, etc.) in the process. After execution, except for the PID, the original process is replaced by the executable program process. Among them, execlp() will search for executable programs from system environment variables.
(注意:exec函数并不创建新进程,且调用其他程序结束,原程序不再执行)

2.Exec family function prototype
#include <unistd.h>
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
3. Parameters and return values

Insert image description here

4. system() function

Insert image description here

5. popen() function

Insert image description here

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    
    
    FILE *file = NULL;
    file = popen("ls -l /","r");
    if(file == NULL){
    
    
        perror("popen");
        exit(-1);
    }
    char *readBuffer = (char *)malloc(128);
    fread(readBuffer,128,1,file);
    printf("read:%s\n",readBuffer);
    pclose(file);

    return 0;
}

Insert image description here

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_54429787/article/details/129927378