Process Shutdown and Restart

CHAPTER 4. PROCESS MANAGEMENT

4.1 Processes in UNIX

4.1.1. Process overview

A process is an object in the system that corresponds to an execution session of a program. A process consists of three components: text, data, and stack. Text is the execution statement component, data is the data component, and stack is the temporary information component that operates according to the LIFO mechanism. The statements in text only operate on the corresponding data and stack areas of the process, and cannot access the data and stacks of other processes, except for shared data areas.

Processes are identified by their process number, abbreviated as PID (Proccess Index). The process created at operating system startup is process 0. All other processes are created from another process through the fork system call : the process that executes the fork system call is called the parent process, and the process created by the fork call is called the child process. Except for process 0 which has no parent, all processes in the system have a parent, and a parent can have many children.

The result of translating the source program will create a target program file consisting of the following parts (stored on an external medium):

The file header describes some characteristics of the program file (similar to the File header of a program file in MS-DOS),

The text part of the program,

Initial values ​​for memory allocation to the program's data area,

Some information tables related to file placement.

 When there is a fork call , through the exec system call , the kernel will load the contents of the file.

program into internal memory according to text, data and stack areas:

The text area of ​​the process corresponds to the program file,

The process data area corresponds to the values ​​specified in the file

programme,

The stack area is automatically created by the kernel with a size according to the flexibility of the kernel.

The stack consists of logical stack frames: each stack frame is inserted when a function is called and removed when it returns. Each stack frame contains function parameters, local variables, etc. Correspondingly, in the stack there is a stack pointer related to the depth of the stack. In the program code there are instructions that manage the shape of the stack, and the kernel will allocate space to the stack as required.


4.1.2. Preliminary control structure of UNIX

Hierarchically, the system operates at three levels: user level, kernel level, and hardware level.

User level: includes user programs and programs in libraries. These programs run (their instructions execute) in the user state of the process. User programs interact with the kernel either directly or indirectly by calling libraries through system calls .

The kernel level is the most important level of the Linux-UNIX operating system. Running at the kernel level are operating system programs belonging to the File Control Subsystem, the Process Control System, system calls, Device Drivers, Buffer cache and Hard Control programs. The two most basic components are the File Control System and the Process Control Subsystem.


User level

Level

core

Interface according to system calls

Control subsystem

File

HARDWARE CONTROL

User program

Sample Library

Cache buffer

The figure below gives a rough outline of the control structure in UNIX:


