You are on page 1of 15

1/25/24

Operating Systems
Processes and Threades
Dr. Youssef Iraqi
College of Computing

Email: Youssef.Iraqi@um6p.ma

Operating Systems 1

Process Model

• All runnable software (including OS) are organized into number of


sequential processes
• Process is an activity of some kind
• has program, input, output, state
• Each process has its own virtual CPU
• CPU switches between processes according to scheduling
algorithm

Operating Systems 2

1
1/25/24

Process Model

• Multiprogramming of four programs


• Conceptual model of 4 independent, sequential processes
• In a single CPU, only one process active at any instant

Operating Systems 3

Process Creation

Principal events that cause process creation


• System initialization
• when OS booted, several processes created
• Execution of system call to create process
• in Unix, fork system call creates child process
• identical copy of parent
• continues from point of creation
• in Win32, CreateProcess system call
• has 10 parameters which include the program to execute
• parent and child have their own address space
• In Unix, child’s initial address space is a copy of parent’s
• In Windows both address spaces are different from start
• User request to create a new process
• Initiation of a batch job (in batch systems)

Operating Systems 4

2
1/25/24

Process Termination

Conditions which terminate processes


• Normal exit (voluntary)
• When program compiled, compiler insert at end a system call to terminate
process
• exit in Unix and ExitProcess in Windows
• Error exit (voluntary)
• Fatal error (involuntary)
• ex: executing an illegal instruction
• Killed by another process (involuntary)
• Kill in Unix and TerminateProcess in Window

Operating Systems 5

Process Hierarchies

• Parent creates a child process, child processes can create its own
process
• Forms a hierarchy
• UNIX calls this a process group
• Windows has no concept of process hierarchy
• all processes are created equal

Operating Systems 6

3
1/25/24

Process States

• Process may be in one of 3 states


• Ready - runnable
• just admitted to system
• OR temporarily stopped to let another process run
• OR was waiting for an event that happened (ex. input)
• Running - using CPU
• only ready processes become running
• scheduler dispatches process into CPU
• Blocked
• unable to run until some external event happens (such as I/O completion or
reception of signal)

Operating Systems 7

Process States

Operating Systems 8

4
1/25/24

Implementation of Processes

• OS maintains a process table for each process


• Table contains information about process
• its state, its program counter, stack pointer, memory allocation, status of its
open files, its accounting and scheduling information
• and all information needed when the process is switched between the 3
states
• Table is initialized when process created

Operating Systems 9

Implementation of Processes

Sample fields of a process table entry

Operating Systems 10

10

5
1/25/24

Threads

• Single sequential flow of control within a process


• Multiple threads run concurrently in process performing different
tasks
• each has its own Program Counter
• share resources of the same process
• Can be created and destroyed dynamically

Operating Systems 11

11

The Thread Model (1)

(a) Three processes each with one thread


(b) One process with three threads
1 2 3

Operating Systems 12

12

6
1/25/24

The Thread Model (2)

Items shared by all threads in a process and Items private to each


thread

Operating Systems 13

13

The Thread Model (3)

Each thread has its own stack

Operating Systems 14

14

7
1/25/24

Multithreading

• Processes normally start with a single thread present.

• This thread has the ability to create new threads by calling a


library procedure, for example, thread_create.

• The creating thread is usually returned a thread identifier that


names the new thread.

Operating Systems 15

15

Multithreading (2)

• When a thread has finished its work, it can exit by calling a library
procedure, say, thread_exit.
• In some thread systems, one thread can wait for a (specific)
thread to exit by calling a procedure, for example, thread_wait.
This procedure blocks the calling thread until a (specific) thread
has exited.
• Another common thread call is thread_yield, which allows a
thread to voluntarily give up the CPU to let another thread run.
• Other calls allow one thread to wait for another thread to finish
some work, for a thread to announce that it has finished some
work, and so on.

Operating Systems 16

16

8
1/25/24

Issues with Multithreading

• While threads are often useful, they also introduce a number of


complications into the programming model.
• Example: UNIX fork system call.
• If the parent process has multiple threads, should the child also have them?
If not, the process may not function properly, since all of them may be
essential.
• However, if the child process gets as many threads as the parent, what
happens if a thread in the parent was blocked on a read call, say, from the
keyboard? Are two threads now blocked on the keyboard, one in the parent
and one in the child? When a line is typed, do both threads get a copy of it?
Only the parent? Only the child? The same problem exists with open
network connections.

Operating Systems 17

17

Issues with Multithreading (2)

• Another class of problems is related to the fact that threads share


many data structures.
• What happens if one thread closes a file while another one is still
reading from it?
• Suppose that one thread notices that there is too little memory
and starts allocating more memory. Part way through, a thread
switch occurs, and the new thread also notices that there is too
little memory and also starts allocating more memory. Memory
will probably be allocated twice.
• Careful thought and design are needed to make multithreaded
programs work correctly.

Operating Systems 18

18

9
1/25/24

Why having Threads?

• In many applications, multiple activities are going on at once. By


decomposing such an application into multiple sequential
threads that run in quasi-parallel, the programming model
becomes simpler.
• Threads are easier to create and destroy than processes. In many
systems, creating a thread goes 100 times faster than creating a
process.
• When an application has substantial computing and also
substantial I/O, having threads allows these activities to overlap,
thus speeding up the application.
• Threads are useful on systems with multiple CPUs.

Operating Systems 19

19

Example of Multi-threaded Programs

• Most programming languages have API for threads


• Java
• Thread object
• Runnable object
• POSIX threads
• C, UNIX-like systems
• pthreads
• Microsoft Windows
• Win32 API

Operating Systems 20

20

10
1/25/24

Example of Multi-Threaded Programs (continued)

Operating Systems 21

21

Example of Multi-Threaded Programs (continued)

Operating Systems 22

22

11
1/25/24

Thread Usage (1)

A word processor with three threads


• Thread 1: interaction with the user
• Thread 2: document reformatting
• Thread 3: document backup

Operating Systems 23

23

Thread Usage (2)

A multithreaded Web server

Operating Systems 24

24

12
1/25/24

Thread Usage (3)

Rough outline of code for previous slide


• (a) Dispatcher thread
• (b) Worker thread

Operating Systems 25

25

Implementing Threads in User Space

A user-level threads
package

Operating Systems 26

26

13
1/25/24

Implementing Threads in User Space (2)

Advantages:
• User-level threads package can be implemented on an operating system that
does not support threads.
• Each process can have its own customized scheduling algorithm.
• User-level threads package scale better
Major problems:
• How blocking system calls are implemented
• Page faults
• Unless a thread enters the run-time system of its own free will, the
scheduler will never get a chance.
• Once a trap has occurred to the kernel, it is hardly any more work for the
kernel to switch threads if the old one has blocked.

Operating Systems 27

27

Implementing Threads in the Kernel

A threads package managed


by the kernel

Operating Systems 28

28

14
1/25/24

Hybrid Implementations

Multiplexing user-level
threads onto kernel-
level threads

Operating Systems 29

29

15

You might also like