Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Lecture



Scheduling of Jobs, Processes and Threads

1. Types of scheduling

Type of scheduling

Functions performed

Long-term

The decision to add a job (process) to the pool of those being executed in the system

Medium-term

The decision to add a process to the set of processes fully or partially residing in main memory

Short-term

The decision as to which of the available processes (threads) will be executed by the processor

I/O scheduling

The decision as to which of the processes' (threads') I/O requests will be handled by a free I/O device

Scheduling diagram taking into account job (process) queues

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and SynchronizationScheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

A typical thread state graph

Thread scheduling algorithms

1. Non-preemptive
  • scheduling is shared between the OS and application programs;
  • control must be passed to the OS frequently, otherwise an application may monopolize the processor;
  • application hangs can lead to a system crash

2. Preemptive

  • scheduling functions are concentrated in the OS;
  • scheduling based on processor time slicing (quantization);
  • scheduling based on thread priorities: static, dynamic, absolute, relative, mixed;

The simplest scheduling algorithm, implementing the thread states as in slide 27

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

A scheduling algorithm that gives preference to threads with intensive I/O

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

  • 1. Thread context switching wastes processor time.
  • 2. As the length of the time quantum increases, service to users deteriorates.
  • 3. In quantum-based algorithms, the OS has no information about the characteristics of the tasks being run

Thread state graph

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Priority scheduling algorithms

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and SynchronizationScheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Priority switching with time slicing

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Changing the base priority of a thread

Priority increase

  • + 1 – completion of disk I/O;
  • + 2 – for a serial line;
  • + 6 – keyboard;
  • + 8 – sound card;
  • + 2 – release of a semaphore block (for a foreground thread);
  • + 1 - release of a semaphore block (for a non-foreground thread);

priority 15 for 2 processor quanta, if a thread that is ready to run has been idle for longer than a certain threshold time.

Priority decrease

  • - 1 – if the processor time quantum has been fully used (repeatedly, down to the base priority).

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Interaction and synchronization of processes and threads
.1. Problems of interaction and synchronization

Degree of awareness

Relationship

Influence of one process on another

Potential problems

Processes are unaware of each other

Competition

Ø The result of one process does not depend on the actions of the others.
Ø One process may affect the running time of another.
ØMutual exclusion
ØDeadlocks
ØStarvation

Processes are indirectly aware of each other's existence

Cooperation by sharing

ØThe result of one process may depend on information obtained from the others.
ØOne process may affect the running time of another.
ØMutual exclusion
ØDeadlocks
ØStarvation
ØSynchronization

Processes are directly

aware of each other's existence

Cooperation by communication

ØThe result of one process depends on information obtained from other processes.
ØOne process may affect the running time of another.
ØDeadlocks (consumable resources)
ØStarvation

2. Competition among processes for resources

Competition is a situation in which two or more processes require access to the same resource (a printer, a file, etc.), called a critical resource. The part of a program that uses a critical resource is called a critical section.

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

The need for mutual exclusion:

  • 1. Processes must not be in their critical regions at the same time.
  • 2. The program must make no assumptions about the speed or the number of processes.
  • 3. A process that is outside its critical region cannot block other processes.
  • 4. A situation in which a process waits forever to enter its critical region must be impossible.

Deadlocks

A group of processes is in a deadlock if each process in the group is waiting for an event that only another process in the same group can cause

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

The "starvation" problem

Starvation. The halting of one or more threads of a multithreaded application for an indefinite time (or permanently). Threads that are not dispatched for execution, even though they are not blocked and are not waiting for anything, are said to be starving. The cause of starvation usually lies in the dispatching rules and policies. For example, if a continuously running non-blocking high-priority thread is dispatched on a single-core processor, another thread with a lower priority will never start running.

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

3. Cooperation by sharing

Processes that interact with other processes without explicit information about each other access shared variables, shared files or databases.

Problems: mutual exclusion, deadlock, starvation. In addition: process synchronization to ensure data consistency

Example: suppose the condition a = b must hold, with the initial values a = b = 1

Option 1: the processes execute sequentially

