Process analysis under the LINUX2.6.32

Source: Internet
Author: User

Objective:

What is a process?

Process is a computer program on a data set on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure. In the early process design-oriented computer architecture, the process is the basic execution entity of the program, and in the contemporary thread-oriented computer architecture, the process is the container of the thread. A program is a description of the instruction, the data and its organization, and the process is the entity of the program

Narrowly defined: A process is a running instance of a program (an instance of a computer programs, which is being executed).

Generalized definition: A process is a running activity of a program with certain independent functions about a data set. It is the basic unit of the operating system dynamic execution, in the traditional operating system, the process is not only the basic allocation unit, but also the basic execution unit.    Process analysis under Linux:

--1. Organizational Processes-

Linux typically uses the fork () function to create a process;

Fork function
1 #include <unistd.h>2 pid_t fork (void);

Function: The child process replicates the parent process.

Features: Call once, return two times.

A fork Example:

1 #include <sys/types.h> 2 #include <unistd.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5  6 int main (void{8     pid_t pid;//Process ID 9     char *message;10     int n;11     pid = fork (); 12     if (PID < 0) {    //create process failed         perror ("fork failed"); exit (1 }16 if (pid = = 0) {//Sub-process + message = "This is the child\n"; n = 6; +} else {//parent process + Message = "This is the parent\n"; n = 3 }23 24 for (; n > 0; n-- printf (message); Sleep (1 }28 return 0;  >

Some of the process-related functions:

Getpid/getppid

1 #include <sys/types.h>2 #include <unistd.h>3 4 pid_t getpid (void);    Returns the PID number of the calling process 5 pid_t getppid (void);   Returns the PID number of the calling process parent process

Getuid/geteuid

1 #include <unistd.h>2 #include <sys/types.h>3 4 uid_t getuid (void); Returns the actual user ID5 uid_t geteuid (void); Returns a valid user ID

Getgid/getegid

1 #include <unistd.h>2 #include <sys/types.h>3 4 gid_t getgid (void); Returns the actual user group ID5 gid_t getegid (void); Returns the valid user group ID

  

Termination of the process

The main functions involved are _exit (), exit (), Atexit (), On_exit ().

1. The termination of the process is divided into normal and abnormal two types. An abnormal termination may be caused by certain signals, some of which can also cause a process to produce a core dump file.

2. Normal termination can be achieved through system call _exit () or the GNU C standard library function exit (). Both have a status parameter that represents the function exit value. The usual return function is similar to the exit () function (in this case, the return value when the parameter is returned is dependent on the version standard of the C language and the compiler used).

3. The kernel performs several cleanup steps, regardless of whether the process is normal or abnormally terminated. Unlike the system call _exit (), calling the Library function exit () terminates a process normally, it will throw an exit handler registered through the GNU C library function atexit () or On_exit () (These exit handlers are called in the function _exit () Or it will not be executed due to a signal termination. ), and then flush the stdio buffer.

--2 Process state transitions--

The process state reflects changes in the process execution process. These states are converted as the process executes and the external conditions change.

The entire life period of a process from creation to revocation, sometimes owning the processor, sometimes running but not the processor, and sometimes having an idle processor but unable to execute because it waits for an event to occur, all of which indicate that the process and program are not the same, that it is active and has a state change, This can be portrayed in a set of states.

Process state transition Diagram:

(1) (Running state): The process is executable, or is executing, or waiting to be executed in the run queue.

(2) (Can interrupt sleep state): The process is blocked, waiting for certain conditions to complete. Once these conditions are fulfilled, the kernel sets the state of the process to run.

(3) (non-interruptible sleep state): The process is blocked, waiting for certain conditions to complete. Unlike an interruptible sleep state, the state process cannot be awakened by a signal.

(4) (Dead State): The process has ended, but its parent process has not yet recycled it.

(5) (terminating state): The process stops executing. Usually the process enters this state when it receives signals such as Sigstop, Sigttin, Sigttou, and so on.
 

Conditions for state switching:

Ready----> Execute Schedule

Execute-----> Ready time slices to

Perform------> Wait for an event to occur and sleep

