What is the context of a process

Operating Systems (Operating System Theory) - Chapter 3 - Processes and Threads



Contents of Dr. E. Nadobnyh

3.1. Processes


Basic concepts


1) a process(Task) is an execution of the program in its own process environment. The process is a central concept of parallel processing in operating system theory.

2) One Process environment(Runtime environment, execution environment) consists of the process context and assigned resources.

3) a resource(Resources) can be hardware or an information unit. The following resources can be assigned to each process:
  1. CPU,
  2. Random access memory,
  3. other resources: open files, consoles, network connections, etc.

4) a Process contextis a detailed description of the process:
  1. Register contents,
  2. Address space description,
  3. Description of the assigned resources,
  4. Administrative data internal to the operating system.

5) a programis a procedural regulation for data processing.
  1. A program is part of the process environment.
  2. A particular program can be executed by several processes.

6) a job(Session, application) is a user request to the operating system.
  1. A job can consist of one or more processes.
  2. Jobs are independent and protected from others.


Process model


1) All software is organized as a set of processes.

2) The operating system creates an illusion for every process that it has the entire computer available to itself. This illusory calculator was realized through multitasking.

3) Every process does not notice any interruption, only the time delay.

4) Each process is protected from other processes.


PCB


1) a PCB* -Process Control Block (Process Context Block) is managed by the operating system to manage a process.
A PCB is a representation of a process and contains its process context.
2) The Process tableis managed by the BS for the management of processes and contains PCBs from all processes.

It can also be implemented as a list.



Typical information in a PCB


1. Register contents:
  1. PC - Program Counter,
  2. PSW - Program Status Word,
  3. SP - stack pointer,
  4. general registers.

2. Information for storage management:
  1. Page table address,
  2. Base register contents,
  3. Boundary register contents,
  4. Addresses to code, data and stack segments of the process, etc.

3. Description of the assigned equipment:
  1. Information for file management: file descriptors, home directory, working directory, etc.
  2. Network connections,
  3. allocated devices, etc.

4. Process management data internal to the operating system:
  1. Process number (PID-Process IDentification)
  2. Process status, e.g. active, ready, blocked,
  3. Priority for scheduling,
  4. Processor time used since the start,
  5. Process number of the parent process (PPID, Parent PID), etc.

⇒ Demo 1.


Process change


Synonyms: process switch, context switch, context switch, process switch, task switch.

Two processes are involved in the process change:
  1. the suspended process that was active before the interruption, and
  2. the activated process that was inactive before the interruption. The activated process was selected by the scheduler.

Two resources are involved in the process change:
  1. CPU register and
  2. Random access memory.


Change of register contents


This switch consists of context backup and context recovery.

(1) At the Context protection(Context saving) register contents of the suspended process must be saved so that they can be restored later.
  1. The interrupt context is saved from the stack in the PCB.
  2. Other register contents are saved from the CPU in the PCB.
Analogy: snapshot.

(2) At the Context recoveryregister contents of the activated process are restored, i.e. copied from the PCB to the CPU.


Change of memory contents


This change takes place e.g. in the computer with virtual memory.

When changing the process:

(3) some memory areas of the suspended process are swapped out and

(4) some address space areas of the activated process can be loaded from the hard disk to memory as required.


Creation and termination of the process


There are a few ways to create a new process:
  1. at system start, e.g. idle process,
  2. by user request, e.g. cmd console,
  3. by system call in the running process, e.g. fork in UNIX or CreateProcess in Windows,
  4. by system call for batch job, e.g. batch job.

There are a few ways to terminate a process:
  1. voluntary termination by system call, e.g. exit in UNIX or exit process in Windows;
  2. involuntary termination by the system due to the error, e.g. trap;
  3. involuntary termination by another process, e.g. kill.

⇒ Demo 2.
⇒ Demo 3.


Process states


A process goes through various states during its lifetime. A basic model contains three states:

1) Active (ongoing, running). The process owns the CPU. In a single processor system, only one process can be in this state at any given time.

2) Ready to calculate. The process is waiting for CPU allocation.

