Overview of operating system conceptual processes

Source: Internet
Author: User
Tags switches terminates time limit

ProcessProcess ConceptsProcess

Process is an executing procedure, which is only an informal statement. The process is not just program code, the program code is called the text segment (code snippet), also includes the current activity, the value of the program counter (PC) and the contents of the processor register to represent. In addition, processes include process stack segments (temporary data, function parameters, local variables, addresses), and data segments (including full global variables). It may also include a heap (leap), which allocates memory dynamically during a process run.


A program is a passive entity, such as a file content (executable file) that contains a series of instructions stored on disk, and a process is an activity entity that has a program counter to represent the next command to execute and a collection of related resources.

Although two processes can be associated with the same program, as a user invokes a different copy of the Web browser, they are independent processes. Although the code snippets are the same, data segments, stacks, and so on are different.

process Status

Process Status:

    • NEW: The process is being created
    • Run (RUNNING): instruction is being executed
    • Wait (wait): The process waits for an event to occur (such as I/O completion or signal received)
    • Ready: Process waits for the processor to be allocated
    • Terminate (TERMINAL): Process completes execution

Only one process can run on one processor at a time, but multiple processes can be in a ready or waiting state.

Process Control block (PCB)

Processes are represented in the operating system using the Process Control BLOCK,PCB , which contains information such as process status, program counters, CPU registers, CPU scheduling information, memory management information, accounting information, I/O status information, and so on.

    • Process state: The state can include new, ready, running, waiting, terminating, and so on.
    • Program Counter: The counter indicates the address of the next instruction to be executed by the process.
    • CPU registers (CPU registers): Together with program counters, the status information of these registers needs to be saved in the event of an outage so that the process can execute correctly later.
    • CPU scheduling information (CPU scheduling information): This type of information includes process priority, dispatch queue pointers, and other scheduling parameters.
    • Memory Management Information (memory-management information): Depending on the memory system, this information includes the base and boundary register values, the page table, or the segment table.
    • Billing information (Accounting information): This information includes CPU time, actual usage time, time limit, accounting data, number of jobs or processes, and so on.
    • I/O state information (I/O status information): This information contains the list of I/O devices assigned to the process, the list of open files, and so on.
Process Scheduling

The purpose of the multi-channel program is to have process execution whenever possible, thus maximizing CPU utilization. The purpose of a time-sharing system is to quickly switch CPUs between processes so that users can interact with them while the program is running. To achieve this, process scheduling selects an available process to execute on the CPU. A single-processor system will never have more than one process executing. If there are multiple processes, then the rest will need to wait for the CPU to be idle and re-dispatched.

Scheduling Queue

When the process enters the system, it is added to the job queue, which contains all the processes on the system. Programs that reside in-memory ready and waiting to be run are saved in the ready queue. This queue is commonly used to implement a list of nodes whose head node points to the first and last PCB block pointers of the list. Each PCB includes a pointer field to the next PCB of the ready queue.

Each process in Linux is defined by the TASK_STRUCT data structure. Task_struct is what we typically call a PCB, and it also contains pointers to parent and child processes. For example, the status of a process is represented by a long state field in the structure.

In the Linux kernel, all the active processes are represented by a doubly linked list called task_struct, and the kernel holds a pointer to the currently running process.

If the kernel wants to modify the currently running process state value to new_state. If current is a pointer to the present process:

current -> state = new_state;

The new process starts in the ready queue, and it waits in the ready queue until it is selected to execute or be dispatched. When a process is assigned to CPU execution, it can occur:

    1. The process sends an IO request and puts it into the IO queue.
    2. Process creates a new child process and waits for it to end
    3. The process forces the CPU to be freed due to an outage and is put back into the ready queue

For the first two cases, the process eventually switches from the wait state to the ready state and puts it back in the ready queue. The process continues this loop until it terminates, and by then it will be removed from all queues and its PCB and resources will be freed.

Dispatch Program

Processes migrate between various scheduling queues, and for scheduling, the operating system must select processes from these queues in some way. The selection of the process is performed by the appropriate scheduler (Scheduler) .

Typically in batch systems, processes are more committed, rather than executed immediately. These processes are usually placed in the buffer pool of the disk for later execution. The long-term scheduler or job scheduler selects a process from this pool and loads the memory to prepare for execution. The short-term dispatcher or CPU scheduler chooses the process from the process being prepared for execution and allocates the CPU to it.

