Linux-process creation and process termination

1. Process creation

1.1 First understanding of fork function

  • Fork function , create a new process from an existing process, the new process is the child process, and the original process is the parent process
    Insert picture description here
  • The process calls fork. When control is transferred to the fork code in the kernel, the kernel will do the following things:
  • 分配新的内存块和内核数据结构给子进程
  • 将父进程部分数据结构内容拷贝至子进程
  • 添加子进程到系统进程列表当中
  • fork返回,开始调度器调度

1.2 The fork function return value (why two return values?)

Insert picture description here

1.3 Copy-on-write (why code sharing and why data private)

  • By default, the parent and child processes share code, and each data is private
  • Code sharing : All codes are shared, generally executed after the fork() function, reason : the code cannot be modified, so each wastes private space
  • Data privacy : Reason : Because of the independence between processes, there are a lot of data, but not all data needs to be used immediately, and not all data needs to be copied. If you need to be independent immediately, you must copy all the data, and copy all the data that can be copied later, or even the data that does not need to be copied, but this is a waste of time and space.
  • The process of copying is not done right away. The code is shared between the father and the son. When the father and son do not write, the data is also shared.At this time, if either party tries to write, it will use a copy-on-write method for each copy, So we introduce aCopy-on-writethe concept of
    Insert picture description here

1.4 The general usage of fork and the reason for call failure

1.4.1 General usage

  • Assignment child process parent process 通过if、else来实现父子进程执行不同的代码段. For example, the parent process 等待客户端请求,生成子进程来进行处理
  • A process needs to execute a different program. such asfork后,子进程调用exec函数

1.4.2 Reasons for call failure

  • Too many processes in the system
  • The number of processes of the actual user exceeds the limit

1.5 How to understand child process creation and fork()?

  • The essence of child process creation is that there is one more process in the system, so the OS needs to manage the child process, and it needs to create a new PCB, virtual space, page table, mapping relationship (these attributes of their own processes are copied from the parent process as a template )
  • And when each of the parent and child processes wants to write data , a copy-on-write will occur to separate the data. For example, the data is 10M in total, but it is 1M when it is written, and copy-on-write occurs. It should be 1M data

2. Process termination

2.1 Process exit scenario

Insert picture description here
Insert picture description here

2.2 Why is the return value of the main() function usually 0?

  • Because the function design in C/C++ usually indicates 0 as correct. When the main() function ends, it returns 0, which means that the function has run normally and the result is correct. Non-zero indicates an error. Each exit code corresponds to one. Kind of error.

2.3 common exit methods of processes

2.3.1 Normal termination

  • Return from main function
  • Call the library function exit()
  • _exit (interface call)

2.3.2 Abnormal termination

  • ctrl+c, signal termination

2.4 The difference between exit, _exit and return

  • exit : Terminate the entire process, any call will terminate the process and refresh the buffer
    Insert picture description here
    Insert picture description here

  • _exit : belongs to a system call and will not refresh the buffer
    Insert picture description here
    Insert picture description here

  • return exit : return is a kindA more common way to exit the process. Executing return n is equivalent to executing exit(n), because the runtime function of calling main will use the return value of main as exita parameter

Guess you like

Origin blog.csdn.net/qq_45657288/article/details/114851733