Linux:fork system Call Implementation profiling

Source: Internet
Author: User

First look at the concepts of procedures and processes:

Program: A program is a collection of instructions to complete a particular task.

Process: From the user's point of view, a process is a process of executing a program. From a system point of view, a process is the unit of the operating system allocating resources such as memory and CPU, and the process is the smallest unit of resource allocation. Each process has its own independent address space and execution state,multitasking operating systems like UNIX enable many programs to run at the same time, and each running program forms a process. Process data structure: A static description of the process: the set of data structures that are manipulated by the PCB, the program segment, and the program segment. Process Control Block: Describes the process situation and control the operation of all the information code snippet: is the process can be executed by the process scheduler on the CPU of the code snippet data segment: A process of data segment, can be the process corresponding to the processing process of the original data, can also be generated after the execution of the program          or final data processes and programs: The process is dynamic and the program is static.          The life cycle of a process is relatively ephemeral, and the program is permanent.          Process data structure PCB.      A process can only correspond to one program, and one program may correspond to multiple processes. Status of the process:ready, run, wait (block)process Status of the Linux kernel:running, interruptible sleep, non-disruptive sleep, pausing, zombies, and their graphs such as:

Process scheduling and its algorithms:First come first service algorithm, time slice rotation method, etc.Process No. 0 is the life of Linux and is the bully process when the kernel starts. After the first No. 0 process is created, the first user process is the 1th process, which init/sbin view kernel process PID Maximum configuration: Cat/proc/sys/kernel/pid_max 32,768 below to enter the topic: Fork System Call    The phenomenon of the fork function is to open a new branch of the process, the concurrency situation occurs.     How do I return a call two times when I call the function? Essentially, processes are returned in their own memory space, because each process has its own memory space.     What data resources does the child process copy the parent process? Code snippets, stacks, data segments, Process Control blocks (records the state of a process, CPU queries the state of a process through a Process control block)     a process is made up of process PCBs, code snippets, data segments, stack segments. Process is an abstraction of the resources of the operating system, PCB is an important data structure of the operating system aware process, the implication is that the club CPU through the PCB to control the process.     If there is a file lock in the data segment and code snippet at this point, will the child process copy?     If a file is opened in the parent process, will the file handle be copied by the quilt process?     If there is a variable in the parent process, is the variable in the child process the same variable as the variable in the parent process? No, each process has a separate memory space. The return value of the    fork function is equal to 0 is the child process, greater than 0 is the parent process, why do you do it? The return value is the ID number of the child process so that the parent process can easily control the child process. A parent process can continue to fork a child process, the parent process and the child process are one-to-many relationships, and if a child process is created that does not process information back to the parent process, then the parent process cannot control multiple child processes. It is also convenient to get the parent process in the child process. What if the fork process goes wrong? Man 2 fork, if the failure returns-1, a variable errno is created to log the error message.     How do you understand that the application code for a subprocess starts with fork instead of declaring the pid_t variable?     pid_t pid;        INT abc= 6;//parent process here or change the data segment assignment     PID = fork ();//Copy the data to the child process, there is no need to let the child process re-assignment     if ( Pid==-1) {       //Sub-process start from here    } The following and Helloword will print several times? 8 times int Main () {    Fork ()//  produces two process branches     Fork ()///Two processes generated in front of each will produce two processes, four processes     fork (),//4 processes generated in the preceding process will produce two processes, a total of eight processes     print (" Helloworld! "); /Eight processes so print eight times     return 0;}
To have the parent process generate 10 processes, enter how many processes to generate, and how many laps each process will run? void TestFunction () {print ("------------------");}        int main () {int procnum = 10;        int loopnum = 100;        int i = 0; pid_t pid;            for (i=0;i<10;i++) {pid = fork (); if (PID) {for (J =0;j<loopnum; j + +){ Testfunciton ();} exit (0); }        }
}
This article source: Http://blog.csdn.net/andywuchuanlong

Linux:fork system Call Implementation profiling

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.