Linux OS-basic of Process

Source: Internet
Author: User

A process can be said to be the most basic component of the OS, because a process hasPseudo-parallelPseudo-parallel refers to fast switching between multiple programs to achieve multi-task processing.Readiness, running, and blocking.

Running State-> blocking state:

 It may be that the process has an I/O request. For example, when a process waits for another process to provide input, or when it waits for information from another process...

Running State-> ready state:

This is the most common, such as clock interruption, such as process priority.

Ready state-> running state:

When the CPU usage of the currently running process is used up, the scheduler selects a running process from the ready state.

Blocking state-> ready state:

When an event that meets the congestion to the ready state occurs, it will be switched.

Pseudo-parallel

We can see three processes A, B, and C. When a encounters I/O read/write, the CPU is temporarily abandoned, and the scheduler selects a process (B) to run, this is a high-priority C, B temporarily suspended, let C run, after the AI/O read and write is successful, because a priority is greater than C, C continues to run, run C with a higher priority before B!

Process tree

A process is a dynamic execution entity of a program. As the execution of the program changes constantly, it has a life cycle. The process is like a thousand creatures and has its own parents, the process that generates it is called a parent process. Each process has only one parent process and can have multiple child processes. In Linux, you can use fork to create child processes. let's take a look at the processes in Linux. There is an INIT process in Linux. The PID is 1. It can be said that it is the ancestor process of all processes. If the parent process of a process is terminated, the process is still live, then the process becomes an orphan process (because there is no parent process), but its ancestor bless it and is responsible for raising it (its ancestor lives) to see this process tree!

Process Control Block

When creating a process, you need to create a PCB (Process Control Block) for it. When the process exits, it recovers the PCB. The PCB is the core of the process, which includes
Identifier, status, priority, PC, memory pointer, context, Io
The execution of a process can be interrupted because of this information.

In Linux, the process description structure is called task_struct (PCB)

struct task_struct {  long state;  long counter;  long priority;  long signal;  struct sigaction sigaction[32];  long blocked;  int exit_code;  unsigned long start_code,end_code,end_data,brk,start_stack;  long pid,father,pgrp,session,leader;  unsigned short uid,euid,suid;  unsigned short gid,egid,sgid;   long alarm;  long utime,stime,cutime,cstime,start_time;  unsigned short used_math;  /* file system info */  int tty;   unsigned short umask;  struct m_inode * pwd;  struct m_inode * root;  struct m_inode * executable;  unsigned long close_on_exec;  struct file * filp[NR_OPEN];  struct desc_struct ldt[3];  struct tss_struct tss;}

In addition, PCB is the data structure frequently read and written in the kernel, so it is resident in the memory.

Another key point is that the PCB is stored together with the kernel stack. This is because Linux puts the two together to save space and occupies 8 KB of memory.

union task_union {struct task_struct task;unsigned long kernel_stack[2408];};

This is an approximate data structure. PCB accounts for about 1 kb. Therefore, the size of the kernel stack cannot exceed 7 kb. Otherwise, the kernel will overwrite the PCB and cause a crash!

In addition to space saving, the two can be put together to make the kernel faster to find the PCB, and to prevent dynamic allocation of additional memory during process creation.

Process Switching

Process switching requires the OS to gain control. If you have experience with the underlying code, you will know that the cost of process switching is very high! Let's take a look at what causes process switching?

-> The first thought is the IO operation in the system interrupt.

-> The second thing that comes to mind is system call. Calling System functions also produces process switching. Therefore, the overhead of calling system functions is very high.

-> There are still some normal clock interruptions, that is, the process has exceeded the maximum specified time slice. If the process has exceeded, it must switch to the ready state. Call another process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.