The main difference between the two schedulers is the frequency of the dispatch.

The short-term scheduler usually executes at least once, and the short-term scheduler must be fast because of the short time between executions of the 100ms.
Short-term scheduler is invoked very frequently (milliseconds) ? (Must be fast).

The long-term scheduler executes infrequently, so the long-term scheduler can use more time to select the execution process.
Long-term scheduler is invoked very infrequently (seconds, minutes) ? (may be slow).

The degree to which long-term scheduling controls the design of multichannel programs (number of processes in memory degree of multiprogramming). The long-term scheduler must carefully select the execution process. Typically, the vast majority of processes can be divided into:

I/o-bound process–spends more time doing I/O than computations, many short CPU bursts.
Cpu-bound process–spends more time doing computations; Few very long CPU bursts.

I/O-based processes typically perform I/O aspects more time than execution calculations, and on the other hand, CPU-dominated processes rarely produce I/O requests. For the system to achieve balance and better performance, the long-term scheduler should choose a reasonable include IO-based and CPU-based combination process to fully use the device and short-term scheduler.

For Linux and Windows systems, there is usually no long-term scheduler, the stability of these systems depends on physical constraints, if the performance of the system degrades a lot, there will be a user exit.

Some systems, such as time-sharing systems, may introduce medium-term scheduler, whose core idea is to be able to move processes out of memory, thus reducing the degree of multi-channel program design, after which processes can be swapped in.

Context Switches

The interrupt causes the CPU to change from the current task to running the Kernel subroutine (the saying is not comprehensive, the running kernel subroutine is the system call, and the system call is a soft interrupt). When an outage occurs, the system needs to save the context of the process that is currently running in the CPU, so that it can recover the context after it has finished processing. The context of the process is represented by a PCB. Typically, you save the current state of the CPU by performing a state save , and then perform a state restore to start again.

Switching the CPU to another process requires saving the current state and recovering another process state, called context switch. When a context switch occurs, the kernel saves the state of the old process on the PCB and then loads the context of the new process that is scheduled to be executed and saved.

Context switching time is an extra overhead because the system does not do anything useful when switching. The switching time is closely related to hardware support.

Process ActionsProcess Creation

When a process executes, it can create several new processes by creating a process system call. The process is created as a parent process, and the new process is called a child process. The new process can then create another process, which forms the process tree.

Most operating systems identify processes based on a unique process identifier (INDENTIFIER,PID), and PID is usually an integer value.

In Unix, you can get a list of processes using the PS command.

Typically processes require certain resources (such as CPU time, memory, files, I/O devices) to complete their tasks. When a child process is created, the child process may get resources directly from the operating system or only from the parent process. The parent process may have to allocate resources or share resources (such as memory or files) between its child processes, restricting the ability of the child process to use only the parent process's resources to prevent overloading of the system caused by creating too many processes.

When a process is created, the initialization data (or input) is passed to the child process by the parent process, in addition to the various physical and logical resources.

When a process creates a new process that is created, there are two possible types of executions:

    • The parent process executes concurrently with the child process
    • The parent process waits until one or all of the child processes have finished executing

The address space of the new process also has two possibilities:

    • A child process is a replica of the parent process (with the same program and data as the parent process).
    • Child process loading another new program

UNIX operating systems, each process is identified by a unique integer identifier, and a new process is created through the fork () system call, which is made by replicating the address space of the original process. This mechanism allows for convenient communication between parent and child processes.

Two processes continue to execute instructions after the system call fork (), but for child processes, the return value of the system call Fork is 0: and for the parent process, the return value is the process identifier of the child process (not 0).

Usually after the system calls fork, a process uses the system to call EXEC () to replace the process's memory space with a new program. The system calls exec () to load the binary into memory (eliminating the program memory map that originally contained the system call EXEC ()) and starts execution. In this way, two processes can communicate with each other and execute in their own way.

The parent process can create more child processes, or if there is nothing to do when the child process runs, it takes the system call wait () to move itself out of the ready queue to wait for the child process to terminate.

