Operating System Theory: Linux Process and Process State (Big O(1) Algorithm Data Structure Model for Process Scheduling)

insert image description here

1. The basic concept of process

  • When the Von Neumann system computer is running, many programs (data + operation instruction set) will be preloaded in the memory, but the CPUOnly one program can be executed at a time(Multiple programs compete for CPU resources), at this time the operating system is requiredManage many programs in memory, so that CPU resources are allocated reasonably, so there is the concept of process:

    • process: a structure object describing the program (PCB structure)andthe program it points to(data and operation instruction set)
    • The PCB structure is named task_struct in Linux
  • The PCB structure of the process will be organized into various data structures in the operating system, and the same PCB structure object will bein multiple data structures at the same time,for example:insert image description here

  • operating systemManage the processis throughData structure formed for PCB structure objectconductCRUDAchieved

  • The PCB of a process in Linux is uniquely identified by the PID (a number):insert image description here

The basic relationship between processes: parent-child relationship

  • In the Linux system, a process can fork()create child processes through library functions
    • pid_t fork(void);
    • After a process creates a child process through the fork function, the parent and child processes shareThe code statement where the fork() function is locatedand the following code snippet
    • Description of the return value of the fork function: ifThe child process was successfully created, the frok function returns the PID of the child process in the parent process, and returns 0 in the child process, ifChild process creation failed, the fork function returns -1 in the parent process
    • When the child process is just created, it willShare all data of the parent process, the subsequent child process will process the data of the parent processcopy-on-write
    • according toThe difference in the return value of fork() in the parent and child processes, we can make the parent-child process follow-upExecute different code segments
int main()
{
    
    
  printf("hello world\n");
  size_t childPid = fork();//创建子进程
  if(childPid == 0)
  {
    
    
    //子进程执行的代码段
    while(1)
    {
    
    
       printf("我是子进程,Pid:%d,PPid:%d\n",getpid(),getppid());
       sleep(1);
    }
  }
  else
  {
    
    
    //父进程执行的代码段
    while(1)
    {
    
    
       printf("我是父进程,Pid:%d,PPid:%d\n",getpid(),getppid());
       sleep(1);
    }
  }

  return 0;
}

insert image description here

  • Operating status:insert image description here
    insert image description here
  • a parent processYou may havemultiple child processes,anda child processonly the onlyparent process, in the operating system, according to the relationship between the parent and child processes, the PCB structure object will form a tree data structure
  • In short, in the operating system, the PCB structure object is in a network of many data structures

2. Process status

  • Process status overview (Linux operating system):insert image description here

(1) The running state of the process R

  • CPUExecute only one process at a time,a CPUcorrespondonly run queue(in LinuxEssentially a hash bucket),running processThe PCB structure will be linked into the run queue, and the operating system willsequential schedulingRun the program pointed to by the PCB structure in the queue, and hand it over to the CPU for calculation.
  • Process PCB will record the process'sscheduling priority(an integer), the scheduling priority of the process will affect itsrun queueposition in .
  • The process priority in Linux is divided into140 levels, where grades 0 to 99 are assigned toreal-time process, grades 100 to 139 are given tonon-real-time process
    • 140 levels correspond to 140 itemsrun queue branch(140 run queue branches are combined in a hash bucket structure)
    • process task_structinrun queueThe position in thedynamic prioritizationinsert image description here

Big O(1) algorithm data structure model of Linux process scheduling (run queue hash bucket):

insert image description here

  • In the Linux kernel there is also a160 bit-sized bitmapfor recordingrun hash bucketWhether each queue in the hash bucket is empty, once the queues in the run hash bucket are all empty, exchangepointers to two arrays of run and tem, and then continue to execute process scheduling
  • Thanks to the data structure shown in the figure above, the Linux operating system does not need task_structtoQuick Sort by Priority, and at any time with O(1) time complexityQuickly locate the branch running queue where a process of a certain priority is located (while performing O(1) queue emptying), the task_struct structure objectEnqueue and dequeue operationsIt is also the time complexity of O(1), which realizes the efficient process scheduling mechanism of the Linux operating system

time slice of the process

  • Each process has a running time slice (such as 20ns), and the time slice of the process determines the CPU's execution time on it.single maximum time, a process completes in the CPUAfter a time slice operationwillTemporarily exit the run queuewait for next dispatch
  • With the limitation of time slice, within a certain period of time (such as 20ms), allin the run queueThe process will be executed by the CPU once, formingconcurrent execution of processes, the CPU resource thus getsreasonable distributionandfully use
  • We feel that the computer is running multiple programs at the same time, but in fact, the CPU is runningAccording to process time sliceIt is formed by traversing and executing each process at a very high speedprocess concurrencythe result of.

(2) The sleep state of the process (S and D)

  • In Linux systemsleep state of the processcorrespondingOperating System Discipline Theoryprocess inblocked state
  • CPU operation speedanddata flow speedare not equal, so the process of data interaction is often in thewaiting for feedbackstatus
  • When a process is inwaiting for a resource, the PCB structure will beexit runqueueandinto the blocking queueIn, for example, a process needs keyboard input data, then it will enter the blocking queue of the keyboard
  • In the operating system, the number of blocking queues is uncertain. There are as many kinds of blocking queues as there are communication processes in the system:insert image description here
  • Linux is located atblocking queueThe process ininterruptible sleep state(S state), when the system memory is insufficient, the operating system will temporarily store some programs (data and instruction sets) corresponding to the processes in the blocking queue in the swap disk partition, and the process enterssuspended state
  • When the Linux operating system runs out of memory, it will also selectively kill someProcesses in a blocking queue, if a process performs a very important task (such as waiting for disk feedback when writing important data to the disk), then the process needs to be marked asUninterruptible sleep state (D state), atuninterruptible sleep statethe process ofBefore being executedWill not respond to operating system requests

(3) Zombie state and death state of the process

  • In a Linux system, when a child process of a parent process terminates, theThe child process will enter the zombie state (Z state),Z state processWill wait for its parent process to recycle itsTermination information in the PCB structure(In order to determine whether it terminates normally or abnormally), only when the parent process handles its termination information, the child process willFrom Z state to death state (X state), at this time, the operating system will use the memory resources occupied by the child processfully recycled
  • ifparent processhas not been dealt withzombie child processThe termination information of , thenzombie child processofPCB structure and related datawill remain in memory forever, causing a memory leak
  • If the parent-child processThe parent process terminates firstThe child process will be entrusted to the operating system and become a child process of the operating system. Such a process is called an orphan process
    insert image description here

Guess you like

Origin blog.csdn.net/weixin_73470348/article/details/132001886