Process and Threads
Process and Threads
Process and Threads
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Process and Thread
● Process concept
● Operations on processes
● Inter-process communication
● UNIX pipes
● Multithreading
● Multithreaded models
● Programs using PThread
Operating System Concepts – 10th Edition 2.2 Silberschatz, Galvin and Gagne ©2018
Process Concept
● An operating system executes a variety of programs:
● Batch system – jobs
● Time-shared systems – user programs or tasks
● Textbook uses the terms job and process almost interchangeably
● Process – a program in execution; process execution must
progress in sequential fashion
● Multiple parts
● The program code, also called text section
● Current activity including program counter, processor
registers
● Stack containing temporary data
4 Function parameters, return addresses, local variables
● Data section containing global variables
● Heap containing memory dynamically allocated during run
time
Operating System Concepts – 10th Edition 2.3 Silberschatz, Galvin and Gagne ©2018
Process Concept (Cont.)
● Program is passive entity stored on disk (executable file),
process is active
● Program becomes process when executable file loaded into
memory
● Execution of program started via GUI mouse clicks, command
line entry of its name, etc
● One program can be several processes
● Consider multiple users executing the same program
Operating System Concepts – 10th Edition 2.4 Silberschatz, Galvin and Gagne ©2018
Process in Memory
Operating System Concepts – 10th Edition 2.5 Silberschatz, Galvin and Gagne ©2018
Process State
Operating System Concepts – 10th Edition 2.6 Silberschatz, Galvin and Gagne ©2018
Diagram of Process State
Operating System Concepts – 10th Edition 2.7 Silberschatz, Galvin and Gagne ©2018
Process Control Block (PCB)
Information associated with each process
(also called task control block)
● Process state – running, waiting, etc
● Program counter – location of
instruction to next execute
● CPU registers – contents of all
process-centric registers
● CPU scheduling information- priorities,
scheduling queue pointers
● Memory-management information –
memory allocated to the process
● Accounting information – CPU used,
clock time elapsed since start, time
limits
● I/O status information – I/O devices
allocated to process, list of open files
Operating System Concepts – 10th Edition 2.8 Silberschatz, Galvin and Gagne ©2018
CPU Switch From Process to Process
Operating System Concepts – 10th Edition 2.9 Silberschatz, Galvin and Gagne ©2018
Threads
Operating System Concepts – 10th Edition 2.10 Silberschatz, Galvin and Gagne ©2018
Multithreaded Server Architecture
Operating System Concepts – 10th Edition 2.11 Silberschatz, Galvin and Gagne ©2018
Benefits
Operating System Concepts – 10th Edition 2.12 Silberschatz, Galvin and Gagne ©2018
Process Scheduling
Operating System Concepts – 10th Edition 2.13 Silberschatz, Galvin and Gagne ©2018
Ready Queue And Various I/O Device Queues
Operating System Concepts – 10th Edition 2.14 Silberschatz, Galvin and Gagne ©2018
Representation of Process Scheduling
Operating System Concepts – 10th Edition 2.15 Silberschatz, Galvin and Gagne ©2018
Schedulers
● Short-term scheduler (or CPU scheduler) – selects which process should
be executed next and allocates CPU
● Sometimes the only scheduler in a system
● Short-term scheduler is invoked frequently (milliseconds) ⇒ (must be
fast)
● Long-term scheduler (or job scheduler) – selects which processes should
be brought into the ready queue
● Long-term scheduler is invoked infrequently (seconds, minutes) ⇒
(may be slow)
● The long-term scheduler controls the degree of multiprogramming
● Processes can be described as either:
● 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
● Long-term scheduler strives for good process mix
Operating System Concepts – 10th Edition 2.16 Silberschatz, Galvin and Gagne ©2018
Addition of Medium Term Scheduling
● Medium-term scheduler can be added if degree of multiple
programming needs to decrease
● Remove process from memory, store on disk, bring back
in from disk to continue execution: swapping
Operating System Concepts – 10th Edition 2.17 Silberschatz, Galvin and Gagne ©2018
Context Switch
● When CPU switches to another process, the system must save
the state of the old process and load the saved state for the
new process via a context switch
● Context of a process represented in the PCB
● Context-switch time is overhead; the system does no useful
work while switching
● The more complex the OS and the PCB 🡺 the longer the
context switch
● Time dependent on hardware support
● Some hardware provides multiple sets of registers per CPU
🡺 multiple contexts loaded at once
Operating System Concepts – 10th Edition 2.18 Silberschatz, Galvin and Gagne ©2018
Operations on Processes
Operating System Concepts – 10th Edition 2.19 Silberschatz, Galvin and Gagne ©2018
Process Creation
● Parent process create children processes, which, in turn
create other processes, forming a tree of processes
● Generally, process identified and managed via a process
identifier (pid)
● Resource sharing options
● Parent and children share all resources
● Children share subset of parent’s resources
● Parent and child share no resources
● Execution options
● Parent and children execute concurrently
● Parent waits until children terminate
Operating System Concepts – 10th Edition 2.20 Silberschatz, Galvin and Gagne ©2018
A Tree of Processes in Linux
Operating System Concepts – 10th Edition 2.21 Silberschatz, Galvin and Gagne ©2018
Process Creation (Cont.)
● Address space
● Child duplicate of parent
● Child has a program loaded into it
● UNIX examples
● fork() system call creates new process
● exec() system call used after a fork() to replace the
process’ memory space with a new program
Operating System Concepts – 10th Edition 2.22 Silberschatz, Galvin and Gagne ©2018
C Program Forking Separate Process
Operating System Concepts – 10th Edition 2.23 Silberschatz, Galvin and Gagne ©2018
Process Termination
Operating System Concepts – 10th Edition 2.24 Silberschatz, Galvin and Gagne ©2018
Process Termination
Operating System Concepts – 10th Edition 2.25 Silberschatz, Galvin and Gagne ©2018
Inter-Process Communication (IPC)
• Definition:
• Communication and data exchange between independent processes.
• Methods:
• Message passing, shared memory, and synchronization primitives.
• Need:
• Cooperation between processes for certain tasks.
Operating System Concepts – 10th Edition 2.26 Silberschatz, Galvin and Gagne ©2018
Interprocess Communication
● Processes within a system may be independent or cooperating
● Cooperating process can affect or be affected by other processes,
including sharing data
● Reasons for cooperating processes:
● Information sharing
● Computation speedup
● Modularity
● Convenience
● Cooperating processes need interprocess communication (IPC)
● Two models of IPC
● Shared memory
● Message passing
Operating System Concepts – 10th Edition 2.27 Silberschatz, Galvin and Gagne ©2018
Communications Models
(a) Message passing. (b) shared memory.
Operating System Concepts – 10th Edition 2.28 Silberschatz, Galvin and Gagne ©2018
Cooperating Processes
● Independent process cannot affect or be affected by the execution
of another process
● Cooperating process can affect or be affected by the execution of
another process
● Advantages of process cooperation
● Information sharing
● Computation speed-up
● Modularity
● Convenience
Operating System Concepts – 10th Edition 2.29 Silberschatz, Galvin and Gagne ©2018
Interprocess Communication – Shared Memory
Operating System Concepts – 10th Edition 2.30 Silberschatz, Galvin and Gagne ©2018
Interprocess Communication – Message Passing
Operating System Concepts – 10th Edition 2.31 Silberschatz, Galvin and Gagne ©2018
Message Passing (Cont.)
Operating System Concepts – 10th Edition 2.32 Silberschatz, Galvin and Gagne ©2018
Message Passing (Cont.)
Operating System Concepts – 10th Edition 2.33 Silberschatz, Galvin and Gagne ©2018
Direct Communication
● Processes must name each other explicitly:
● send (P, message) – send a message to process P
● receive(Q, message) – receive a message from process Q
● Properties of communication link
● Links are established automatically
● A link is associated with exactly one pair of communicating
processes
● Between each pair there exists exactly one link
● The link may be unidirectional, but is usually bi-directional
Operating System Concepts – 10th Edition 2.34 Silberschatz, Galvin and Gagne ©2018
Indirect Communication
Operating System Concepts – 10th Edition 2.35 Silberschatz, Galvin and Gagne ©2018
Indirect Communication
● Operations
● create a new mailbox (port)
● send and receive messages through mailbox
● destroy a mailbox
● Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Operating System Concepts – 10th Edition 2.36 Silberschatz, Galvin and Gagne ©2018
Indirect Communication
● Mailbox sharing
● P1, P2, and P3 share mailbox A
● P1, sends; P2 and P3 receive
● Who gets the message?
● Solutions
● Allow a link to be associated with at most two
processes
● Allow only one process at a time to execute a receive
operation
● Allow the system to select arbitrarily the receiver.
Sender is notified who the receiver was.
Operating System Concepts – 10th Edition 2.37 Silberschatz, Galvin and Gagne ©2018
UNIX Pipes
• Definition:
• Characteristics:
• FIFO (First In, First Out): Data is read in the order it was written.
Operating System Concepts – 10th Edition 2.38 Silberschatz, Galvin and Gagne ©2018
UNIX Pipes: Syntax and Usage
• Pipe Creation:
• The pipe() system call is used to create a pipe.
• Syntax: int pipe(int filedes[2]);
• Process Forking:
• The fork() system call is often used to create a child process.
• Both the parent and child processes can use the pipe for
communication.
• File Descriptors:
• The filedes array returned by pipe() contains two file descriptors:
• filedes[0]: Read end of the pipe.
• filedes[1]: Write end of the pipe.
• Duplication:
• The dup2() system call can be used to duplicate file descriptors,
ensuring that both processes have access to the pipe.
Operating System Concepts – 10th Edition 2.39 Silberschatz, Galvin and Gagne ©2018
Example: Piping the Output
#include <unistd.h> Explanation:
#include <stdio.h> The parent process writes the string "Hello,
int main() { Pipe!" to the pipe, and the child process
int filedes[2]; reads it.
char buffer[30]; Communication Direction:
pid_t child_pid;
Parent to Child:
// Create pipe
Parent writes to the pipe (write(filedes[1], ...))
pipe(filedes);
and child reads (read(filedes[0], ...)).
// Create child process
if ((child_pid = fork()) == 0) {
Child to Parent:
// Child process (reader) Child writes to the pipe and parent reads.
close(filedes[1]); // Close write end
read(filedes[0], buffer, sizeof(buffer));
close(filedes[0]); // Close read end
printf("Child Process: Received data - %s\n", buffer);
} else {
// Parent process (writer)
close(filedes[0]); // Close read end
write(filedes[1], "Hello, Pipe!", 13);
close(filedes[1]); // Close write end
}
return 0;
}
Operating System Concepts – 10th Edition 2.40 Silberschatz, Galvin and Gagne ©2018
Multithreading
• Definition:
• Execution of multiple threads within a process, sharing the same
resources.
• Threads are lighter than processes.
• Advantages:
• Improved responsiveness, resource sharing, and parallelism.
• Multithreading vs. Multicore (Multiprocessing):
• Comparison of thread-based concurrency and process-based
concurrency.
Operating System Concepts – 10th Edition 2.41 Silberschatz, Galvin and Gagne ©2018
Multicore Programming
Operating System Concepts – 10th Edition 2.42 Silberschatz, Galvin and Gagne ©2018
Multicore Programming (Cont.)
● Types of parallelism
● Data parallelism – distributes subsets of the same data
across multiple cores, same operation on each
● Task parallelism – distributing threads across cores, each
thread performing unique operation
● As # of threads grows, so does architectural support for
threading
● CPUs have cores as well as hardware threads
● Consider Oracle SPARC T4 with 8 cores, and 8 hardware
threads per core
Operating System Concepts – 10th Edition 2.43 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
● Concurrent execution on single-core system:
Operating System Concepts – 10th Edition 2.44 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded Processes
Operating System Concepts – 10th Edition 2.45 Silberschatz, Galvin and Gagne ©2018
Amdahl’s Law
● Identifies performance gains from adding additional cores to an
application that has both serial and parallel components
● S is serial portion
● N processing cores
● But does the law take into account contemporary multicore systems?
Operating System Concepts – 10th Edition 2.46 Silberschatz, Galvin and Gagne ©2018
Multithreaded Models
• User-Level Threads (ULTs):
• Managed entirely by the application.
• Lightweight, but limited by the host operating system.
• Kernel-Level Threads (KLTs):
• Managed by the kernel.
• Greater flexibility but heavier.
• Many-to-One, One-to-One, Many-to-Many Models:
• Discussion of different mapping strategies between user-level and
kernel-level threads.
Operating System Concepts – 10th Edition 2.47 Silberschatz, Galvin and Gagne ©2018
User Threads and Kernel Threads
Operating System Concepts – 10th Edition 2.48 Silberschatz, Galvin and Gagne ©2018
Multithreading Models
● Many-to-One
● One-to-One
● Many-to-Many
Operating System Concepts – 10th Edition 2.49 Silberschatz, Galvin and Gagne ©2018
Many-to-One
Operating System Concepts – 10th Edition 2.50 Silberschatz, Galvin and Gagne ©2018
One-to-One
● Each user-level thread maps to kernel thread
● Creating a user-level thread creates a kernel thread
● More concurrency than many-to-one
● Number of threads per process sometimes
restricted due to overhead
● Examples
● Windows
● Linux
● Solaris 9 and later
Operating System Concepts – 10th Edition 2.51 Silberschatz, Galvin and Gagne ©2018
Many-to-Many Model
● Allows many user level threads to be
mapped to many kernel threads
● Allows the operating system to create
a sufficient number of kernel threads
● Solaris prior to version 9
● Windows with the ThreadFiber
package
Operating System Concepts – 10th Edition 2.52 Silberschatz, Galvin and Gagne ©2018
Two-level Model
● Similar to M:M, except that it allows a user thread to be
bound to kernel thread
● Examples
● IRIX
● HP-UX
● Tru64 UNIX
● Solaris 8 and earlier
Operating System Concepts – 10th Edition 2.53 Silberschatz, Galvin and Gagne ©2018
Thread Libraries
Operating System Concepts – 10th Edition 2.54 Silberschatz, Galvin and Gagne ©2018
Pthreads
Operating System Concepts – 10th Edition 2.55 Silberschatz, Galvin and Gagne ©2018
Pthreads Example
Operating System Concepts – 10th Edition 2.56 Silberschatz, Galvin and Gagne ©2018
Pthreads Example (Cont.)
Operating System Concepts – 10th Edition 2.57 Silberschatz, Galvin and Gagne ©2018
Pthreads Code for Joining 10 Threads
Operating System Concepts – 10th Edition 2.58 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program
Operating System Concepts – 10th Edition 2.59 Silberschatz, Galvin and Gagne ©2018
Windows Multithreaded C Program (Cont.)
Operating System Concepts – 10th Edition 2.60 Silberschatz, Galvin and Gagne ©2018