#include <cstdio>#include <unistd.h>#include <syspes.h>#include <sys/wait.h>#include <stdlib.h>#include <iostream>#define DEBUG (a) cout << #a "=" << (a) << Endl;UsingNamespaceStdint main (int argc,char* argv[]) {pid_t pid;int F0 =0, F1 =1, TMP; PID = fork ();if (PID <0) {printf"Fork failed!\n");Return-1; }Elseif (PID = =0) {printf"I am the Son process\n"); Debug (Atoi (argv[1]));int num = atoi (argv[1]);if (Num <1) {printf"Requst ilegal!\n");Return-1; }Switch (num) {Case1:printf"0\n");BreakCase2:printf"0 1\n");Break ; default: { printf ("0 1"); For (int i = 3; I <= num; i++) {tmp = f1; f1 = F0 + f1; f0 = tmp; printf ("%d", F1);} printf ("\ n");}} } else { printf ("I am The parent process\n"); Wait (NULL); printf ("I am The parent process\n"); printf ("Parent process exit!\n");} return 0;}                  

The other code found on the network is explained in detail.

When the child process completes by calling exit (), the parent process proceeds from the wait () call and calls exit () to indicate the end.


Process Termination

The process terminates when the process finishes executing the final statement and uses the system call exit () to request the system to delete itself. At this point, the process can return a status value (usually an integer) to the parent process (by calling wait () through the system). All process resources (physical and virtual memory, open files, and I/O buffering) are freed by the operating system.

In other cases, termination is also possible.

The process can terminate another process through an appropriate system call. Typically, only the parent process of the terminated process can perform this system call. Otherwise, the user can terminate each other's jobs arbitrarily.

The parent process terminates its child process for a number of reasons, such as:

    • The child process uses more resources than it allocates. (To determine whether this occurs, the parent process is required to have a mechanism to check the state of its child processes)

    • Tasks assigned to child processes are no longer required

    • The parent process exits, and if the parent process terminates, the operating system does not allow child processes to continue (some operating systems, which are known as cascading terminations for such operating systems).

UNIX: You can terminate a process by calling exit () from the system, and the parent process can call wait () through the system to await the termination of the child process. System call Wait () returns the process identifier that aborts the child process so that the parent process can know which child process is terminated. If the parent process terminates, all its child processes take the INIT process as the parent process, so the child process still has a parent process to collect state and execution statistics.

inter-process communication

Whether a process shares data with other processes can be divided into independent and collaborative processes, either independent or collaborative.
You may need to provide an environment to allow process collaboration for the following reasons:

    1. Information sharing
      Multiple users may be interested in the same information
    2. Increase the computational speed:
      If you want a specific task to run quickly, you must divide it into sub-tasks, each of which can be executed in parallel with other subtasks. If you want to achieve such an acceleration, you need a computer with multiple processing units (such as CPU and I/O Channels)
    3. Modular:
      The system may need to be structured in a modular fashion
    4. Convenient:
      A single user may also perform multiple tasks at the same time.

A collaborative process requires an interprocess communication mechanism (IPC) to allow processes to exchange data and information with each other. There are two basic mode modes of interprocess communication:

    • Shared Memory: Exchange information by creating a piece of memory that is shared by the collaborative process and reading and writing data in the region.
    • Messaging: communication is achieved by exchanging messages between collaborative processes.

For both of these modes, messaging is useful for exchanging less data and is easier to implement, but requires more time for kernel intervention.
Shared memory is faster than message delivery and allows for convenient communication at the fastest speed.

Process Shared Memory

Shared memory systems need to establish shared memory areas. Typically a piece of shared memory area resides in the address space of the process that generated the shared memory segment. Other processes that want to communicate using this shared memory segment must place this on their own address space. The form or location of the data depends on these processes rather than the operating system, and the process is responsible for ensuring that they do not write data to the same region at the same time.

Producer-consumer issues are a common example of a collaborative process. Producer processes generate information for consumer processes to consume. For example, the assembler code produced by the compiler is used by the assembler, which in turn produces the target code for connection and loader use.

Using shared memory is one of the methods to solve the production value-consumer problem. To allow producer processes and consumer processes to execute concurrently, there must be a buffer to be populated by the producer and used by the consumer. This buffer resides in a shared memory area where the producer can produce another item when the consumer uses one. Producers and consumers must synchronize so that consumers do not consume an item that is not produced.

Two types of buffering are available:

