First job: Analysis of process model based on Linux operating system

Source: Internet
Author: User

1. 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. It is not just the code of the program, it also includes the current activity, which is shown by the value of the program counter and the contents of the processing register.

• The concept of the process is mainly two points: first, the process is an entity. Each process has its own address space, in general, the package expands the text area (text region), the data region, and the stack. The text area stores the code executed by the processor, the data region stores variables and the dynamically allocated memory used during process execution, and the stack area stores the instructions and local variables for the active procedure call. Second, the process is an "executing procedure". The program is an inanimate entity, and only the processor gives the program life (the operating system executes it) to become an active entity, which we call a process.

Characteristics of the process:

• Dynamic: The essence of the process is the process of a program in a multi-channel program system, the process is dynamic generation, dynamic extinction. • Concurrency: Any process can execute concurrently with other processesIndependence: The process is a unit that can operate independently, but also the system allocates resources and scheduling independent units;• Asynchrony: Because of the inter-process constraints, the process has a discontinuity of execution, that is, the process is moving forward at their own, unpredictable speed .2. Creation and termination of processes

The following types of operations can create and terminate threads.

A Create Process

(1) System initialization creates a new process

(2) When a running process executes a system call to create a process, a new process is also created

(3) The user makes a request to create a process

(4) A new thread is also created when a batch job is initialized

Technically, there is only one way to create a new process, which is to create a new process in an already existing process through a system transfer.

You can use the Fork function in Linux to create a new process. As shown in the following code:

#include <stdio.h>  #include<sys/types.h>  #include<unistd.h>  int Main () {         = fork ();       printf ("Hello Proc:%d,ret =%d\n", Getpid (), ret);          return 0 ;    }

B process Termination

(1) Normal exit

(2) Error exit

(3) Fatal error

(4) Killed by other processes

3. Transition of the state of the process

A running process may have the following three basic states. (1) Readiness Status (ready):The process has obtained the required resources in addition to the processor, waits for the processor resources to be allocated, and executes as long as the processor process is assigned. A ready process can divide queues by multiple priorities. For example, when a process enters a ready state due to time slices, it is queued to a high priority queue when the process is ready by I/O operations. (2) operating status (Running):The process consumes processor resources, and the number of processes in this state is less than or equal to the number of processors. When no other process can execute, such as when all processes are in a blocking state, the idle process of the system is usually performed automatically. (3) blocking status (Blocked):because the process waits for a certain condition, such as an I/O operation or a process synchronization, the execution cannot continue until the condition is satisfied. The event does not run even if the processor resource is assigned to the process before it occurs.

4. Scheduling Algorithms for Processes

Time slice rotation scheduling algorithm:

Each process is assigned a time period, called its time slice, which is the time that the process is allowed to run. If the process is still running at the end of the time slice, the CPU is stripped and assigned to another process. If the process blocks or ends before the end of the time slice, the CPU switches immediately. What the scheduler has to do is maintain a list of ready processes that are moved to the end of the queue when the process runs out of its time slice.

• The algorithm takes a very fair approach, that is, to let each process on the ready queue run only one slice at a time. If there are n processes on the ready queue, then each process can get a 1/n processor time approximately every time.

• The size of the time slice has a significant impact on the system performance. Selecting a small time slice will benefit short jobs, but it means that the process scheduling and the process context switching are frequently performed, which will undoubtedly increase the overhead of the system. Conversely, if the time slice is chosen too long, and in order to make each process can be completed in a time slice, the time rotation scheduling algorithm will degenerate into a first-come-first service algorithm, unable to meet the needs of short-term jobs and interactive users.

Algorithm Run Flowchart:

The definition of a program control block:

typedefstructNode {Charname[ -];/*the name of the process*/          intRound/*time slices for allocating CPUs*/          intCPUTime;/*CPU Execution Time*/          intNeedtime;/*the time it takes for the process to execute*/          CharState/*status of the process, w--ready state, r--execution state, f--completion state*/          intCount/*record number of executions*/          structNode *next;/*Linked list Pointers*/}PCB; 

voidRoundrun ()/*time-Slice rotation scheduling algorithm*/        {          intFlag =1;          GetFirst ();  while(Run! =NULL) {              while(flag) {Run->count++; Run->cputime++; Run->needtime--; if(Run->needtime = =0){Run->state ='F';                Insertfinish (run); Flag=0; }              Else if(Run->count = = Run->round){Run->state ='W'; Run->count =0;Inserttime (run); Flag=0; }} Flag=1;          GetFirst (); }        }

For the analysis of the source code:

First, set a flag bit to 1, get the first node from the ready queue, when the node is not empty and the flag bit is 1 o'clock, the number of times that the process executes plus one, the CPU execution time is also self-added, the process takes time to subtract, and then to judge, when the process takes 0 of the process is completed, Set the status of the process to completion, and then insert the process into the end of the completion queue, otherwise when the time slice runs out, the state of the process is set to ready, the counter is zeroed, the process is inserted into the end of the ready queue, the flag position is 0, and the first node is retrieved from the ready queue again. Until the end of all process execution.

5. Your own view of the operating system model

The operating system is the interface between the user and the computer, but also the computer hardware and other software. Is the most basic system software that runs directly on the machine, and any other software must be run with the support of the operating system. So the operating system is very important to us for computers and learning computers. Among them, the process is the most basic and core of the operating system, we need to understand and grasp the process, which for us in the future more in-depth study of the operation of the decency also has a very important meaning.

First job: Analysis of process model based on Linux operating system

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.