3) Blocked (waiting, blocked). The process is waiting for an event, e.g .:
  1. Expiry of a period of time which the process itself has commissioned, e.g. sleep,
  2. Allocation of a resource other than CPU,
  3. Message from another process.

Other process states are also possible in specific operating systems.



There are the following transitions between the states in the basic model:

(1) blocking. The active process releases the CPU because it is waiting for an event, e.g. for an input. The process is blocked.

(2) Displacement (deactivation, preemption, interruption of priority). The CPU is withdrawn from the active process, e.g. after the interrupt from the timer.

(3) Allocation (activate). The CPU is assigned to the process.

(4) Wake up (wakeup, unblocking). The reason for blocking is released because the resource is available.


3.2. Threads


definition


Threads are parallel activities in a process.

Synonyms: threads, strands, light weight processes (LWP).

Basic idea: The entire computing work of a process is divided into several activities, which can run in parallel.

One-threaded process


Multi-threaded process



example


A text system (e.g. Microsoft Word) can be efficiently parallelized using four threads:

1) enter text,
2) check spelling,
3) design line / page breaks,
4) periodically create a backup copy on the disk. All four processes work together with a text document.


Thread model


1) A thread is an execution of a part of a program.

2) Each thread belongs to a specific process.

3) Every process implicitly contains at least one thread.

4) Each process can generate any number of threads.

5) All threads of a process can be executed in parallel (pseudo-parallel). 6) Thread has states (active, ready to compute, blocked).

7) Thread has its own register set and its own stack.

8) All threads of a process have a common process environment, e.g. the same address space, the same open files, etc.

9) Each thread is not protected against other threads in this process.


Separation of resource management and processor management


The resources are shared between the process and the threads:

1) A process owns all resources except registers and stack.

2) Each thread has its own register set and its own stack. The CPU is not assigned to the process but to the threads.

3) The process owns the resources, the thread executes the code.


TCB


A thread context is put together in the so-called thread control block (TCB):

1) thread identification,
2) thread status (active, ready to compute, blocked),
3) priority for scheduling,
4) register contents,
5) Pointer to PCB, etc.

A PCB then does not contain any register content and is reduced accordingly.


Generation and termination of the thread


1) If a process is created, it provides an execution environment for threads. The code of the process is executed by a thread (primary thread) that is automatically created along with the process.

Each thread can create additional threads that belong to the same process.

2) Each thread can terminate itself or be terminated by another thread. If the primary thread is terminated, the entire process is terminated.

With process termination, all of its threads are also terminated.

⇒ Demo 4.
⇒ Demo 5.


Advantages and disadvantages of threads


1) Creating, terminating, communicating and changing the context of threads are much faster than with processes. This is where the term lightweight processes comes from.

2) The context change takes place particularly often. A thread context switch is approximately 100 times faster than a process context switch.

Steps in changing context Process changeThread change
Replace TCB + +
Change the storage management table + -
Replace memory areas + -
Emptying and reloading the cache + -


3) Threads are not protected from each other and must be synchronized by programmers.


Multi-threading


Multi-threading is an operating mode with several threads per process.

Examples:

Single thread systems Multi-thread systems
One process system MS-DOS Java runtime system, e.g. JDK version 1.6
multitasking Older UNIX variants Windows XP, Solaris, Modern UNIX



Thread forms


1) Kernel-level threads (KL threads) are managed by the operating system.

2) User-level threads (UL threads, user threads) are managed by the process. The operating system is not aware of the existence of UL threads.
  1. Advantage: The UL threads can be switched more quickly. For example, when measuring time for a VAX-UNIX system, the following relationships resulted: ULTh / KLTh / processes = 1/10/40.
  1. Disadvantage: If a UL thread becomes blocked (e.g. due to I / O), then all UL threads of the process are also blocked.
  1. Example: Fibers from Windows.


Java threads


1) Java is a multithreading language.

2) Java threads are allocated to the CPU by the JVM.

JVM (Java Virtual Machine) is the runtime system for
Java programs.


3) Forms of Java threads:
  1. UL threads (Green Threads), e.g. JDK version 1.2 for Linux,
  1. KL threads, (native threads), e.g. Windows NT / 2000 / XP.

⇒ Demo 6
⇒ Demo 7



CategoryBSys