Description of the process
The three main management functions of the operating system: process management, memory management, file system
To manage the process, the kernel must have a clear description of each process, and the process descriptor provides the process information that the kernel needs to understand.
Process Control block PCB task_struct: Process status, Process-open files, process priority information
Task_struct abstraction of the overall data structure:
tty:控制台fs:文件系统files:文件描述符mm:内存管理signal:信号描述
Status of the process:
Note: Under Linux, both the ready state and the running state are task_running
One, gdb trace analysis a fork system call kernel processing function Sys_clone
Second, reading comprehension TASK_STRUCT data structure
Iii. Creating a new process
Copy a pcb--task_struct
Assigning a new kernel stack to a new process
Modify the copied process data, such as PID, process chain list
Iv. where does the new process begin?
Fork out the child process from the ret_from_fork
beginning to execute, and then jump to syscall_exit
, return from the system call.
Fork function to create a child process
Fork the code of a child process
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(int argc, char * argv[]){int pid;/* fork another process */pid = fork();if (pid < 0) { /* error occurred */ fprintf(stderr,"Fork Failed!"); exit(-1);} else if (pid == 0) //子进程pid == 0时返回,子进程是在内核中返回{ /* child process */ printf("This is Child Process!\n");} else //父进程可以返回{ /* parent process */ printf("This is Parent Process!\n"); /* parent will wait for the child to complete*/ wait(NULL); printf("Child Complete!\n");}}
Vi. Summary
All process creation in Linux is based on replication, and Linux creates a new process by replicating the parent process, by invoking Do_ Fork, and then modifying the necessary information to get the task_struck of the child process.
The new child process that fork creates is executed from the ret_from_fork
start, and then jumps to syscall_exit
, returned from the system call.
Linux Kernel Analysis Sixth week analysis of the Linux kernel creating a new process