P1: a = a + 1; b = b + 1; P2: b = 2 * b; a = 2 * a;

Option 2: the processes interrupt each other

P1: a = a + 1; interrupt; P2: b = 2 * b; interrupt;

P1: b = b + 1; interrupt; P2: a = 2 * a;

Consistency is violated: a = 4, b = 3

Situations in which two or more processes handle shared data (files) and the final result depends on the relative speeds of the processes (threads) are called race conditions

4. Mutual exclusion methods

1. Disabling interrupts on entering the critical region and enabling interrupts after leaving the critical region. Advantages: simple to implement. Disadvantages: processor monopolization, a possible OS crash if a process fails, and it cannot be used in multiprocessor systems.

2. Lock variables (software approach)

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

3. Using system functions for entering a critical section

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

4. Dijkstra Semaphores

Semaphore: a variable S, primitives P (proberen – test; down) and V (verhogen – increment, up)

V(S) – the variable S is increased by 1 in a single action. Fetching, incrementing and storing cannot be interrupted. The variable S cannot be accessed while this operation is being executed.

P(S) – the variable S is decreased by 1 if this is possible, staying within the range of non-negative values. If S cannot be decreased, the thread executing the P operation waits until the decrease becomes possible. The P operation is indivisible.

In the special case, the semaphore S can take binary values 0 and 1, turning into a locking variable (a binary semaphore).

The P operation contains the potential for the process that executes it to move into a waiting state (if S = 0).

Under some circumstances, the V operation can activate a process that was suspended by the P operation.

A queue operating on the FIFO principle is used to hold the processes waiting on semaphores.

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

5. Deadlocks

Conditions for a deadlock situation to arise:

1. Mutual exclusion. Each resource is at any moment either assigned to exactly one process or unavailable.
2. Hold-and-wait condition. Processes currently holding previously acquired resources may request new resources.
3. No preemption of resources. Resources previously granted to a process cannot be forcibly taken away from it.
4. Circular wait condition. There is a circular chain of two or more processes, each of which is waiting for access to a resource held by the next member of the chain.

Strategies for dealing with deadlocks:

1. Ignoring the problem altogether.

2. Detection and elimination of deadlocks (recovery).

3. Avoiding deadlock situations through careful resource allocation.

4. Prevention by structurally negating one of the four conditions necessary for a deadlock.

Deadlock detection methods

1. One resource of each type in the system.

For example, suppose a system of seven processes (A, B, C, D, E, F, G) and six resources

(R, S, T, V, W, U) at some moment corresponds to the following list:

  • Process A holds resource R and wants to obtain resource S.
  • Process B uses nothing, but wants to obtain resource T.
  • Process C uses nothing, but wants to obtain resource S.
  • Process D holds resource U and wants to obtain resources S and T.
  • Process E holds resource T and wants to obtain resource V.
  • Process F holds resource W and wants to obtain resource S.
  • Process G holds resource V and wants to obtain resource U.

QUESTION: Is this system deadlocked, and if so, which processes are involved?

THE ANSWER CAN BE OBTAINED BY BUILDING A RESOURCE-PROCESS GRAPH.

• Resource-process graph

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

2. Several resources of each type in the system.

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Deadlock detection algorithm

It is based on comparing resource vectors. In the initial state, all processes are unmarked. As the algorithm proceeds, processes are marked to indicate that they can finish their work, i.e. they are not in a deadlock. After the algorithm terminates, any unmarked process is in a deadlock situation.

Algorithm

1. Look for a process Pi for which the i-th row of the matrix R is less than the vector

A, i.e. Ri <= Aj or ri j <= Aj , j = 1, m.

2. If such a process is found, it is marked, and then the i-th

row of the matrix C is added to the vector A, i.e. Aj := Aj + ci j , j = 1, m.

Return to step 1.

3. If no such processes exist, the algorithm terminates. If there are unmarked processes, they are in a deadlock.

•

Deadlock recovery methods

