Week Six: Description of the process and creation of the process

Source: Internet
Author: User

Lu Songhon + Original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

I. Description of the process 1. Three main functions of the operating system:
Process Management memory Management file system
2. Process Descriptor TASK_STRUCT Data structure
struct : In order to manage processes, the kernel must have a clear description of each process, and the process descriptor provides the process information that the kernel needs to understand. Status of the process: the state of the Linux process (ready state, run state, blocked state) the indicator PID of the process: used to mark the process
Status of the 3.Linux process

4. Process descriptor TASK_STRUCT Data structure:
structtask_struct {volatile LongState/*running state of the process -1 unrunnable, 0 runnable, >0 stopped*/void*stack;/*the kernel stack for the process is specified*/atomic_t usage;unsignedintFlags/*identifiers for each process*/intON_RQ;/*Run Queue*/pid_t pid;/*Process Identifiers*/struck List_head task; /*Process Chain List*//*Parent-Child processes*/structTask_struct __rcu *real_parent;/*Real Parent Process*/structTask_struct __rcu *parent;structList_head children;/*List of my children*/
Ii. creation of the process
There are 3 system calls to create a process, they go through the unified system call interface into the kernel mentality to run their own ways, each call their own kernel functions sys_fork, sys_clone, sys_vfork processing. These three functions end up calling the Do_fork function to create a child process, but using different parameters. Linux creates a new process by copying the parent process: Copy the parent process PCB-task_struct to create a new process to assign a new kernel stack to the new process. Modify the copied process data, such as PID, process chain list and so on to perform copy_process and Copy_thread. After that, the new process starts from ret_from_fork. 
#include <stdio.h>#include<stdlib.h>#include<unistd.h>intMainintargcChar*argv[]) {    intpid; /*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 the child process!\n"); }     Else     {         /*Parent Process*/printf ("This is the Parent process!\n"); /*parent would wait for the*/Wait (NULL); printf ("Child complete!\n"); }}
Iii. experimental content 1. Delete menu, download new code and execute.

2.GDB Commissioning

3. Set Breakpoints

4. Single-Step tracking

Iv. Summary
Linux creates a new process by copying the parent process: Copy the parent process pcb--task_struct to create a new process and assign a new kernel stack to the new process. Modify the copied process data, such as PID, process chain list and so on to perform copy_process and Copy_thread. Set the SP Scheduler to the child process when the kernel stack top, IP to the child process when the first instruction address, when the child process to gain control of the CPU to start running, ret _ form _ fork can be back stack out of the stack, from Iret back to the user state, thereby switching to the child process user space, Complete the creation of the new process. 

Week Six: Description of the process and creation of the process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.