Linux Kernel Analysis Sixth week analysis of the Linux kernel creating a new process

Source: Internet
Author: User

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

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.