Lecture 4: Processes

Fall 2019 Jason Tang

Slides based upon Concept slides, http://codex.cs.yale.edu/avi/os-book/OS9/slide-dir/index.html Copyright Silberschatz, Galvin, and Gagne, 2013 1 Topics

• Process Concept

• Process Lifecycle

• Scheduling

• Process Creation and Termination

2 Process Concept

• An OS executes processes (programs in execution)

• Text section: executable code

• Program counter and processor registers: current state of CPU

• Stack: function parameters, return addresses, local variables

• Heap: dynamically allocated variables

• Data section: global variables

3 Process Concept

• Program is passive, stored as an executable file on disk, while a process is active

• Program becomes a process when executable file loaded into memory

• Execution of program begins with a double-click, tap an icon, etc.

• Single program can spawn multiple several processes

• Such as by multiple users executing the same program

4 Process Memory Layout

For most operating systems and architectures, stack grows downwards (towards address 0).

5 Process Lifecycle

• A process transitions through several states during its lifetime

• New: process is being created, memory allocated and initialized

• Running: process is using the CPU to execute instructions

• Waiting: OS has suspended process because it is waiting for I/O, timer, or some other event to occur

• Ready: process has all necessary resources, but something else is occupying CPU currently

• Terminated: process has finished execution; OS is cleaning it up

6 Process Lifecycle

7 Process Control Block

• OS represents each process within a Process Control Block (PCB) (also known as a task)

• Process state

• Unique process identifier

• Program counter

• Scheduling information - task priority

• Memory management - memory allocated to process

8 Process Control Block

• Accounting information - CPU time spent, time elapsed

• I/O status - list of open files, network connections

• Security information - owner of process

• CPU registers - copy of registers while process is suspended

9 CPU Switch from Process to Process

10 Threads

• A process with multiple program counters, one per thread

• Threads may be simultaneously executing on multiprocessor system

• PCB contains separate sections for each thread

11 Processes in

• Represented by structure task_struct struct task_struct { … /* -1 unrunnable, 0 runnable, >0 stopped: */ volatile long state;

/* * This begins the randomizable portion of task_struct. Only * scheduling-critical items should be added above here. */ randomized_struct_fields_start

void *stack; atomic_t usage; /* Per task flags (PF_*), defined further below: */ unsigned int flags; unsigned int ptrace; … /* Filesystem information: */ struct fs_struct *fs;

/* Open file information: */ struct files_struct *files; … }

https://elixir.bootlin.com/linux/latest/source/ 12 include/linux/sched.h Process Scheduling

• While one process is waiting, OS runs another process

• Process scheduler: mechanism for how OS chooses which ready process to run next

• Scheduling queue: queues of processes, categorized by process state

• Job queue: set of all processes in system

• Ready queue: processes waiting to execute

• Device queue: processes waiting for I/O to finish

13 Process Scheduling

• Queueing diagram represents queues, resources, flows

14 Schedulers

• Short-term scheduler (or CPU scheduler) - selects which ready process to execute next

• Invoked frequently (every few milliseconds / microseconds)

• Invoked after handling an interrupt or a system call

• Long-term scheduler (or job scheduler) - selects which processes to bring into ready queue, used for batch processing

• Invoked infrequently (every few seconds or even minutes)

• Not present in all operating systems

15 Schedulers

• Medium-term scheduler - moves suspended processes from main memory to secondary storage (swap out), or vice versa (swap in)

• Sometimes combined with long-term scheduler

16 Choosing Scheduling Algorithms

• Generally, processes are of one type or another:

• I/O-bound - spends majority of time waiting for I/O or user interaction; many short CPU bursts, followed by long waiting times

• CPU-bound - spends majority of time performing calculations; few but long CPU bursts

• Schedulers must balance time spent running CPU-bound processes with interactiveness expected by I/O-bound processes

17 Context Switch

• Context switch: when CPU switches to another process, it saves state of original process (registers, program counter) to main memory, and then loads from memory the next process’s state (registers, program counters)

• Context of process represented by its PCB

• During a context switch, system is not doing any useful work

• Time to make context switch based upon hardware; some architectures faster than others

• More complex OSes have more complex context switches

• Larger context switch means slower computer response time

18 Process Creation

• Parent process creates child processes, which create grandchild processes, forming a process tree

• Each process has a unique process identifier (PID)

• Resource sharing options:

• Parent and children share all resources, or children share subset, or children share no resources

• Execution options:

• Parent and children execute concurrently, or parent waits for children to terminate

19 Example Linux Process Tree

$ pstree -h -p -G systemd(1)!"!ModemManager(344)!"!{ModemManager}(373) # $!{ModemManager}(377) %!NetworkManager(340)!"!dhclient(12487) # %!{NetworkManager}(413) # $!{NetworkManager}(416) … %!systemd(483)!"!(sd-pam)(484) # %!at-spi-bus-laun(733)!"!dbus-daemon(738) # # %!{at-spi-bus-laun}(734) # # %!{at-spi-bus-laun}(735) # # $!{at-spi-bus-laun}(736) … # %!--cal(813)!"!{gnome-shell-cal}(814) # # %!{gnome-shell-cal}(816) # # %!{gnome-shell-cal}(827) # # %!{gnome-shell-cal}(828) # # $!{gnome-shell-cal}(1036) # %!gnome-terminal-(1277)!"!bash(1299)!!!pstree(28960)

20 Process Creation

• Typical pattern is fork() and then exec()

• fork - OS creates child process, that is duplicate of parent’s PCB

• Child PCB gains its own PID, and starts running at the parent’s PC

• execute - OS then replaces the memory space (text and data sections) with new program

21 Example Fork/Exec Pattern (Unix)

#include #include #include #include #include

int main(void) { pid_t pid = fork(); if (pid < 0) { // error occurred fprintf(stderr, "fork failed\n"); exit(EXIT_FAILURE); } else if (pid == 0) { // child process execlp("/bin/ls", "ls", NULL); fprintf(stderr, "exec failed\n"); exit(EXIT_FAILURE); } // parent process waitpid(pid, NULL, 0); printf("Child process done\n"); exit(EXIT_SUCCESS); }

22 Process Termination

• Process ends when it executes last statement, it prematurely quits (such as exit() function), or some other process forcibly terminates it (such as by sending it a kill() signal)

• OS stores return status data from child, so that parent can retrieve it later (via wait() or equivalent)

• OS releases the rest of process’s resources

• Closes files, deallocates memory, other cleanup activities

• In Linux, this process becomes a zombie until parent reaps the return value

23 Process Termination

• For some OSes, if a parent process terminates, the OS will automatically terminate its child processes (and recursively grandchildren, etc.)

• For other OSes, a parent can terminate without causing this cascading termination

• If a parent terminates without reaping its children, those child process become orphans

• In Linux, the init process (typically PID 1) becomes the orphans’ new parent

24