Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

EDAF35: OPERATING SYSTEMS

MODULE 3
PROCESSES, THREADS
CONTENTS
MODULE 3

• “Process” concept, features and operations

• Inter-process communication (IPC) CHAPTER 3

• Examples from common OS

• “Thread” concept, relation to processes

• Multithreading and OS CHAPTER 4

• Examples of threading APIs


PROCESSES (CH3)
A FEW DEFINITIONS
PROCESSES

A process (in memory)

• job (batch systems), task/user program (time sharing system)


= process

• process — “a program in execution”

• sequence of instructions (text), data (heap, stack,…),


current instruction to execute (PC), other state info, etc.

execute

PROGRAM

DISK
PROCESS STATE
PROCESSES

• multiple processes on the same CPU

• only one active (running) at any time


PCB AND CONTEXT SWITCHES
PROCESSES

Context Switch from P0 to P1 and back

Process Control Block (PCB)

PID

(e.g. “ready”) (“running”)


PROCESS SCHEDULING
PROCESSES

• multiprogramming — keep several processes in memory and run them concurrently

• a goal: maximize the use of the CPU

• processes migrate among different queues: job queue — all processes, ready
queue — waiting to run, device queue — waiting for a particular I/O device

• schedulers — selects which process to run next

• good mix of I/O-bound and CPU-bound processes

more in Module 5

A “queuing diagram” — helper tool


OPERATIONS ON
PROCESSES
CREATE TERMINATE
• “parent” creates “children” • normally — execute last instruction,
(tree of processes) produces a exit code
(main return or exit)

• parent terminates a child


(identified via “pid”, given on creation)

• choice: whole branch or only one?


• how do they execute relative to each other? • zombie vs. orphan processes
(wait)

• what happens to the parent’s resources? CHECK MAN PAGES FOR:


FORK, EXEC, WAIT, EXIT, PS, KILL
FORKING PROCESSES IN UNIX (C)

fork() returns both in child (0) and in parent


(child pid)

exec() replaces the process’ memory with a


new process!

(instructions after the exec line are not run)


INTERPROCESS COMMUNICATION (IPC)
PROCESSES

• independent vs. cooperating processes:


sharing information, computation speedup, modularity, convenience

• two basic IPC types:

(a) message passing

(b) shared memory

advantages and drawbacks?


AN EXAMPLE: POSIX SHARED MEMORY
PROCESSES