Infinite buffering has no limit on buffer size. Consumers may have to wait for new items, but producers can always produce new items.

The finite buffering assumes that the cache size is fixed. In this case, if the buffer is empty, then the consumer must wait, if the buffer is full, then the producer must wait.

# define BUFFER-SIZE 10typedef struct     {...}  item;item buffer[BUFFER-SIZE];int  in=0;int out=0;

The shared cache is implemented by looping an array and two logical pointers in and out, which points to the next slot in the buffer, and out points to the first full bit in the buffer. When in = = out, the buffer is empty; when (in+1)%buffer-size = = out, the buffer is full.

Producer and consumer codes are as follows: The producer process has a local variable nextproduced to store the resulting new item. The consumer has a local variable nextconsumed to store the new item to be used.

Producer Process:

while (true) {   /* Produce an item */while (((in = (in + 1) % BUFFER SIZE count) = = out); /*do nothing */buffer[in] = item;in = (in + 1) % BUFFER SIZE;}

Consumer processes:

while (true) {while (in == out); // do nothing -- nothing to consume// remove an item from the bufferitem = buffer[out];out = (out + 1) % BUFFER SIZE;return item;}

This method allows the maximum number of entries for the cache to be buffer-size-

Message delivery system

Messaging provides a mechanism to allow a process to communicate and synchronize without having to share an address space, which is useful in distributed systems. For example, the chat program for WWW is to communicate through information exchange.

If the process p and Q need to communicate, there will be a communication line between them (communication link). There is no concern about the physical implementation of the circuit to discuss only the logical implementation. Here are some logical lines and how to receive/Send operations:

    • Direct or indirect communication
    • Synchronous or asynchronous communication

These related questions are studied below

1. Naming

Processes that need to communicate must have a method to reference each other, and they can use direct or indirect communication.

Direct communication: Each process must be explicitly named as the recipient or sender of the communication, as defined below:

    • Send (P,message) sends information to process P
    • Receive (Q,message) receives a message from Q

The communication lines for this scenario have the following properties:

    • Automatically establish a line between each pair of processes that need to communicate. Processes only need to know the identifiers of each other's communication.
    • One line only related to two processes
    • There is only one line between each pair of processes

This approach demonstrates symmetric addressing, where the sending and receiving processes must name each other for communication.

A variant of this scheme uses asymmetric addressing, which is defined as long as the sender names the recipient:

    • Send (P,message) sends message to process P
    • Receive (Id,message) receives a message from any process, and the variable ID is set to the process name that communicates with it

The disadvantage of symmetric and asymmetric addressing is that it limits the modularity of process definitions

Indirect communication, sending and receiving messages through mailboxes or ports.
A mailbox can be abstracted as an object in which a process can hold messages or remove messages from it, with unique identifiers for each mailbox. A process may communicate with other processes through many different mailboxes. However, two processes can communicate with each other only when they share at least one mailbox.

    • Send (A,message) sends a message to mailbox a
    • Receive (A,message) receives a message from mailbox a

For this scenario, the communication line has the following properties:

Can communicate with each other only if two processes share at least one mailbox

One line can be associated with more than two processes

There can be different lines between two processes, one mailbox per line

2. Synchronization

Message passing can be either blocking or non-blocking-also known as synchronous or asynchronous.

    • Block send: The sending process is blocked until the message is received by the receiving process or mailbox.
    • Non-blocking send: The send process sends a message and then continues the operation.
    • Blocking receive: The recipient is blocked until a message is available.
    • Non-blocking receive: The recipient receives a valid message or an empty message.

Send and receive can be either blocked or non-blocking. When both are blocked, there is a collection point (rendezvous) between the recipient and the sender. When using blocking send and receive, it is not important how to address producer and consumer issues. The producer only needs to call block Send () and wait until the message is sent to the process or mailbox. Similarly, when a consumer calls receive (), blocking occurs until a message is available.

3. Buffering

There are three ways to implement a queue, regardless of whether the process is direct or indirect, and the messages exchanged by the communication process reside in the staging queue

      • 0 Capacity: The maximum length of the queue is 0. There is no message waiting in the line, and in this case, the message must be blocked until the recipient receives the message.
      • Limited capacity: Up to n messages reside in, such as line full, blocking sender
      • Unlimited capacity

Overview of operating system conceptual processes

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.