Waiting for-------> Ready to wake up waiting for something to happen

--Process scheduling--

Some methods of process scheduling:

1. First come first to serve:

Concept:

If the well-prepared process is in front of the ready queue, and the late-ready process is placed behind the ready queue, the first-come-first service (the Fcfs:first come-a) always dispatches the process that is currently at the top of the ready queue to the running state. It also says that it only considers the order in which the process enters the ready queue, regardless of the length of its next CPU cycle and other factors.

Essentials:

Schedule and assign processor execution in the order in which the process enters the ready queue. First come first service scheduling algorithm is a non-preemptive algorithm, first into the ready queue process, the first allocation processor operation. Once a process takes over the processor, it runs until the process completes its work or is not able to continue running because it waits for an event to occur.

(1) If the system has a fallback job queue or a ready process queue established by FIFO rules, it is a job control block JCB or the Process control block PCB joins the queue at the end of the corresponding queue.

(2) scheduled to exit the queue from the beginning of the corresponding queue scan, the relevant JCB or PCB scheduling to move out of the corresponding queue.

Pros: Useful for long jobs and CPU busy jobs

Cons: not conducive to short jobs and I/O busy jobs

2. Short job (process) Priority scheduling algorithm SJ (P) F

Concept: Job (process) prioritization for short execution times. Usually later short jobs do not preempt the job being executed.

Advantages:

Improvement of average turnaround time and average time to cycle, shorten the waiting time of the job than FCFS;

Improve the throughput of the system;

Disadvantages:

Very unfavorable to long work, may not be executed for a long time;

Failure to prioritize execution based on the urgency of the job;

It is difficult to accurately estimate the execution time of a job (process), which can affect scheduling performance.

3. Rotation method

Concept: Allow each process to wait in the ready queue in proportion to the time it takes to enjoy the service.

Defined:

The time slice rotation method is similar to "turns the Republicans" the thought, the condition is: each job approximate thought "simultaneously" arrives, the question condition is the back work sequentially is late one time unit, the analysis should be strictly according to the RR scheduling algorithm realization thought: The system puts all ready processes in the first-in first-out principle into a queue. The new process is added to the end of the ready queue. Whenever a process scheduler is executed, the process scheduler always selects the ready queue's first process, allowing it to run a time slice on the CPU. When the process runs out of time slices to it, the scheduler stops the process and puts it at the end of the ready queue.

4. Multilevel Feedback Queue algorithm

Concept:

Set up multiple ready queues, assigning different priorities, such as decreasing progressively, with queue 1 having the highest priority. The length of the execution time slice of each queue is also different, the lower the priority level, the longer the time slice, such as doubling.

After the new process enters the memory, first puts into the queue 1 the end, presses the FCFS algorithm to dispatch, if by the queue 11 time slices failed to execute, then reduces the input to the end of the queue 2, similarly presses the FCFS algorithm to dispatch; So down to the last queue, the "time slice rotation" algorithm is dispatched until the completion.

Processes in lower-priority queues are dispatched only if the higher-priority queue is empty. If a process executes when a new process enters a higher-priority queue, the new process is preempted and the preempted process is put to the end of the original queue.

Multilevel feedback queue scheduling algorithm, also known as feedback loop queue or multi-queue strategy, the main idea is to divide the ready process into two or more stages, the system establishes two or more ready process queues, the higher priority queue is generally allocated to a shorter time slice. Processor scheduling selects the processor-owning process from the advanced-ready process queue first, and is selected from the lower-level ready process queue only if it is not available.

Advantages:

Take care of short processes to improve system throughput and shorten average turnaround time.

Take care of I/O processes for better I/O device utilization and reduced response time.

No need to estimate process execution time, dynamic tuning

Factors affecting process scheduling under 1.LINUX

Linux scheduling is based on the time-sharing technology, to each of the operational processes to allocate a slice, when the process runs the end or the time slice expires, the process will switch. In the Linux scheduling algorithm, each process switch, kernel Scan can run the process chain list, calculate the priority of the process, sometimes with a complex algorithm to find the current priority of the process, select the "most suitable" process to run (when not described SMP, the default is a single processor system), that each process has a value associated with it, This value is used to indicate how the process is allocated appropriately to the CPU, which is the priority of the process.