• Create
shared memory segment (“everything is a file in UNIX”):
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
— other processes use it to open an existing segment.
• Set the size of the object: ftruncate(shm_fd, 4096);
• Map it to memory: STANDARD
ptr = mmap(0, 4096, PROT_READ, MAP_SHARED, shm_fd, 0); FILE
OPERATIONS
• Write to/read from the shared memory:
sprintf(ptr, "Writing to shared memory”);
• Remove the segment: shm_unlink(name);

…check also man pages…


IPC — MESSAGE PASSING
PROCESSES

• more structured and controlled than shared memory

• goto model for distributed systems

• operations — send(message,…), receive(message,…)

• naming — direct (process-to-process) vs. indirect communication (via mailboxes)

• synchronization — blocking (synchronous) vs. non-blocking (asynchronous)

• buffering — zero/bounded/unbounded capacity


AN EXAMPLE: MACH MESSAGE PASSING
PROCESSES

• even system calls are messages

• Kernel and Notify mailboxes (ports) in each task

• three system calls:


msg_send(), msg_receive(), msg_rpc()

• ports created via:


port_allocate() • e.g. on full mailbox choose to:

‣ wait indefinitely
• send and receive — flexible
‣ wait at most n milliseconds

‣ return immediately

‣ temporarily cache a message


COMMUNICATIONS IN CLIENT-SERVER SYSTEMS
PROCESSES

• Sockets

• Remote Procedure Calls (RPC) / Remote Method Invocations (Java RMI)

• Pipes

focus of Networking and Web Programming courses — see book for more details
SOCKET COMMUNICATION
PROCESSES
SOCKETS IN JAVA
PROCESSES
A Date Server
• Three types of sockets:

• connection oriented (Transmission


Control Protocol TCP) — messages
arrive in order as sent

• connectionless (User Datagram


Protocol UDP) — no order
guarantees

• multicast — send data to several


recipients

see book for the client code


PIPES
PROCESSES

• another IPC mechanism, originally from UNIX

• choices:
uni- or bi-directional?
full or half duplex?
parent—child based or not? Rd Wr
local or network based?

• ordinary (anonymous) vs. named pipes

see UNIX and Windows code samples in the book


A SIMPLE UNIX PIPE EXAMPLE
PROCESSES
F
ig
int fd[2]; ur
e
pid_t pid; th
is
o
pipe(fd); ut
pid = fork(); by
ch
ec
if(pid == 0) { k
in
dup2(fd[0], STDIN_FILENO); g
close(fd[1]); th
e
exec(<whatever>); m
an
} else {
pa
close(fd[0]); ge
… s!
THREADS (CH4)
SINGLE- VS. MULTI-THREADED PROCESSES
THREADS

thread
context
WHY MULTIPLE THREADS?
THREADS

• Responsiveness – part of a process can block while other parts still run (e.g. GUI)

• Resource Sharing – process resources are shared (no IPC needed)

• Economy – cheaper than processes, thread switching lower overhead

• Scalability – multithreaded processes can take advantage of multiprocessors


MULTICORE PROGRAMMING
THREADS

More challenging to efficiently use the architecture:

• divide activities

• balance

• divide the data

• handle dependencies

• test and debug

— see Jonas Skeppstedt course, EDAN26


CONCURRENCY VS. PARALLELISM
THREADS

T3 and T4 (and T1, T2) execute concurrently

T3 and T4 execute in parallel


PARALLELISM AND PERFORMANCE

1
speedup ≤ 1−S
S+ N
AMDAHL’S LAW

The serial portion of an application (S) has a disproportionate


effect on performance when adding additional cores (N).
TYPE OF THREADS IN AN OS
THREADS

POSIX THREADS
WINDOWS THREADS
JAVA THREADS

MANAGED HERE = USER LEVEL


User Space
May (or not) employ these

VIRTUALLY ALL
Kernel Space MANAGED HERE = KERNEL LEVEL GENERAL PURPOSE
OS
MULTITHREADING MODELS
MAPPING THREADS

User Space

Kernel Space
1 many-to-one 2 one-to-one 3 many-to-many

4 bind only some


MOST COMMON:
WINDOWS, LINUX
PTHREADS
THREAD LIBRARIES

• interface/specification, not implementation

• may be implemented as user or kernel level

• POSIX standard API, IEEE Std 1003.1c—1995

• thread creation and synchronization

• common in Unix-like OS (BSD, Mac OS X, Linux,…)

• some ports for Windows


PTHREADS EXAMPLE
THREAD LIBRARIES
IMPLICIT THREADING
PROGRAMMING WITH THREADS

Can we decouple programming functionality from thread management?

text book

THREAD POOLS

OPENMP
others
GRAND CENTRAL DISPATCH

THREADING BUILDING BLOCKS (C++ LIB)

JAVA.UTIL.CONCURRENT (JAVA LIB)

see also Patrik Persson’s course, EDAP10 — Concurrent Programming


THREADING ISSUES

• fork() and exec() semantics with threads


see Unix kill and pthread_kill
• signal handling — which thread(s)?

• thread cancellation — async. vs deferred see pthread_cancel and pthread_testcancel

• thread local storage


see C11 thread_local and pthread_key_*
• scheduler activations
SCHEDULER ACTIVATIONS
THREADS

• “many-to-many”— how many k-threads?

• lightweight process (LWP) — intermediate level data structure

• kernel: LWP attached to a k-thread


(blocks if k-thread blocks)

• user: LWP is virtual processor


(u-threads can be scheduled on it) http://www.cs.washington.edu/homes/bershad/Papers/p53-anderson.pdf

• scheduler activation — scheme for communicating between u-thread lib and kernel

• upcalls — kernel informs u-thread lib about k- events (e.g.“LWP about to block”)
WINDOWS THREADS
OS EXAMPLES

• Windows API — Win 98, NT, 2000, XP, 7

• kernel-level, one-to-one

• executive thread block (ETHREAD),


kernel thread block (KTHREAD),
thread environment block (TEB)

• separate kernel & user stacks


LINUX THREADS
OS EXAMPLES

• called tasks (= threads = processes)

• remember “one-to-one” model

• clone(…),clone3(…) — like fork(), but finer control of what is shared

see man clone


END OF MODULE 3

You might also like