当前位置:网站首页>Basic knowledge of process (orphan, zombie process)

Basic knowledge of process (orphan, zombie process)

2022-07-07 11:06:00 Exy-

Catalog

The von Neumann system :

operating system :

Process concept :

Process status

Process creation

Zombie process

Orphan process

environment variable (path)

Program address space

Page replacement algorithm

Missing pages interruption


The von Neumann system :

Computer hardware architecture - Five hardware units Input , Output , Arithmetic unit , Memory , All devices of the controller work around memory ( What memory does in all devices is to buffer data

operating system :

Management software - Manage the software and hardware resources on the computer , Make the computer easier to use

management : describe + organization

system call : The operating system provides an interface for users to access system kernel resources

Library function : The encapsulation of the system call interface by the bosses for the functions of typical scenarios

The relationship between library functions and system call interfaces : The calling relationship between superior and subordinate .

Process concept :

From the user's point of view, a process is a running program , But from the perspective of the operating system , The process is pcb- Process control block , stay linux Next is task_struct Structure , Is the operating system's description of the program running , Complete the management of program operation through these descriptions .

Description information in the process : identifier -PID, Memory pointer , Program counter , Context data ,IO Information , Process status ,...

cpu Time sharing mechanism : The operating system makes every program run in cpu Only for a short period of time ( Time slice ), Switch operation .

Process status

linux Process state classification under :

  • R(running) Running state : It refers to the process that is running and can run after getting the time slice
  • S(sleeping) Interruptible sleep state , It means that the current block can be awakened by interruption
  • D(disk sleep) Non interruptible sleep state , It means that the current block cannot be awakened by interruption , Wait for yourself to wake up when the conditions are met
  • T(stopped) Stop state   The current process stops running , Do nothing
  • Z(zombie) Deadpan , A process in which resources are not completely released after the program exits

Here we focus on orphan process and zombie process

Process creation

pid_t fork(void), Returns the of the child process to the parent process pid; Return... To the child process 0; Failure to return -1

View process information instructions ps -ef  perhaps ps -aux, Usually with | grep  To use . 

Zombie process

Concept : Simply put, the child process exits before the parent process , The parent process does not recycle the child process , Release the resources of the child process , At this point, the sub process becomes a zombie process

harm : Memory leak , Take up a lot of resources

How to avoid ?

have access to wait To wait for

Orphan process

Concept : Contrary to the zombie process , It is because the parent process exits before the child process , Cause the child process to be 1 Number (init) Process takeover , At this time, the child process is an orphan process .

Daemon - Elvish process : It's a special orphan process , Running in the background , Break away from the terminal .( Use setsid();)

Create an orphan process

#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t id = fork();
if(id < 0){
perror("fork");
return 1;
}
else if(id == 0){//child
printf("I am child, pid : %d\n", getpid());
sleep(5);
}else{//parent
printf("I am parent, pid: %d\n", getpid());
sleep(10);
exit(0);
}
return 0;
}

environment variable (path)

The so-called environment variable is the variable that saves the running environment of the program

Related instructions :

env View all environment variables
set View all variables in the environment
echo Print the data of a specified variable
export Used to declare environment variables
unset Delete variables

Important environmental variables :

PATH :  The default search path of the system
HOME : Specify the user's home working directory ( That is, the user logs in to Linux Time in system , The default directory )
SHELL : At present Shell, Its value is usually /bin/bash

characteristic : It has transitivity between processes , Inherit , For example, the child process has the environment variables of the parent process

Each program will receive a table of environment variables , It's in main The third argument to the function , Is an array of string pointers

The benefits of environmental variables : The system running environment configuration is simpler and more flexible , You can pass parameter information to the process by setting environment variables .

Global variables : The configuration file needs to be modified , Variables are permanent

local variable : Use export Command statement is enough , Variable is closing shell Time lapse .

Program address space

The operating system virtualizes an independent and complete virtual address space for each process , Each process can access its own independent and continuous virtual address, but the actual data storage can be stored in different locations of physical memory after address mapping , This realizes the discrete storage of data in physical memory , Improved memory utilization

Program address space , Essentially, the operating system passes... For the process mm_struct Describe the virtual address space , Let each process access an independent and continuous virtual address , After mapping , Realize discrete storage in physical memory .

So why prohibit direct access to physical memory ?

The code data of the process are all continuous memory addresses , Direct access to physical space will cause a waste of memory .

Direct access to memory will lead to insecurity due to the lack of memory access control .

Typical way of memory management of operating system :

Paging management ; Segmented management ; Section management

Page replacement algorithm

One of the difficulties in the implementation of page virtual memory is the design of page replacement algorithm , When you call a new page into memory , If all the physical pages in memory have been allocated , It is necessary to discard a page according to a certain strategy , Release the physical page it occupies . For example, there is a first in first out Scheduling Algorithm (FIFO), The most recently unused scheduling algorithm (LRU). Best permutation algorithm (OPT).

Missing pages interruption

The page to be accessed by the operating system is not in main memory , You need to call it into main memory before accessing .
 

原网站

版权声明
本文为[Exy-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130620441205.html