1. Preemption of resources. Taking a resource away from a process, giving it to another process, and then returning it in such a way that the original process does not "notice" this (difficult and most often impossible).
2. Recovery through "rollback". Processes periodically create checkpoints that allow a process to be restarted from an earlier state. When a deadlock occurs, the process holding a needed resource is "rolled back" to the checkpoint taken before it acquired the resource. If the resumed process again attempts to obtain this resource, it is put into a waiting state until the resource is released.
3. Recovery by killing processes.

Deadlock avoidance through safe resource allocation. Such algorithms are based on the concept of safe states. For example, Dijkstra developed a scheduling algorithm that makes it possible to avoid deadlocks (the banker's algorithm).

6. OS synchronization objects

To synchronize threads belonging to different processes, the OS must provide threads with system synchronization objects.

Such objects include events, mutexes (mutex – mutual exclusion), system semaphores, and others.

An event object is used to notify threads that certain actions have been completed.

A mutex (the simplest binary semaphore) is used to control access to data.

Semaphores are used to signal that a sequence of events has occurred.

"Ordinary" OS objects are also used for synchronization: files, processes, threads

All synchronization objects can be in a signaled or a non-signaled (free) state. Using the system call WAIT(X), a thread can synchronize its execution with a synchronization object X. Using the system call SET(X), a thread can put the object X into the signaled state. In addition, the OS defines a set of signals for logical communication between processes, as well as between processes and users (terminals).

7 Hardware and software support for multiprogramming

7.1. Interrupt systems

Classes of interrupts: external, internal, software

1. External interrupts – the result of user actions and of signals from the computer's peripheral devices and controlled objects.

2. Internal interrupts – the result of emergency situations arising during the execution of a program instruction.

3. Software interrupts – the result of executing special instructions planned in the program (a system call).

Principles of building interrupt systems:

  • hardware support (interrupt controller, DMA controller, external device controllers, buses connecting external devices, microprocessor facilities);
  • vectored, polled and combined interrupt methods;
  • priority servicing mechanism (with absolute and relative priorities);
  • interrupt masking;
  • interrupt dispatcher and interrupt service routines.

Sequence of actions when handling interrupts

1. Primary hardware recognition of the interrupt type. If interrupts are disabled, the current program continues. Otherwise, the interrupt dispatcher is called and, depending on the information received by the processor (interrupt vector, priority, etc.), the interrupt handling routine is invoked.

2. Some part of the context of the interrupted thread is saved, which will allow its execution to be resumed after the interrupt is handled (usually the processor status word – the EFLAGS register in the Pentium – and the general-purpose registers). The full context may also be saved if the OS services the interrupt with a process switch.

3. The address of the interrupt handling routine is loaded into the program counter, and a new PSW is set, which defines the privileged mode of processor operation during interrupt handling.

4. Interrupts are temporarily disabled by masking so that a queue of nested threads of the same routine does not form.

5. After the interrupt has been handled by the operating system kernel, the interrupted context is restored (partly by hardware – the PSW and the contents of the program counter, partly by software – retrieving data from the stack), the handling of interrupts of this type is re-enabled, and the thread resumes from the point of interruption.

7.2. System calls

A system call allows an application to ask the OS to perform some action, implemented as a procedure of the OS code segment.

The implementation of system calls must satisfy the following requirements:

  • provide switching to privileged mode;
  • have a high speed of invoking OS procedures;
  • provide, where possible, a uniform way of invoking system calls for all hardware platforms on which the OS runs;
  • allow simple extension of system calls;
  • enable the OS to control the correct use of system calls.

Possible schemes for servicing system calls:

1. Decentralized – each system call has its own interrupt vector assigned to it. Advantage – high speed of processing system calls; disadvantage – growth of the interrupt vector table.

2. Centralized – by means of a system call dispatcher.

Scheduling of Jobs, Processes and Threads: Algorithms, Interaction and Synchronization

Fig. Centralized scheme of system call handling

See also

  • Processes
  • Threads
  • Fibers
  • mutex
  • semaphore
  • transaction
  • [[b6777]]

  • [[b6067]]
  • [[b8571]]
  • [[b5964]]
  • [[b6054]]
  • [[b2532]]
  • [[b11322]]

See also

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Operating Systems and System Programming"

Terms: Operating Systems and System Programming