编程知识 cdmana.com

Linux Process concept

Process concept :

process : The procedure in progress ( User perspective ), Running a program needs to describe the operation of a program , Through one task_struct Description of structure , Collectively referred to as PCB( Operating system perspective )
Program : A pile of code , Stored on hard disk , The program will be loaded into memory when it runs , Delete programs from the hard disk , It doesn't shut down the process
Operating system management process : Describe the process , PCB—struct task_struct{} The operating system passes through PCB To manage running programs , Its essence is a task_struct Structure
Description information :  Process identifier (PID) , Process status , priority , Program counter , Context data , Billing information Memory pointer , IO Status information, etc

View process information
cat /proc/self/limits View detailed resource constraints for the current process , Ordinary users modify the upper limit of resources , Up to the hard limit ,root Users can exceed the limit
/proc Process running information storage directory
ps -ef -aux -ajx View the process information on the system
getpid() Get the process that called the process ID
env View the environment variables of the current process
Kill process :
kill -9 pid Kill process
kill -9 -pid Kill the process group

Create process through system call :
  #include <unistd.h>
  pid_t fork(void);
   Call the process by copying , Create a subprocess
   The child process copies the parent's PCB( Parent child process data , The code looks the same )
   characteristic : Code sharing , Data unique
Process status :
   function R, Can interrupt sleep S, Don't interrupt sleep D, stop it T, Death X, dead Z
Zombie process :
After the child process has finished running , Not recycled by the parent process PCB, This situation leads to the leakage of system resources
The code that generates the zombie process :

int main()      
{      
  int pid = fork();      
  if(pid > 0)      
  {      
    while(1)      
    {      
      printf("i'm parent %d\n", getpid());    
      sleep(1);    
    }    
  }    
  if(pid == 0)    
  {      
      printf("i'm child %d\n", getpid());    
  }    
} 

Orphan process :
The parent process ends before the child process , A child process is called an orphan process , The parent of a child process becomes 1 Process of no. ,init process ,init The adoption process
Code that generates orphan processes

int main()      
{      
  int pid = fork();      
  if(pid > 0)      
  {      
    while(1)      
    {      
      printf("i'm parent %d\n", getpid());    
    }    
  }    
  if(pid == 0)    
  {      
    while(1)                                                                                                                       
    {    
      printf("i'm child %d\n", getpid());    
      sleep(1);    
    }    
  }    
} 

Daemon :
   Orphan processes that are disconnected from terminals and sessions
Process priority :
   priority : The hierarchy that determines the priority allocation of resources
Why have priorities :
   Make the operating system run more reasonable
   Interactive process : Once there's an operation , priority
   Batch process : Processing data all the time , But yes. cpu Not very demanding
   Set up : ps -efl
  PRI priority NI nice value : PRI You can't set it directly , But you can set NI value , adjustment PRI to
  PRI = PRI + NI
  renice -n size -p pid

版权声明
本文为[Ran1366]所创,转载请带上原文链接,感谢

Scroll to Top