Linux kernel-process & thread creation

1. Process and thread creation

At the end of the fork call, at the desired location of the return point, the parent process resumes execution, and the child process begins to execute. Fork returns from the kernel twice, once to the parent process, and once to the child process.
The process calls exit to exit. After exiting, it is set to a dead state until its parent process calls wait or waitpid.
Each process has its own address space. If the parent process wants to share the address space with the child process, you can call clone(). Setting the CLONE_VM flag, we call this kind of process
whether the thread shares the address space is almost the only difference between the thread and the process.
When the fork is called, the child process calls allocate_mm() to apply for the mm_struct structure space (that is, each process has a unique The mm_struct structure, which is the only process address space), and then use copy_mm() to copy the parent process memory descriptor; while the thread is created, call clone() and set the CLONE_VM flag, so that the kernel will not call allocate_mm To apply for mm_struct space, the function only needs to call copy_mm() to point the mm field to the memory descriptor of its parent process.
To sum up: the child process will apply for its own process address space and copy the content in the parent process address space; and the child thread will not apply for its own address space, but will share the process address space of its parent process

2. Copy-on-write technology

  1. Create pcb
  2. Copy the data of the pcb in the parent process (have the same virtual address space, the same page table)
  3. The parent and child processes map the same physical memory at the beginning
  4. When the physical memory is modified, the memory is reopened for the child process and the data is copied.

Example of copy-on-write technology :
Insert picture description here
At the beginning, the variables val on process 1 and process 2 described by PCB are both 100, which are the same position in the virtual address space of process 1 and process 2, so we are in the program The address of the variable val is the same, but it is actually a virtual address, a fake address. At this time, it is mapped to the same location in physical memory through the page table, but when process 1 changes the value of val, it is different.
Insert picture description here

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int okc=0;
int main()
{
    
    
    int ret=fork();
    if(ret<0){
    
    
        perror("fork");
        return 1;
    }
    else if(ret==0){
    
    
        okc=100;
        printf("child:%d %d %p\n",getpid(),okc,&okc);//子进程先退出,修改了okc的值
    }
    else
    {
    
    
        sleep(3);
        printf("father:%d %d %p\n",getpid(),okc,&okc);
    }
    return 0;
}

Insert picture description here
Result: It can be seen that the addresses of the variables are the same, but the values ​​are different, indicating that the addresses of the variables in the address spaces of the two processes are the same, but the addresses mapped to the physical addresses are different.

Guess you like

Origin blog.csdn.net/chengcheng1024/article/details/114408095