Process control subsystem (consists of 3 subcomponents on the right

Transmission

interprocess communication

Scheduler

Ingredient

memory coordination

Maybe you are interested!

Process Shutdown and Restart


Character

Block

Device Control


Level

n Hardware related


HARDWARE


Structure of the Core and Process Levels


4.1.3. Subsystems in the kernel

The File control subsystem is responsible for managing the File system and providing free memory.

on disk for Files, File access control and data search etc. Most of the algorithms for

File-related system calls and low-level subroutine functions were presented in Chapter 2. Processes interact with the File Control Subsystem through system calls (high-level File system calls). Access to Files is through two ways: direct access to the File or through the buffer cache.

Buffer caches store data temporarily in blocks. Data input and output are accessed through intermediate blocks and secondary memory devices: data is accessed in blocks,

The kernel directly operates with the device control block to directly access data in the File without going through the auxiliary device: character-based access.

The process control subsystem is responsible for synchronizing interprocess interactions, managing memory, and scheduling execution for existing processes. The File Control Subsystem and the Process Control Subsystem interact when files are loaded into internal memory and executed. Some system calls for the process control block:

fork: Create a new process. The call to this function is of the form pid=fork()

exec: Execute an existing process; exec(pid)

exit: To terminate an existing process,

brk: controls the size of memory allocated to the process,

signal: Controls abnormal phenomena during the process

The process control subsystem consists of the following three components:

The memory coordinator component is responsible for managing and controlling memory allocation. Some pages are discarded when allocating memory to a process.

The scheduler is responsible for allocating CPU to processes. Processes have priorities and the scheduler selects the process with the highest priority.

Interprocess communication performs synchronization of related processes.

Hardware control is responsible for allowing interrupts and information interaction with the machine. Devices such as disks and terminals can interrupt the CPU while it is performing a process. Interrupt handlers are separate functions in the kernel, not processes.

Stack in progress

Each process executes in kernel mode and user mode so it is divided into two types.

kernel stack and user stack. Let's consider the following example:

#include <fenth.h> char buffer[2048]; int version;

main(argc, argv);

int argc; char *argv[];

|

int fdold, fdnew; if (argc != 3)

|

printf('2 arguments required for file copy program!');

exit(1)

|

fdold = open (argv[1], O_RDONLY); /* open read-only source file */ if (fdold == -1)

|

printf (' Cannot open file &csn',argv[1]); exit(1);

|

fdnew =create (argv[2],0666); /*open destination file rw for everyone */ if (fdnew ==-1)

|

printf('Could not initialize file &csn',argv[2]; exit(1);

|

copy(fdold,fdnew); exit(0);

|

copy (old, new)

int old, new;

|

int count;

while (count = read(old,buffer,sizeof(buffer))>0) write(buffer,count);

|


In the above program, the code (called the text part) of the file is generated from the main and copy functions. Initialize the version variable with an initial value and reserve memory for the buffer array variable.

User Stack

Local variables

Not available

Frame 2 address

Return address after

write

Parameters of write

buffer counter

new

Local variables

count

Address of Frame 1

Return address after copy call

Parameters

old

Stack multiplier


of stack


Local variables

Address of Frame 1

Return address after calling func2

Parameters of func2 multiplication

In the above example, the parameters argc, argv and the variables fdold, fdnew in the main program are in the stack when main is called (once for every program), while the parameters old and new and the variable count in the copy function appear every time copy is called.


­ Upward


frame 3 Frame 3

call write () call

func2()

of copy

new

Local variables

fdold

fdnew

Address of Frame 0

Return address after call

main

Parameters of

main

argc

argv


Local variables


Address of Frame 0

Return address after call

func1

Parameters of func1 multiplication

frame 2 Frame 2

call copy () call

func2()


Frame 1 Frame 1

call main() call func1()

Stacks for a process


A process in UNIX executes in one of two modes: kernel mode or user mode, and for each mode, the process uses a separate stack.

The user stack contains arguments, local variables, and other data for functions executed in user mode. The kernel stack contains arguments, local variables, parameters, binding addresses, etc. related to functions executed in kernel mode.


4.1.4. Preliminary information on process control

The kernel uses the following four data structures to access processes:

Process table, corresponding to each existing process in the system is a component. Each component includes the following fields (each component here is a PCB):

- Process status,

- Owner of the process,

- Field related to the process's pause state (according to the sleep function call)

- The address of the user area corresponding to the process,

- The corresponding information is presented in the PCB.

The user area (U-area) contains special information that is effective while the process is running:

- Component index corresponding to the process in the process table: address of the corresponding PCB block,

- Kernel-level and user-level runtime counters,

- The return values ​​and error codes (if any) for the current system call,

- Description of open files corresponding to the process,

- Parameters for storing the amount of data moved in and out.

- Current directory and current root directory: the environment of the process,

- File and process size limits,

- Allowable levels for the process,

- Some other information

Memory address tables for each process,

Table containing common memory areas: memory partitions, characteristics of each area according to partition: contains text, data or shared memory areas, etc.

A brief overview of the relationships between the above data structures is shown in the figure below.

The kernel handles system calls as follows:

- With the fork call: Copies the address space of the old process, allowing processes to share memory space,

- With the exec call: The kernel allocates real memory areas for the text, data and stack areas,

- With the exit call: The kernel will free the memory areas associated with the process.

Table of regions

remember for one

U-area













progress

Memory area table

(virtual page table)


Internal memory (physical pages)

Process control data structures


4.1.5. States and state transitions

Waiting for lack of resources (4)

Realize the level

multiplication (2)

The diagram representing states and state transitions in UNIX is shown in the figure below (Process state numbers are shown in the figure).


User level implementation (1)

Ready for user level (7)

Complete

(9)


Ready to do (3)

The process of emergence (8)

Waiting for memory to execute (5)

Waiting in memory

outside (6)

Process state transition diagram

When a process is spawned it is in state (8), depending on whether the process memory is allocated to internal memory (3) or external memory (5). State (3) represents that the process is ready to execute, its components are already in internal memory waiting for the CPU to execute. The subsequent execution depends on its previous state. If it is spawned for the first time, it needs to go to the kernel level execution to complete the work the fork call will go from state

(3) to state (1), otherwise from state (3) it goes to user level CPU waiting state (7).

In the user-level execution state (1), the process goes to state (2) when a system call or interrupt occurs. From state (1) to state (7) when the time quantum expires.

State (4) is the waiting state in memory and state (6) represents waiting in external memory.

The transition from state (2) to state (2) occurs when, while in the kernel execution state, the system kernel calls the corresponding interrupt handler functions.


4.1.6. Process shutdown and restart

A process in the kernel level execution state is capable of entering the suspended state upon a call to the sleep function . The suspended state occurs in a number of situations waiting for an event: completion of I/O, another process making an exit call, etc.

After the event occurs, the process moves from the stopped state to the ready-to-go state.

can be allocated CPU to run.


4.1.7. Preliminary command for the process

When you open a man page , list files with ls , run the vi editor , or run any command in Linux, you are creating one or more processes. In Linux, any running program is considered a process. There can be multiple processes running at the same time. For example, the command ls -l | sort | more will create three processes: ls , sort , and more .

A process can go through different states and at any given time a process is in one of those states. The table below shows the basic process states in Linux.


Symbol Meaning

D ( uninterruptible sleep ) in this state the process is suspended and cannot be restarted by a signal.

R ( runnable ) is the state of being ready to execute, meaning the process is able to execute but is waiting for its turn to execute because another process is available.

S CPU.

( sleeping ) a state of suspension, that is, a process of inactivity

Dynamic (20 seconds or less)

( traced or stopped ) stopped state, the process may be suspended by a

Z external process

( zombie process ) the process has finished executing, but it is still being

W reference in the system

no permanent pages


< higher priority process N lower priority process L has locked pages inside memory


4.2. Basic commands

4.2.1. The fg command and the bg command

Linux allows users to use the key combination CTRL+z to stop a process and restart it by typing the command fg . The fg ( foreground ) command refers to the programs that the screen and keyboard are working with.

For example, a user is viewing the man page for the sort command, and sees the -b option at the bottom , and wants to try this option while still viewing the man page . Instead of typing q to exit and then running the man command again , allow the user to type CTRL+z to pause the man command and type the command to try the -b option . After testing, type fg to resume viewing the sort command's man page . The result of the above process is displayed as follows:

# man sort | more SORT(1) FSF SORT(1) NAME

sort - sort lines of text Files SYNOPSIS

../src/sort [OPTION] ... [Files]... DESCRIPTION

Write sorted concatenation of all FILE(s) to standard output.

+POS1 [-POS2]

start a key at POS1,end it *before* POS2 obsoles-cent)field numbers and character offsets are num-bered starting with zero(contrast with the -k option)

-b ignore leading blanks in sort fields or keys

--More-- (CTRL+z)

[1]+ Stopped man sort | more

# ls -s | sort -b | head -4

1 Archives/

1 InfoWorld/

1 Mail/

1 News/

1 OWL/

# fg

man sort | more

--More--


In the previous section, the way to press CTRL+z to pause a process was introduced. Linux also provides users with a way to run a program in the background.

Comment


Agree Privacy Policy *