Creation of Linux processes

Source: Internet
Author: User

1. Process Number:

Each process is initialized, the system assigns it a uniquely identified process ID, called the process number;

The process number is of type pid_t, and the current process number and the process number of the parent process of the current process can be obtained by getpid () and Getppid ().

2. Process replication:

The fork function is to copy a new child process from the parent process, including the copy code snippet, data segment, stack segment, etc., in addition to the code snippet, the child process will have its own physical memory space, where the content is the data segment of the parent process, the stack segment is the same, of course, the process number is not the same;

Under Linux, the Fork function is implemented with write replication. So-called write replication, only the virtual address space (data structure) of the parent process is copied, and the child process does not produce its own physical memory space until the parent process or child process has write behavior to a segment, and then creates independent physical memory control for the child process. The purpose of the write replication is to provide the efficiency of the creation process;

The fork function is called once in the parent process and returns two times. Depending on the return value, the decision is performed either in the parent process or in the child process. The parent process returns the process number of the child process, and the child process returns 0 if the failure returns-1;

3. System function:

Creates a new process to invoke the shell command, and blocks the current process until the shell command finishes executing;

Cannot perform return 127, successful return process status value, failed to return-1;

int ret = System ("ping www.baidu.com");

4. exec function Family:

The EXEC function creates a new process and replaces the current process's resources, so the new process number is still the same as the original process number;

The EXEC function does not return after execution, because the new process resource already occupies the resources of the current process, including code snippets, data segments, and stack segments, and only the process call fails to return-1;

The EXEC function is usually called after the fork in the new subprocess, so that the new process consumes the child process's resources to run;

The Linux system uses write-time replication, and if the EXEC function is called immediately after the fork, the resources of the parent process are not copied immediately, but the original process is overwritten with the exec parameters. A new physical memory space is created only if the original memory content has changed;

#include <stdio.h>#include<sys/types.h>#include<unistd.h>#include<stdlib.h>intMainintargcChar**Arg) {pid_t PID=Getpid (); pid_t Ppid=Getppid (); printf ("Linux Net:%d%d \ n", PID, ppid); pid_t Cpid=Fork (); intCount =1; if(Cpid >0) {printf ("Parent Progress:%d%d%d.%d \ n", Cpid, Getpid (), Getppid (), count); Count=111; }    Else if(Cpid = =0) {printf ("Child Progress:%d%d%d.%d \ n", Cpid, Getpid (), Getppid (), count); Count=222; intret = EXECVP ("./net-exe-test",0); printf ("EXEC ret%d \ n", ret); }    Else{printf ("create progress failed. \ n"); } printf ("---------------------1 PID%d count%d \ n", Getpid (), count);    GetChar (); return 0;}

Creation of Linux processes

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.