Process basic concepts

basic concept

  • Program: A program is static, a file on disk.

  • Textbook knowledge: an execution instance of the program, the program being executed, etc. (dynamic).

  • Kernel point of view: Acts as the entity that allocates system resources (CPU time, memory).

  • Once a program is loaded into memory by the operating system, the program becomes a process.

  • Process information is placed in a data structure called a process control block (PCB), which can be understood as a collection of process attributes.

  • The structure that describes the process in Linux is called task_struct.

  • task_struct is a data structure of the Linux kernel that is loaded into RAM (memory) and contains process information

 At any given time when a process is executing, a process can be uniquely characterized by the following elements:

  • Identifier : A unique identifier associated with this process, used to distinguish other processes.

  • Status : If the process is executing, then the process is in the running state.

  • Priority : The priority relative to other processes.

  • Program Counter : The address of the next instruction in the program that is about to be executed.

  • Memory pointers : Including pointers to program code and process-related data, as well as pointers to memory blocks shared with other processes.

  • Context data : The data in the processor's registers at the time of program execution.

  • I/O status information : including displayed I/O requests, I/O devices allocated to the process, and a list of files used by the process.

  • Billing information : may include total processor time, total number of clocks used, time limits, account numbers, etc.

  Therefore, it can be said that a process consists of program code and related data as well as process control blocks. For a single-processor computer, at most one process is running at any time, and the state of the running process is the running state.

Process creation and termination

  • Process creation:
      When a new process is added to the set of processes that are being managed, the operating system needs to establish a data structure for managing the process and allocate address space to it in memory. These behaviors constitute a new process. Process creation process. There are usually 4 events that lead to the creation of a new process:

describe

  • Termination of the process:
      Any computer must provide a method for the process to indicate its completion. The batch processing job should contain a Halt instruction or a call to terminate the operating system display service to terminate. In the former case, the Halt instruction will generate an interrupt to alert the operating system that a process has completed. For interactive applications, the user's behavior will indicate when the process is complete. In some operating systems, a process can be terminated by the process that created it, or when the parent process terminates. Here are a few situations in which a process terminates:

describe

View progress

describe

  • PID: pid_t getpid(void); Get the PID of the process
    PID is a resource dynamically allocated after the program is loaded into the memory by the operating system and becomes a process.
    Each time the program is executed, the operating system is reloaded, and the PID is different each time it is loaded.

  • PPID: pid_t getppid(void); Get the PPID of the process
    PPID is the parent process number of the program.

  • Another new process created by a process is called a child process. Conversely, the process that creates the child process is called the parent process. For an ordinary user process, its parent process is the shell that executes it. For Linux, the shell is bash.

  • The ancestor of all processes is the init process.

Create a process:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int mian(){
    while(1){
        sleep(1);
    }
    return 0;
}

describe

Get process identifier via system call

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int mian(){
    pid_t pid = getpid();
    printf("pid:%d\n",pid);
    pid_t ppid = getppid();
    printf("ppid:%d\n",ppid);
    while(1){
        sleep(1);
    }
    return 0;
}

describe

ps axj | grep ./a.out 

describe

Create a process via a system call

fork features:

  1. A call has two return values, the parent process returns the child process PID, and the child process returns 0.

  2. Both the parent process and the child process continue execution from the position after the fork execution ended.

  3. The child process uses the parent process as a template (PCB, data and code). Copy-on-write .

  4. The order in which the parent and child processes are executed is uncertain and depends on the operating system scheduler.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int mian(){
    int ret = fork();
    printf("pid: %d, ppid: %d, ret: %d\n", getpid(), getppid(), ret);
    while(1){
        sleep(1);
    }
    return 0;
}

describe

Use if to shunt:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int mian(){
    int ret = fork();
    if(ret < 0){
        perror("fork");
        return 1;
    }
    else if(ret == 0){
        printf("I am child: pid: %d, ppid: %d, ret: %d\n", getpid(), getppid(), ret);
    }
    else{
        printf("I am father: pid: %d, ppid: %d, ret: %d\n", getpid(), getppid(), ret);
    }
    while(1){
        sleep(1);
    }
    return 0;
}

describe

process status

describe

  • R (ready) : Indicates that the process is either running or in the run queue

  • S (suspended) : The process is waiting for the event to complete (interruptible sleep)

  • D (deep sleep) : the process usually waits for the I/O to finish (uninterruptible sleep)

  • T (pause) : The process can be stopped by sending the SIGSTOP signal. The suspended process can continue running by sending the SIGCONT signal.

  • t (tracking)

  • X (dead) : just a return status.

  • z (zombie state) : its parent process has not called the wait function

describe

Stop process 9037:

describe

recover:

describe

zombie process

  • When the child process ends before the parent process, and the parent process does not recycle the child process and release the resources occupied by the child process, the child process will become a zombie process.

  • The zombie process will remain in the process table in a terminated state and will always wait for the parent process to read the exit status code.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main(){
    int ret = fork();
    if(ret < 0){
        perror("fork");
        return 1;
    }
    else if(ret > 0){
        printf("parent[%d] is sleeping...\n", getpid());
        sleep(30);
    }
    else{
        printf("child[%d] is begin z...\n",getpid());
        sleep(5);
        exit(EXIT_SUCCESS);
    }
    return 0;
}

describe

describe

Zombie processes will occupy system resources. If there are too many, it will seriously affect the performance of the server.
If it is not recycled, it may cause memory leaks.

orphan process

  • When a parent process exits while one or more of its child processes are still running, those child processes become orphaned. The orphan process will be adopted by the init process (process number is 1), and the init process will complete the state collection work for them.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int mian(){
    int ret = fork();
    if(ret < 0){
        perror("fork");
        return 1;
    }
    else if(ret == 0){
        printf("I am child: pid: %d, ppid: %d, ret: %d\n", getpid(), getppid(), ret);
        sleep(10);
    }
    else{
        printf("I am father: pid: %d, ppid: %d, ret: %d\n", getpid(), getppid(), ret);
        sleep(3);
        exit(0);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325575592&siteId=291194637