Linux---Basic concept of process

Before understanding the process, we must first understand the basic principles of the operating system.

operating system

An operating system is a computer program that manages computer hardware and software resources, and is the core and cornerstone of a computer system. The simple understanding is that the operating system includes two parts: the kernel and other programs.

  • The kernel includes: process management, memory management, file management, etc.
  • Other programs: library functions, shell programs, etc.

Designing an operating system is to facilitate the interaction between users and hardware, and to help users manage software and hardware resources. So how to manage it?
For example: students, monitors, head teachers, students are managed by the monitor, and the monitor is under the management of the head teacher.
In general, the operating system manages software and hardware resources by describing them first, and then organizing and managing them.

  • It is described with struct structure.
  • Organization is through some data structures.
    Since the operating system kernel includes process management, the operating system also manages processes. First describe and then organize (organize multiple processes).

process

concept

A process is a running program, but the operating system describes the running program and realizes the running scheduling of the program through the description. This description information is the entity that the operating system schedules a program to run.

  • User perspective: a running program.
  • Operating system perspective: The description information of the running program is described by a task_struct structure, collectively called pcb (also called process control block).

Content contained in task_struct (content in pcb)

  • Process identifier
  • Memory pointer
  • Context data
  • Program counter
  • priority
  • Accounting information
  • I/O status information
  • other information

The way the operating system organizes the process: the task_struct structure is stored in the kernel in the form of a linked list.

Time-sharing mechanism of cpu (time slice rotation)
The processing time of cpu for each process is very short. After the processing is completed, the next process is switched, and the polling process is continuously performed.

View of the process

  • ps -ef / -aux
    Insert picture description here
    Insert picture description here
    ps -aux can view more information than ps -ef.

The process creates a
fork function, which creates a new process from an existing process. The new process is called a child process, and the original process is called a parent process.

#include <stdio.h>
#include <unistd.h>
int res = fork();

Fork return value:

  • Child process: == 0
  • Parent process:> 0 returns the pid of the child process, <0 failed to create the child process.

After fork creates a child process, usually use if to divert

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

int main()
{
    
    
        //创建一个子进程
        int ret = fork();
        if(ret<0)
        {
    
    
                perror("创建失败“);
                return -1;
        }
        else if(ret == 0)
        {
    
    
                printf("子进程 进程ID = %d ret = %d",getpid(),ret);
        }
        else
        {
    
    
                printf("父进程 进程ID = %d ret = %d",getpid(),ret);
        }
        return  0;
}

Use fork to create a child process, the new process (child process) will copy the parent process (code sharing, data unique), the child process created with fork, just copy the data behind the fork function, the previous will not be copied.

Process state The state of the
process includes: ready state, running state, and blocking state.
The status of the process under Linux:

  • R running state does not mean that the process must be running, it is possible that the process is in the running queue.
  • S dormant state means that the process is waiting for the completion of a certain event and can be interrupted.
  • D Disk sleep state: it cannot be interrupted. In this state, the process generally waits for the end of the IO operation.
  • T stop state
  • X state of death

Method of viewing process status: ps -aux/-axj
Insert picture description here

Orphan process

Cause: The parent process exits early, the child process is called an "orphan process", the child process becomes a background process, and the child process is adopted by the process number 1, and its parent process becomes process number 1.

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

int main()
{
    
    
        //创建一个子进程
        int ret = fork();
        if(ret<0)
        {
    
    
                perror("创建失败“);
                return -1;
        }
        else if(ret == 0)
        {
    
    
                printf("子进程 进程ID = %d",getpid());
                sleep(10);
        }
        else
        {
    
    
                printf("父进程 进程ID = %d",getpid());
                sleep(5);
                exit(0); //父进程退出
        }
        return  0;
}

Zombie process

After the child process exits, the resource is not released and is in a dead state.
Cause: The child process exits prior to the parent process. The parent process is performing other operations and does not pay attention to the child process exit. At this time, the operating system will not release the resources in the child process in order to protect the child process exit, and the child process is neither running , Did not exit, and became a zombie process in a dead state.

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

int main()
{
    
    
        //创建一个子进程
        int ret = fork();
        if(ret<0)
        {
    
    
                perror("创建失败“);
                return -1;
        }
        else if(ret == 0)
        {
    
    
                printf("子进程 进程ID = %d",getpid());
                exit(0); //子进程退出
        }
        else
        {
    
    
                printf("父进程 进程ID = %d",getpid());
                sleep(10);
        }
        return  0;
}

Insert picture description here
The harm of zombie processes

  • If the parent process has not paid attention to the exit of the child process, the child process will always be a zombie process.
  • Because the child process did not exit, the pcb has not been released.
  • There is a memory leak.

How to avoid zombie processes

  • Close the parent process. When the parent process is closed, the child process will not need to save its exit information.
  • Set the process to wait, wait for the child process to exit, and notify the parent process.

Environment variable

In the operating system, parameters used to specify the operating environment of the operating system.

Common environment variables

  • HOME: Home directory.
  • PATH: Specify the search path of the command.
  • SHELL

View environment variables

  • echo $name, through the variable name, view the specified variable.
  • env: View all environment variables
  • export: declare an environment
  • unset: delete a temporary variable

Insert picture description here
Every program has an environment table. The environment table is similar to a pointer to an array in which environment parameters are stored.

How to view environment variables:

#include<stdio.h>

int main(int agv,char *argv[])
{
    
    
        extern char **environ;
        int i = 0;
        for(;environ[i];i++)
        {
    
    
                printf("%d -- %s\n",i,environ[i]);
        }
        return 0;
}

Insert picture description here

#include <stdio.h>
#include <stdlib.h>
int main()
{
    
    
	char * env = getenv("MYENV"); //getenv访问特定环境变量的接口
	if(env){
    
    
		printf("%s\n", env);
	}
	return 0;
}

Since the child process will copy the PCB of the parent process, the environment variables of the parent process can be inherited by the child process.

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/104349058