2019-2020-1 20199305 "Linux kernel principle and Analysis" in the seventh week of work

Description and creating process

Description (a) process

(1) the operating system kernel to achieve the three major operating systems management capabilities (most core process management)

  • Process Management

  • Memory Management

  • File system

(2) with a data structure struct task_struct in Linux process described below is part of the data structure

struct task_struct { 
 volatile long state;        //进程状态
 void *stack;                // 指定进程内核堆栈
 pid_t pid;                  //进程标识符
 unsigned int rt_priority;   //实时优先级
 unsigned int policy;        //调度策略
 struct files_struct *files; //系统打开文件
 ...
}

(3) the state of the Linux kernel process management

When using the fork () system call to create a new process, the state of the new process TASK_RUNNING (ready state, but not running). When the scheduler selects the newly created process runs, the process is switched to the newly created state run, it is also TASK_RUNNING. In the Linux kernel, when a process is TASK_RUNNING state, it is run, that is ready state, depending on whether it is running there is no gain control of the CPU, which means that this process did not go to the actual execution in the CPU If the actual execution, and that process is state run state; if the kernel scheduler out in the waiting queue is ready state. For a running process, call the user-mode library function exit () into the kernel executes the kernel function do_exit (), that is to terminate the process, you'll go to TASK_ZOMBIE state that the termination status of the process. TASK_ZOMBIE state commonly known as zombies, Linux kernel at the appropriate time to dispose of the zombies, then dispose of the process descriptor is released, the process before disappearing from Linux. A process running while waiting for a specific event or resource enters the blocking state, blocking state, there are two: TASK_INTERRUPTIBLE and TASK_UNINTERRUPTIBLE, the former may be a signal and wake_up () wake, which can only be wake_up () wake.

Creating (b) process

Initialization (1) 0 Process

init_task for the first process (process number 0) of the process descriptor structure variable, which is initialized by listening to the fixed coding mode. In addition, all other initialization process is initiated by way do_fork copy of the parent process.

(2) create a user-mode process method

Create a specific process is probably the current process descriptor and other related resources copy process, resulting in a child process, and requires root process of copying process descriptor to do some changes, and then put the created child process the run queue (queue ready operating system in principle). When the scheduling process, the child process the newly created state of readiness have the opportunity to be scheduled for execution.

 #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 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");
        }
    }

The above code, else if (pid == 0) and else two pieces of code have been executed, the reason is because the fork system call to copy the current process of a child process, ie a process into two processes, two processes perform the same piece of code, but the return value may be different to the parent and child processes, the information output may be different, and did not break the structure of the parent process conditional branch if else, and the execution order of the parent-child process is not determined.

(C) test track and analyze the process of creating process

(1) First command MenuOS fork added, the results are compiled

(2) The results shown below the fork, can see the return of the parent and child processes

(3) Next, a breakpoint is set, as shown in FIG.

(4) to the next fork made only on the output of a stopped, continued to perform, you will see parked in the do_fork () position

(5) The next step execution, execution down to see the copy_process

(6) continue to enter dup_task_struct

(7) to perform copy_thread, we had to continue to trace the ret_from_fork, because it is the assembly code, may not be able to track

Guess you like

Origin www.cnblogs.com/20199305yizihan/p/11776045.html