Example: The CFS algorithm for Linux process scheduling2.1 Process Weights

In the complete fair scheduling algorithm, the most important is the weight determined by the priority, as well as the CPU time generated by the different weights .

The weights for the different priorities are as follows:

Static Constint prio_to_weight[40] = {

/*-20 */88761, 71755, 56483, 46273, 36291,

/*-15 */29154, 23254, 18705, 14949, 11916,

/*-10 */9548, 7620, 6100, 4904, 3906,

/* -5*/3121, 2501, 1991, 1586, 1277,

/* 0*/1024, 820, 655, 526, 423,

/* 5*/335, 272, 215, 172, 137,

/* 10*/110, 87, 70, 56, 45,

/* 15*/36, 29, 23, 18, 15,

};

The corresponding priority is from 100-139 (also can be said to be from Nice value -20-19), its weight datum is 0 corresponds to 1024, from 0 to 1, weight reduction 10%, from 0 to - 1, that is, priority increases one level, weight increases CPU time by 10%.

The ready process has a weighted sum, when a process is started or added to the queue, the system will be based on the weight of each process to calculate the total weight, left behind to use, conversely, when there is a process is not ready to lose its weight from the sum, can refer to the function inc_nr_running () and Dec_nr_running ()

2.2 Clock Statistics

1 static void __sched_fork (struct task_struct*P) 2 3{4 5 P->se.exec_start = 0;//process Start run time 6 7 P->se.sum_exec_runtime = 0;//this time total Run time 8 9 P->se.prev_sum_exec_runtime = 0;//Last total run time, which is assigned by the current run time when dispatching to another process, that is, se->prev_sum_exec_runtime = se->sum_exec_runtime;10 11}12 13The Task_tick_fair () function is periodically executed in the clock interrupt function, in which some time variables are counted. The static voidupdate_curr (struct CFS_RQ *CFS_RQ) 16 17{sched_entity *curr =cfs_rq->curr;20 U64 now = rq_of (CFS_RQ)->clock; Get current time unsigned longDelta_exec;24 25...... Delta_exec = (unsigned long) (Now-curr->exec_start); Process Execution Time 28 29__update_curr (Cfs_rq, Curr, delta_exec); curr->exec_start = Now; Update process start run time 32 33..... 34 35}36 Panax Notoginseng StaticINLINEVOID40 __update_curr (Structcfs_rq *cfs_rq, struct sched_entity *curr,42 unsigned longDelta_exec) 44 45{46 47 ... Curr->sum_exec_runtime + =  delta_exec;50/+  ... delta_exec_weighted = delta_exec;//run time from clock calculation (unlikely (curr->load.weight! =  nice_0_load)) {56 delta_exec_weighted =  Calc_delta_fair (delta_exec_weighted,58 &curr->load);//Consideration of the weight after the run time 60 61 }62 Curr->vruntime + = delta_exec_weighted; This value determines the position of the process in the red-black tree, the smaller the value, the closer to the left, the more easily be re-dispatched to, conversely, the more the more to the right, the scheduling opportunity becomes smaller. }66)  After counting the time in the clock cycle, Determine whether the process run time has reached the share allocated to it: void72 Check_preempt_tick (Structcfs_rq *cfs_rq, struct sched_entity *  Curr) [Ideal_runtime] (sched_slice)  {unsigned long  ideal_runtime, delta_exec;78 ;//Calculates the time that the current process is allowed to occupy. Delta_exec = Curr->sum_exec_runtime-curr->  prev_sum_exec_runtime;84 if (delta_exec;  Ideal _runtime) Resched_task (rq_of (CFS_RQ)->curr);//Set up a dispatch flag (in) "      

--Your own view of the operating system process model--

Since the previous contact with the operating system is windows, not much knowledge of Linux, but open source Linux for beginners to better learn more about the operating system, perhaps the Linux specific model is not known, but the core of the process to learn more

2018-05-01

Process analysis under the LINUX2.6.32

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.