Linux advanced Programming--06. Process overview

Source: Internet
Author: User

Process Control block

In Linux, each process has a Process Control block (PCB) in the kernel to maintain process-related information, which typically contains the following information:

    • The process ID. Each process in the system has a unique ID, represented in the C language by the pid_t type, which is actually a nonnegative integer.
    • The status of the process, there are running, hang, stop, zombies and other states.
    • Some CPU registers that need to be saved and restored during process switching.
    • Describes the virtual address space information.
    • Describes the control terminal information.
    • Current working directory (working directory).
    • Umask Mask.
    • A file descriptor chart that contains a number of pointers that point to file structures.
    • and signal-related information.
    • User ID and group ID.
    • Control terminal, session, and process groups.
    • The resource cap (Resource limit) that the process can use.
Fork and exec

The role of fork is to replicate a new process based on an existing process, which is called the parent process, and the new process is called the child process.

There are many processes running at the same time in the system, all of which are copied from the beginning of only one process. Entering a command under the shell can run a program because the shell process calls fork to copy a new shell process after reading the user's input command, and then the new shell process calls exec to execute the new program.

A program can be loaded into memory multiple times to become multiple simultaneous processes, such as multiple terminal windows can be run/bin/bash, on the other hand, a process before and after calling exec can also execute two different programs, such as the shell prompt to enter a command LS, First fork creates the child process, at which point the child process is still executing the/bin/bash program, and then the child process calls exec to execute the new program/bin/ls, as shown in.

Fork function

The fork function is declared as follows:

pid_t fork(void);

When the fork copy process fails, it returns-1, and when the replication process succeeds, the copied process executes only the code after the fork function. Although both the parent and child processes execute subsequent code, they can be distinguished by the return value of the fork, a simple example is as follows:

#include <sys/types.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>int main(void){    pid_t pid;    char *message;    int n;    pid = fork();    if (pid < 0) {        perror("fork failed");        exit(1);    }    if (pid == 0) {        message = "This is the child\n";        n = 6;    } else {        message = "This is the parent\n";        n = 3;    }    for(; n > 0; n--) {        printf(message);        sleep(1);    }    return 0;}

Fork returns the ID of the child process to the parent process, and the child process returns 0, so the parent or child process can be distinguished by the return value.

This design makes sense, although the child process cannot get the process ID by the return value, but it can get its own process ID through the GETPID function, or it can call the Getppid function to get the ID of the parent process. In the parent process, you can get your own process ID with getpid, but if you want the ID of the child process, you only have to record the return value of the fork, there is no other way.

After the process is differentiated based on the return value, you can specify their processes, as in the previous example, their processes are as follows:

exec function

When you create a child process with fork, it executes the same program as the parent process, and the child process can also execute another program through the EXEC function. When a process calls an EXEC function, the user space code and data for the process are completely replaced by the new program, starting with the start routine of the newly-executed program. Functionally similar to the C-like system function.

An example is as follows:

#include <unistd.h>#include <stdlib.h>int main(void){    execlp("ps", "ps", "-o", "pid,ppid,pgrp,session,tpgid,comm", NULL);    perror("exec ps");    exit(1);}


From for notes (Wiz)

Linux advanced Programming--06. Process overview

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.