First, the experimental operation
Using GDB to track the process of creating a new process
First remove the menu folder and re-clone a new menu from GitHub, replacing test.c with TEST_FORK.C
After compiling, start menuos and find the command fork that you just added has been able to use the
Next to debug
Set Breakpoints: B sys_clone
b do_fork
b dup_task_struct
b copy_process
b copy_thread
b ret_from_fork
The fork displays the results as follows:
Second, study notes
1.
Fork code for a child process # include <stdio.h> #include <stdlib.h> #incldue <unistd.h>int main (int argc, char *argv []) {int pid;pid=fork ();//fork is a syscall,it's function is creat a son jinchengin usertypeif (pid<0) {fprintf (stderr, "F Ork failed! "); Exit (-1);} else if (pid==0) {printf ("This is a child process!\n);} else{printf (' This is Parent process!\n '), Wait (NULL);p rintf ("Child complete!\n");}} The parent process returns the child process ID, and the child process returns pid=0, so the latter two of the IF Else statements will execute
2.linux creates a new process by assigning a parent process, and it thinks of the framework as:
1. Copy a pcb--task_struct err=arch_dup_task_struct (Tsk,orig);
2. To assign a new kernel stack to the new process Ti=alloc_thread_info_node
tsk->stack=ti;
Setup+thread_stack (Tsk,orig);//This is just a copy thread_info, not a copy of the kernel stack
3. Modify the copied data
3. Three main functions of the operating system: Process management/Memory management/File system
4. Process State Transformation: The process has just been created with the state: Task_running, which is the ready state, but not running; When the scheduler selects this process, the state switches to the task_running state but is running; When a running process terminates, it enters task_ Zombie State, the system will dispose of this process, and when a process waits for a specific resource or event, it will enter the state of the blocking state task_interruptible or task_uninterruptible. At this point, if the event occurs or the resource is available, the process is woken up and placed on the run queue again into the ready state.
2017-2018-1 20179219 "Linux kernel Principles and design" Seventh week assignment