Description of the process and creation of the process
I. Description of the process
(i) Process Control block Pcb--task_struct
1. The three major management functions of the operating system include:
(1) Process management
(2) Memory management
(3) File system
2, the PCB task_struct contains:
(1) Process status
(2) Process open file
(3) Process priority information
3. Distinguish each process by a unique process identity PID.
4. Process Control block Pcb--task_struct
5. Process Status
(1) After creating a new process the actual state is task_running, ready but not running, the scheduler chooses a task to enter the running state, also known as task_running.
(2) When the process is task_running, representing the process is operational, as far as it is not really running, depending on whether it has to gain control of the CPU, that is, there is no actual running on the CPU.
(3) An ongoing process calls Do_exit (), enters Task_zombie, and the process is terminated, "zombie process".
(4) Wait for a specific time or resources, enter the blocking state, if the condition is satisfied to enter the ready state, is selected to enter the operating state.
6. Understanding TASK_STRUCT Data structure
(1) Process indication PID
(2) In order to effectively search for a given type of process, the kernel maintains several process lists.
(3) All process linked list struct List_head tasks
7. CPU-related status
Ii. creation of the process
(i) Overview of the creation of the process and the user-state code of the fork one process
1 , fork The code for 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)
{
/* Child process */
printf ("This is a child process!\n");
}
Else
{
/* Parent Process */
printf ("This is the Parent process!\n");
/* Parent'll wait for the complete*/
Wait (NULL);
printf ("Child complete!\n");
}
}
(1) the fork () system call returns two times from the kernel: one time back to the parent process, and another to the newly generated child process.
(2) fork () is actually implemented by the clone () system call.
(ii) Understanding how the process creates complex code
1. The process of calling fork:
(iii) Creation of a new process in the kernel execution process
- Fork, Vfork, and clone three system calls can create a new process, and all are created by calling Do_fork to implement the process;
- Linux creates a new process by replicating the parent process, which gives us an idea of the framework that this process provides:
- copy a pcb--task_struct
- err = arch_dup_task _struct (Tsk, orig);
-
ti = alloc_thread_info_node (Tsk, node);
- tsk->stack = ti;
-
- *childregs = *current_pt_regs (); Copy the kernel stack
- Childregs->ax = 0; Why the fork of the subprocess returns 0, here is the reason!
- P->THREAD.SP = (unsigned long) childregs; Top of the kernel stack when dispatched to a child process
- P->thread.ip = (unsigned long) ret_from_fork; The address of the first instruction when dispatched to a child process
New Process Execution starting point: Return_from_fork, only part of the kernel stack is copied, int instruction and save_all to the kernel stack, parameters, system call number, etc. are stacked.
(iv) using GDB to track the process of creating a new process
1. Preparatory work:
RM MENU-RF
git clone http://github.com/mengning/menu.git//Update menu
CD Menu
MV Test_fork.c test.c//test.c cover off
Make Rootfs
2, for GDB debugging:
Qemu-kernel LINUX-3.18.6/ARCH/X86/BOOT/BZIMAGE-INITRD Rootfs.img-s-S
Gdb
File Linux-3.18.6/vmlinux
Target remote:1234
Set breakpoints
b sys_clone//Because the fork is actually a clone of the execution
b do_fork
b dup_task_struct
b copy_process
b copy_thread
b ret_from_fork
C
N
Tsk->stack = Ti; Assign the address of the kernel stack to it
STURCT Pt_regs *childregs = Task_pg_regs (p); Locate and initialize the space address of the stack of kernel stacks.
*childregs = *current_pt_regs ();
Childregs->ax = 0; Assigns a register of the current process's kernel stack's pressure to the child process.
P->thread.ip = (unsigned long) ret_from_fork; Set the IP that the child process is dispatched, that is, the start of the child process
JMP Syscall_exit; After that, we can't track it.
Iii. Summary
A process is a procedure that is in execution, a process that is a real-time result of an executing program code, a process that is in the process of execution, and a generic term for the associated resources.
The process begins to survive at the moment it is created. In a Linux system, this is usually the result of calling the fork () system. The fork () system call returns two times from the kernel: one time back to the parent process, and another to the newly generated child process. It is actually implemented by the clone () system call.
Linux Kernel Analysis--description of processes and creation of processes