Processes and Threads What are processes? How does the operating system manage them? Nick Urbanik nicku@vtc.edu.hk Department of Information and Communications Technology Copyright Conditions: Open Publication License (see http://www.opencontent.org/openpub/) OSSI — ver. 1.5 Processes - p. 1/112 What is a process? I I I I I A process is a program in execution Each process has a process ID In Linux, $ ps ax prints one line for each process. A program can be executed a number of times simultaneously. N Each is a separate process. Introduction What is a process? What is a process? — 2 What is a thread? Program counter Environment of a process Permissions of a Process Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 2/112 What is a process? — 2 I I I A process includes current values of: N Program counter N Registers N Variables A process also has: N The program code N It’s own address space, independent of other processes N A user that owns it N A group owner N An environment and a command line This information is stored in a process control block, or task descriptor or process descriptor N a data structure in the OS, in the process table N See slides starting at §34. Introduction What is a process? What is a process? — 2 What is a thread? Program counter Environment of a process Permissions of a Process Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 3/112 What is a thread? I I I I I I A thread is a lightweight process N Takes less CPU power to start, stop Part of a single process Shares address space with other threads in the same process Threads can share data more easily than processes Sharing data requires synchronisation, i.e., locking — see slide 95. This shared memory space can lead to complications in programming: “Threads often prevent abstraction. In order to prevent deadlock. you often need to know how and if the library you are using uses threads in order to avoid deadlock problems. Similarly, the use of threads in a library could be affected by the use of threads at the application layer.” – David Korn Introduction What is a process? What is a process? — 2 What is a thread? Program counter Environment of a process Permissions of a Process Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References See page 180, ESR in references, §112. OSSI — ver. 1.5 Processes - p. 4/112 Program counter I I I I The code of a process occupies memory The Program counter (PC) is a CPU register PC holds a memory address. . . . . . of the next instruction to be fetched and executed Introduction What is a process? What is a process? — 2 What is a thread? Program counter Environment of a process Permissions of a Process Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 5/112 Environment of a process I I I The environment is a set of names and values Examples: PATH=/usr/bin:/bin:/usr/X11R6/bin HOME=/home/nicku SHELL=/bin/bash In Linux shell, can see environment by typing: $ set Introduction What is a process? What is a process? — 2 What is a thread? Program counter Environment of a process Permissions of a Process Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 6/112 Permissions of a Process I I I I A process executes with the permissions of its owner N The owner is the user that starts the process A Linux process can execute with permissions of another user or group If it executes as the owner of the program instead of the owner of the process, it is called set user ID Similarly for set group ID programs Introduction What is a process? What is a process? — 2 What is a thread? Program counter Environment of a process Permissions of a Process Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 7/112 Multitasking I I Our lab PCs have one main CPU N But multiprocessor machines are becoming increasingly common N Linux 2.6.x kernel scales to 16 CPUs How execute many processes “at the same time”? Introduction Multitasking Multitasking Multitasking — 2 Multitasking — 3 Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 8/112 Multitasking — 2 I I I I CPU rapidly switches between processes that are “ready to run” Really: only one process runs at a time Change of process called a context switch N See slide §36 With Linux: see how many context switches/second using vmstat under “system” in column “cs” Introduction Multitasking Multitasking Multitasking — 2 Multitasking — 3 Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 9/112 Multitasking — 3 I This diagram shows how the scheduler gives a “turn” on the CPU to each of four processes that are ready to run Introduction Multitasking Multitasking Multitasking — 2 Multitasking — 3 process D C B A CPU executes process Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads time context switches OSSI — ver. 1.5 Race Condition Synchronisation Summary and References Processes - p. 10/112 Birth of a Process I I I I I In Linux, a process is born from a fork() system call N A system call is a function call to an operating system service provided by the kernel Each process has a parent The parent process calls fork() The child inherits (but cannot change) the parent environment, open files Child is identical to parent, except for return value of fork(). N Parent gets child’s process ID (PID) N Child gets 0 Introduction Multitasking Start of Process Birth of a Process Process tree Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 11/112 Process tree I I I Processes may have parents and children Gives a family tree In Linux, see this with commands: $ pstree or $ ps axf Introduction Multitasking Start of Process Birth of a Process Process tree Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 12/112 Scheduler I I I OS decides when to run each process that is ready to run (“runable”) The part of OS that decides this is the scheduler Scheduler aims to: N Maximise CPU usage N Maximise process completion N Minimise process execution time N Minimise waiting time for ready processes N Minimise response time Introduction Multitasking Start of Process Scheduler Scheduler When to Switch Processes? Scheduling statistics: vmstat Interrupts Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 13/112 When to Switch Processes? I I The scheduler may change a process between executing (or running) and ready to run when any of these events happen: N clock interrupt N I/O interrupt N Memory fault N trap caused by error or exception N system call See slide §17 showing the running and ready to run process states. Introduction Multitasking Start of Process Scheduler Scheduler When to Switch Processes? Scheduling statistics: vmstat Interrupts Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 14/112 Scheduling statistics: vmstat I I The “system” columns give statistics about scheduling: N “cs” — number of context switches per second N “in” — number of interrupts per second See slide §36, man vmstat Introduction Multitasking Start of Process Scheduler Scheduler When to Switch Processes? Scheduling statistics: vmstat Interrupts Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 15/112 Interrupts I I I Will discuss interrupts in more detail when we cover I/O An interrupt is an event (usually) caused by hardware that causes: N Saving some CPU registers N Execution of interrupt handler N Restoration of CPU registers An opportunity for scheduling Introduction Multitasking Start of Process Scheduler Scheduler When to Switch Processes? Scheduling statistics: vmstat Interrupts Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 16/112 Process States Introduction Multitasking Start of Process waiting for input Running scheduler chooses this process scheduler chooses another process Ready input available Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References Blocked OSSI — ver. 1.5 Processes - p. 17/112 What is Most Common State? I I I Now, my computer has 160 processes. How many are running, how many are ready to run, how many are blocked? What do you expect is most common state? Introduction Multitasking Start of Process Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 18/112 Most Processes are Blocked 9:41am up 44 days, 20:12, 1 user, load average: 2.02, 2.06, 2.13 160 processes: 145 sleeping, 2 running, 13 zombie, 0 stopped Introduction Multitasking Start of Process Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References I I I I I I Here you see that most are sleeping, waiting for input! Most processes are “I/O bound”; they spend most time waiting for input or waiting for output to complete With one CPU, only one process can actually be running at one time However, surprisingly few processes are ready to run The load average is the average number of processes that are in the ready to run state. In output from the top program above, see over last 60 seconds, there are 2.02 processes on average in RTR state OSSI — ver. 1.5 Processes - p. 19/112 Linux Process States stopped Introduction Multitasking Start of Process running state Scheduler Process States Process States What is Most Common State? creation ready to run scheduling wait for event event executing zombie Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks signal or event System Calls uninterruptible A shell program IPC Threads Race Condition interruptible Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 20/112 Linux Process States — 2 I I I Running — actually contains two states: N executing, or N ready to execute Interruptable — a blocked state N waiting for event, such as: I end of an I/O operation, I availability of a resource, or I a signal from another process Uninterruptable — another blocked state N waiting directly on hardware conditions N will not accept any signals (even SIGKILL) Introduction Multitasking Start of Process Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 21/112 Linux Process States — 3 I I Stopped — process is halted N can be restarted by another process N e.g., a debugger can put a process into stopped state Zombie — a process has terminated N but parent did not wait() for it (see slide 65) Introduction Multitasking Start of Process Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 22/112 Process States: vmstat I I I The “procs” columns give info about process states: “r” — number of processes that are in the ready to run state “b” — number of processes that are in the uninterruptable blocked state Introduction Multitasking Start of Process Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 23/112 Tools for monitoring processes I I I I I I Linux provides: vmstat N Good to monitor over time: $ vmstat 5 procinfo N Easier to understand than vmstat N Monitor over time with $ procinfo -f View processes with top — see slides 27 to §30 The system monitor sar shows data collected over time: See man sar; investigate sar -c and sar -q See the utilities in the procps software package. You can list them with $ rpm -ql procps w ps slabtop top pkill watch free uptime snice pmap pgrep vmstat tload skill Introduction Multitasking Start of Process Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 24/112 Monitoring processes in Win 2000 I I I Windows 2000 provides a tool: Start → Administrative Tools → Performance. Can use this to monitor various statistics Introduction Multitasking Start of Process Scheduler Process States Process States What is Most Common State? Most Processes are Blocked Linux Process States Linux Process States — 2 Linux Process States — 3 Process States: vmstat Tools for monitoring processes Monitoring processes in Win 2000 top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 25/112 Introduction Multitasking Start of Process Process Monitoring with top Scheduler Process States top Process Monitoring — top load average top: process states top and memory Virtual Memory: suspended processes Suspended Processes Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 26/112 Process Monitoring — top 08:12:13 up 1 day, 13:34, 8 users, load average: 0.16, 0.24, 0.49 111 processes: 109 sleeping, 1 running, 1 zombie, 0 stopped CPU states: cpu user nice system irq softirq iowait idle total 0.0% 0.0% 3.8% 0.0% 0.0% 0.0% 96.1% Mem: 255608k av, 245064k used, 10544k free, 0k shrd, 17044k buff 152460k active, 63236k inactive Swap: 1024120k av, 144800k used, 879320k free 122560k cached PID 1253 1769 23548 1 2 3 4 6 5 USER root nicku nicku root root root root root root PRI 15 16 16 16 15 15 34 15 15 NI SIZE RSS SHARE STAT %CPU %MEM 0 73996 13M 11108 S 2.9 5.5 0 2352 1588 1488 S 1.9 0.6 0 1256 1256 916 R 1.9 0.4 0 496 468 440 S 0.0 0.1 0 0 0 0 SW 0.0 0.0 0 0 0 0 SW 0.0 0.0 19 0 0 0 SWN 0.0 0.0 0 0 0 0 SW 0.0 0.0 0 0 0 0 SW 0.0 0.0 TIME CPU COMMAND 19:09 0X 2:10 0 magicdev 0:00 0 top 0:05 0 init 0:00 0 keventd 0:00 0 kapmd 0:00 0 ksoftirqd/0 0:00 0 bdflush 0:11 0 kswapd OSSI — ver. 1.5 Processes - p. 27/112 top: load average 08:12:13 up 1 day, 13:34, 8 users, load average: 0.16, 0.24, 0.49 Introduction Multitasking Start of Process Scheduler Process States top Process Monitoring — top load average top: process states I I I load average is measured over the last minute, five minutes, fifteen minutes Over that time is the average number of processes that are ready to run, but which are not executing A measure of how “busy” a computer is. top and memory Virtual Memory: suspended processes Suspended Processes Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 28/112 top: process states 111 processes: 109 sleeping, 1 running, 1 zombie, 0 stopped Introduction Multitasking Start of Process Scheduler Process States top Process Monitoring — top load average top: process states sleeping I/O running zombie Most processes (109/111) are sleeping, waiting for This is the number of processes that are both ready to run and are executing There is one process here that has terminated, but its parent did not wait() for it. I The wait() system calls are made by a parent process, to get the exit() status of its child(ren). I This call removes the process control block from the process table, and the child process does not exist any more. (§34) ¨  stopped When you press  Control-z © a shell, you will in increase this number by 1 top and memory Virtual Memory: suspended processes Suspended Processes Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 29/112 top: Processes and Memory PID USER 1253 root PRI 15 NI SIZE 0 73996 RSS SHARE STAT %CPU %MEM 13M 11108 S 2.9 5.5 TIME CPU COMMAND Introduction 19:09 0X Multitasking Start of Process Scheduler Process States top Process Monitoring — top load average top: process states SIZE This column is the total size of the process, including the part which is swapped (paged out) out to the swap partition or swap file Here we see that the process X uses a total of 73,996 Kb, i.e., 73,996 × 1024 bytes ≈ 72MB, where here 1MB = 220 bytes. top and memory Virtual Memory: suspended processes Suspended Processes Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation RSS The resident set size is the total amount of RAM that a process uses, including memory shared with other processes. Here X uses a total of 13MB RAM, including RAM shared with other processes. The amount of shared memory is the amount of RAM that this process shares with other processes. Here X shares 11,108 KB with other processes. SHARE We can see that the total amount of RAM used exclusively by one process is rss − share. Here we see that X uses about 13 × 220 − 11,108 × 210 ≈ 2 MB OSSI — ver. 1.5 Summary and References Processes - p. 30/112 Virtual Memory: suspended processes I I I I With memory fully occupied by processes, could have all in blocked state! CPU could be completely idle, but other processes waiting for RAM Solution: virtual memory N will discuss details of VM in memory management lecture Part or all of process may be saved to swap partition or swap file Introduction Multitasking Start of Process Scheduler Process States top Process Monitoring — top load average top: process states top and memory Virtual Memory: suspended processes Suspended Processes Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 31/112 Suspended Processes I Could add more states to process state table: N ready and suspended N blocked and suspended Introduction Multitasking Start of Process Scheduler Process States top Process Monitoring — top load average top: process states top and memory Virtual Memory: suspended processes Suspended Processes Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 32/112 Introduction Multitasking Start of Process Process Control Blocks The Process Table Data structure in OS to hold information about a process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 33/112 OS Process Control Structures I I I I Every OS provides process tables to manage processes In this table, the entries are called process control blocks (PCBs), process descriptors or task descriptors. We will use the abbreviation PCB. There is one PCB for each process in Linux, PCB is called task_struct, defined in include/linux/sched.h N In a Fedora Core or Red Hat system, you will find it in the file /usr/src/linux-2.*/include/linux/sched.h if you have installed the kernel-source software package Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 34/112 What is in a PCB I I In slide §3, we saw that a PCB contains: N a process ID (PID) N process state (i.e., executing, ready to run, sleeping waiting for input, stopped, zombie) N program counter, the CPU register that holds the address of the next instruction to be fetched and executed N The value of other CPU registers the last time the program was switched out of executing by a context switch — see slide §36 N scheduling priority N the user that owns the process N the group that owns the process N pointers to the parent process, and child processes N Location of process’s data and program code in memory N List of allocated resources (including open files) PCB holds the values as they were when process was last switched out of executing by a context switch — see slide §36 Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 35/112 Context Switch I OS I I does a context switch when: N stop current process from executing, and N start the next ready to run process executing on CPU OS saves the execution context (see §37) to its PCB OS loads the ready process’s execution context from its PCB Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References I I When does a context switch occur? N When a process blocks, i.e., goes to sleep, waiting for input or output (I / O), or N When the scheduler decides the process has had its turn of the CPU, and it’s time to schedule another ready-to-run process A context switch must be as fast as possible, or multitasking will be too slow N Very fast in Linux OS OSSI — ver. 1.5 Processes - p. 36/112 Execution Context I I I Also called state of the process (but since this term has two meanings, we avoid that term here), process context or just context The execution context is all the data that the OS must save to stop one process from executing on a CPU, and load to start the next process running on a CPU This includes the content of all the CPU registers, the location of the code, . . . N Includes most of the contents of the process’s PCB. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 37/112 Program Counter in PCB I I I What value is in the program counter in the PCB? If it is not executing on the CPU, N The address of the next CPU instruction that will be fetched and executed the next time the program starts executing If it is executing on the CPU, N The address of the first CPU instruction that was fetched and executed when the process began executing at the last context switch (§36) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 38/112 Process Control Blocks—Example I I I I The diagram in slide §40 shows three processes and their process control blocks. There are seven snapshots t0 , t1 , t2 , t3 , t4 , t5 and t6 at which the scheduler has changed process (there has been a context switch—§36) On this particular example CPU, all I / O instructions are 2 bytes long The diagram also shows the queue of processes in the: N Ready queue (processes that are ready to run, but do not have a CPU to execute on yet) N Blocked, or Wait queue, where the processes have been blocked because they are waiting for I / O to finish. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 39/112 PCB Example: Diagram CPU idle t0 t1 t2 t3 t4 t5 t6 time Ready Queue: Blocked Queue: P3 P2 P3 P1 P1 P2 P2 P3 P3 PCB for P1 0xCAFE Running 0xC0DE Blocked 0xC0DE Ready 0xC0DE Running Process 1 has terminated; It’s PCB has been freed ents PCB for P2 0xFACE Ready 0xFACE Running 0xFEED Blocked 0xFEED Ready 0xFEED Running Process 2 has terminated PCB is freed PCB for P3 0xDEAF Ready 0xDEAF Ready 0xDEAF Running 0xD1CE Blocked 0xD1CE Ready 0xD1CE Running P3 has exited; PCB freed OSSI — ver. 1.5 Processes - p. 40/112 PCB Example — Continued I I I In slide §40, N The times t0 , t1 , t2 , t3 , t4 , t5 and t6 are when the scheduler has selected another process to run. N Note that these time intervals are not equal, they are just the points at which a scheduling change has occurred. Each process has stopped at one stage to perform I / O N That is why each one is put on the wait queue once during its execution. Each process has performed I / O once Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 41/112 What is the address of I/O instructions? I We are given that all I / O instructions in this particular example are two bytes long (slide §39) N We can see that when the process is sleeping (i.e., blocked), then the program counter points to the instruction after the I / O instruction N So for process P1, which blocks with program counter PC = C0DE16 , the I / O instruction is at address C0DE16 − 2 = C0DC16 N for process P2, which blocks with program counter PC = FEED16 , the I / O instruction is at address FEED16 − 2 = FEEB16 N for process P3, which blocks with program counter PC = D1CE16 , the I / O instruction is at address D1CE16 − 2 = D1CC16 Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks OS Process Control Structures What is in a PCB Context Switch Execution Context Program Counter in PCB PCB Example PCB Example Diagram PCB Example — Continued Address of I/O instructions System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 42/112 Introduction Multitasking Start of Process Process System Calls How the OS controls processes How you use the OS to control processe Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 43/112 A shell program Major process Control System Calls I I I I fork() — start a new process execve() — replace calling process with machine code from another program file wait(), waitpid() — parent process gets status of its’ child after the child has terminated, and cleans up the process table entry for the child (stops it being a zombie) exit() — terminate the current process Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 44/112 A shell program File I/O system calls: a sidetrack #include ssize_t read( int filedes, void *buf, size_t nbytes ); returns number of bytes read, 0 at end of file, −1 on error ssize_t write( int filedes, void *buf, size_t nbytes ); returns number of bytes written, else −1 on error Note: these are unbuffered, that is, they have effect “immediately”. This is different from stdio.h functions, which are buffered for efficiency. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does I I I I Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 45/112 A shell program Process IDs and init I I I I I Every process has a process ID (PID) process 0 is the scheduler, part of kernel process 1 is init, the parent of all other processes N a normal user process, not part of kernel N program file is /sbin/init All other processes result from init calling the fork() system call This is the only way a new process is created by the kernel Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 46/112 A shell program SUID, SGID and IDs I I I I Every process has six or more IDs associated with it UID and GID of person who executes program file: N real user ID, real group ID IDs used to calculate permissions: N Effective UID, Effective GID IDs saved when use exec() system call: N Saved set-user-ID, saved set-group-ID N idea is can drop special privileges and return to executing with real UID and real GID when privilege is no longer required Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 47/112 A shell program Other system calls: getting process info #include #include pid_t getpid(void); returns PID of calling process pid_t getppid(void); returns PID of parent uid_t getuid(void); returns real user ID of process uid_t geteuid(void); returns effective user ID of process gid_t getgid(void); returns real group ID of process gid_t getegid(void); returns effective group ID of process Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call I I I I I I Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 48/112 A shell program fork(): what it does #include #include pid_t fork(void); returns 0 in child returns PID of child in parent returns −1 if error Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call I I I Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 49/112 A shell program Using fork(): pseudocode ( ( pid = fork() ) < 0 ) fork_error has happened else if ( pid == 0 ) /∗ I am the child ∗/ do things the child process should do /∗ I am the parent ∗/ else do things the parent should do if Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 50/112 A shell program Simple fork() Example (no Checking) #include #include int Introduction Multitasking Start of Process { main() pid = fork(); printf( "PID is %d\n", pid ); if ( pid == 0 ) printf( "I’m the child\n" ); int else Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call } printf( "I’m the parent\n" ); Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 51/112 A shell program An example using fork() #include #include #include int glob = 6; char buf[] = "a write to standard output\n"; int main( void ) { int var = 88; /∗ local variable on the stack ∗/ pid_t pid; if ( write( STDOUT_FILENO, buf, sizeof (buf) − 1 ) = sizeof( buf ) − 1 ) { fprintf( stderr, "write error" ); exit( 1 ); } Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 52/112 A shell program Example using fork()—(contd.) printf( "before fork\n" ); if ( ( pid = fork() ) < 0 ) { fprintf( stderr, "fork error\n" ); exit( 1 ); } else if ( pid == 0 ) { ++glob; ++var; } else sleep( 2 ); /∗ parent ∗/ printf( "pid = %d, glob = %d, var = %d\n", getpid(), glob, var ); exit( 0 ); } Introduction Multitasking Start of Process Scheduler /∗ child ∗/ Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 53/112 A shell program Output of fork-example.c: $ gcc -o fork-example fork-example.c $ ./fork-example a write to standard output before fork pid = 7118, global = 7, var = 89 child’s vars changed pid = 7117, global = 6, var = 88 parent’s copy not changed Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 54/112 A shell program Running fork-example again $ ./fork-example > tmp.out $ cat tmp.out a write to standard output before fork pid = 7156, global = 7, var = 89 before fork pid = 7155, global = 6, var = 88 Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 55/112 A shell program Why two “before fork” messages? I I I I write() system call not buffered write() called before fork(), so one output printf() is buffered N line buffered if connected to terminal N fully buffered otherwise; parent and child both have a copy of the unwritten buffer when redirected exit() causes both parent and child buffers to flush Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 56/112 A shell program So what does this show? I I I I I It shows that the child is an exact copy of the parent, with all variable values, buffers, open files,. . . All are inherited by the child Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 57/112 A shell program Running another program — exec() I I I To run another program file first call fork() to create a child process child calls exec() to replace current copy of parent with a totally new program in execution Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 58/112 A shell program execve() system call #include int execve( const char ∗filename, char ∗const argv[], char ∗const envp[] ); executes the program filename, replaces current process Passes the command line in argv[] passes the environment variables in envp[] Does not return, unless error, when returns with −1 Usually called through library exec*() calls — see man 3 exec Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call I I I I I Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 59/112 A shell program fork() — exec() Example #include #include int Introduction Multitasking Start of Process main() pid = fork(); printf( "PID is %d\n", pid ); if ( pid == 0 ) printf( "I’m the child\n" ); int else Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example { printf( "I’m the parent\n" ); } Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 60/112 A shell program Using execl() int execl( const char *path, const char *arg, ... ); Parameter number: 1. gives full path of the program file you want to execute 2. gives name of the new process 3. specifies the command line arguments you pass to the program 4. last is a NULL pointer to end the parameter list. We must always put a NULL pointer at the end of this list. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example I I Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 61/112 A shell program print.c: a program we call #include #include int Introduction Multitasking Start of Process { main( int argc, char ∗argv[] ) // argv[0] is the program name int num = atoi( argv[1] ); int loops = atoi( argv[2] ); int i; for ( i = 0; i < loops; ++i ) printf( "%d ", num ); Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call } Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 62/112 A shell program Calling ./print using execl() #include #include int Introduction Multitasking Start of Process Scheduler main() printf( "hello world\n" ); int pid = fork(); printf( "fork returned %d\n", pid ); if ( pid == 0 ) execl( "./print", "print", "1", "100", NULL ); else { Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call execl( "./print", "print", "2", "100", NULL ); } Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 63/112 A shell program vfork() sytem call I I I I I A lightweight fork() Designed for running execvp() straight after N modern Linux fork() is very efficient when call exec*() Child does not contain an exact copy of parent address space; child calls exec() or exit() after fork() parent is suspended till child calls fork() or exit() Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 64/112 A shell program wait(), waitpid() system calls #include #include pid_t wait( int *status ); pid_t waitpid( pid_t pid, int *status, int options ); return process ID if OK, 0, or −1 on error Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call I Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 65/112 A shell program wait(), waitpid() system calls I I I I wait() can block caller until child process terminates waitpid() has option to prevent blocking waitpid() can wait for a specific child instead of the first child if child has terminated already (it’s a zombie), wait returns immediately, cleaning up the process table data structures for the child Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls System Calls File I/O system calls: a sidetrack init SUID, SGID and IDs Other system calls: getting process info fork(): what it does Using fork(): pseudocode Simple fork() Example (no Checking) An example using fork() Example using fork()—(contd.) Output of fork-example.c: Running fork-example again Why two “before fork” messages? So what does this show? Running another program — exec() execve() system call fork() — exec() Example Using execl() print.c: a program we call Calling ./print using execl() vfork() sytem call wait(), waitpid() system calls wait(), waitpid() system calls OSSI — ver. 1.5 Processes - p. 66/112 A shell program Part of Simple Shell Program int { main( int argc, char ∗∗argv ) ∗prog_name = basename( ∗argv ); print_prompt( prog_name ); read_command(); for ( ;; ) { int pid = fork(); if ( pid == 0 ) { execvp( args[ 0 ], args ); } wait( NULL ); print_prompt( prog_name ); read_command(); } char Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program Part of Simple Shell Program Windows and Processes Windows and Processes — 2 CreateProcess() prototype CreateProcess() Example: CreateProcess() Processes in Linux, Unix, Windows IPC Threads Race Condition Synchronisation Summary and References } OSSI — ver. 1.5 Processes - p. 67/112 Windows and Processes I I I Windows provides a Win32 API call to create a process: CreateProcess() Creates a new process, loads program into that process CreateProcess() takes ten parameters Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program Part of Simple Shell Program Windows and Processes Windows and Processes — 2 CreateProcess() prototype CreateProcess() Example: CreateProcess() Processes in Linux, Unix, Windows IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 68/112 Windows and Processes — 2 I I I Win32 uses handles for almost all objects such as files, pipes, sockets, processes and events handles can be inherited from parent No proper parent-child relationship N caller of CreateProcess() could be considered as parent N but child cannot determine it’s parent Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program Part of Simple Shell Program Windows and Processes Windows and Processes — 2 CreateProcess() prototype CreateProcess() Example: CreateProcess() Processes in Linux, Unix, Windows IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 69/112 CreateProcess() prototype I I CreateProcess() is much more complicated than pid_t fork( void ); Four of the parameters point to structs, e.g., N LPSTARTUPINFO points to a struct with 4 members N LPPROCESS_INFORMATION points to a struct with 18 members! BOOL CreateProcess ( LPCTSTR lpApplicationName, // pointer to executable module LPTSTR lpCommandLine, // pointer to command line string LPSECURITY_ATTRIBUTES lpProcessAttrib, // process security LPSECURITY_ATTRIBUTES lpThreadAttrib, // thread security BOOL bInheritHandles, // handle inheritance flag DWORD dwCreationFlags, // creation flags LPVOID lpEnvironment, // pointer to new environment block LPCTSTR lpCurrentDirectory, // pointer to current dir name LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO LPPROCESS_INFORMATION lpProcessInformation // pointer to // PROCESS_INFORMATION ); OSSI — ver. 1.5 Processes - p. 70/112 CreateProcess() I I Can Specify Program in either 1st or 2nd parameter: N first: location of program to execute N second: command line to execute Creation flags: N if 0, runs in existing window Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program Part of Simple Shell Program Windows and Processes Windows and Processes — 2 CreateProcess() prototype CreateProcess() Example: CreateProcess() Processes in Linux, Unix, Windows IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 71/112 Example: CreateProcess() #include #include void Introduction Multitasking Start of Process main() { Scheduler STARTUPINFO si; PROCESS_INFORMATION pi; Process States memset( &si, 0, sizeof( si ) ); top si.cb = sizeof(si); Process Control Blocks if ( ! CreateProcess( NULL, System Calls "..\\..\\print\\Debug\\print.exe 5 100", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) ) A shell program Part of Simple Shell Program fprintf( stderr, "CreateProcess failed with %d\n", GetLastError() ); Windows and Processes WaitForSingleObject( pi.hProcess, INFINITE ); Windows and Processes — 2 CreateProcess() prototype CloseHandle( pi.hProcess ); CreateProcess() CloseHandle( pi.hThread ); Example: CreateProcess() Processes in Linux, Unix, Windows IPC Threads Race Condition Synchronisation Summary and References } OSSI — ver. 1.5 Processes - p. 72/112 Processes in Linux, Unix, Windows I I I I I Linux often provides 2 or more processes per application Example: apache web server parent process watches for connections, one child process per client Linux processes have much less overhead than in Windows fork() — exec() very efficient POSIX threads are very efficient, and faster than fork() — exec() I I I Windows have one process per application, but often 2 or more threads Windows CreateProcess() takes more time than fork() — exec() CreateThread() takes very much less time than CreateProcess() Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program Part of Simple Shell Program Windows and Processes Windows and Processes — 2 CreateProcess() prototype CreateProcess() Example: CreateProcess() Processes in Linux, Unix, Windows IPC Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 73/112 Introduction Multitasking Start of Process IPC Inter Process Communication How Processes can Talk to Each Other Scheduler Process States top Process Control Blocks System Calls A shell program IPC Problem with Processes Interprocess Communication (IPC) IPC — Shared Memory IPC — Signals Signals and the Shell Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 74/112 Problem with Processes I I I I I Communication! Processes cannot see the same variables Must use Inter Process Communication (IPC) IPC Techniques include: N pipes, and named pipes (FIFOs) N sockets N messages and message queues N shared memory regions All have some overhead Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Problem with Processes Interprocess Communication (IPC) IPC — Shared Memory IPC — Signals Signals and the Shell Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 75/112 Interprocess Communication (IPC) I I Pipe — circular buffer, can be written by one process, read by another N related processes can use unnamed pipes I used in shell programming, e.g., the vertical bar ‘|’ in $ find /etc | xargs file N unrelated processes can use named pipes — sometimes called FIFOs Messages — POSIX provides system calls msgsnd() and msgrcv() N message is block of text with a type N each process has a message queue, like a mailbox N processes are suspended when attempt to read from empty queue, or write to full queue. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Problem with Processes Interprocess Communication (IPC) IPC — Shared Memory IPC — Signals Signals and the Shell Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 76/112 IPC — Shared Memory I I I Shared Memory — a Common block of memory shared by many processes Fastest way of communicating Requires synchronisation (See slide 95) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Problem with Processes Interprocess Communication (IPC) IPC — Shared Memory IPC — Signals Signals and the Shell Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 77/112 IPC — Signals I I I I I A process sends a signal to another process using the kill() system call signals are implemented as single bits in a field in the PCB, so cannot be queued A process may respond to a signal with: N a default action (usually process terminates) N a signal handler function (see trap in shell programming notes), or N ignore the signal (unless it is SIGKILL or SIGSTOP) A process cannot ignore, or handle a SIGSTOP or a SIGKILL signal. N A KILL signal will always terminate a process (unless it is in interruptible sleep) N A SIGSTOP signal will always send a process into the stopped state. Some signals can be generated from the keyboard, i.e., ¨  ¨  Control-C © interrupt (SIGINT);  — Control-\ © quit —  ¨  Control-Z © stop (SIGSTOP) — (SIGQUIT),  Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Problem with Processes Interprocess Communication (IPC) IPC — Shared Memory IPC — Signals Signals and the Shell Threads Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 78/112 Signals and the Shell I I I I We can use the kill built in command to make the kill() system call to send a signal A shell script uses the trap built in command to handle a signal Ignoring the signals SIGINT, SIGQUIT and SIGTERM: trap "" INT QUIT TERM Handling the same signals by printing a message then exiting: trap "echo ’Got a signal; exiting.’;exit 1" INT QUIT TERM Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Problem with Processes Interprocess Communication (IPC) IPC — Shared Memory IPC — Signals Signals and the Shell Threads Race Condition Synchronisation Summary and References I I Handling the same signals with a function call: signal_handler() { echo "Received a signal; terminating." rm -f $temp_file exit 1 } trap signal_handler INT QUIT TERM Sending a SIGKILL signal to process with PID 3233: $ kill -KILL 3233 OSSI — ver. 1.5 Processes - p. 79/112 Introduction Multitasking Start of Process Threads Lightweight processes that can talk to each other easily Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 80/112 Threads and Processes I I I I Threads in a process all share the same address space Communication easier Overhead less Problems of locking and deadlock a major issue I I I I Processes have separate address spaces Communication more indirect: IPC (Inter Process Communication) Overhead higher Less problem with shared resources (since fewer resources to share!) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 81/112 Threads have own. . . I I I I I stack pointer register values scheduling properties, such as policy or priority set of signals they can each block or receive own stack data (local variables are local to thread) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 82/112 Threads share a lot I I I Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads. Two pointers having the same value point to the same data. A number of threads can read and write to the same memory locations, and so you need to explicitly synchronise access Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 83/112 Threads in Linux, Unix I POSIX I I I is a standard for Unix Linux implements POSIX threads On Red Hat 8.x, documentation is at $ info ’(libc) POSIX Threads’ N or in Emacs, C-H m libc then middle-click on POSIX threads Provides: N semaphores, N mutexes and N condition variables for locking (synchronisation) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 84/112 hello.c: a simple threaded program #include #include #define NUM_THREADS 5 void ∗ print_hello( void ∗threadid ) { printf( "\n%d: Hello World!\n", threadid ); pthread_exit( NULL ); } int main() { pthread_t threads[ NUM_THREADS ]; int rc, t; for ( t = 0; t < NUM_THREADS; t++ ) { printf( "Creating thread %d\n", t ); rc = pthread_create( &threads[ t ], NULL, print_hello, ( void ∗ ) t ); if ( rc ) { printf( "ERROR; pthread create() returned %d\n", rc ); exit( −1 ); } } pthread_exit( NULL ); } Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 85/112 How to Compile a POSIX Threads Program I I I Need to use the libpthread library N Specify this with the option -lpthread Need to tell the other libraries that they should be reentrant (or “thread safe”) N This means that the library uses no static variables that may be overwritten by another thread N Specify this with the option -D_REENTRANT So, to compile the program program .c, do: $ gcc -D_REENTRANT -lpthread -o program program Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program .c Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 86/112 pthread_create() #include Introduction Multitasking Start of Process void * Scheduler pthread_create( pthread_t *thread, Process States pthread_attr_t *attr, void *(*start_routine)(void *),top Process Control Blocks void *arg ); System Calls returns: 0 if successfully creates thread A shell program returns error code otherwise IPC I I Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 87/112 pthread_create() I I I Quite different from fork() Thread must always execute a user-defined function parameters: 1. pointer to thread identifier 2. attributes for thread, including stack size 3. user function to execute 4. parameter passed to the user function Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 88/112 Problem with threads: I I I I I Avoid 2 or more threads writing or reading and writing same data at the same time Avoid data corruption Need to control access to data, devices, files Need locking Provide three methods of locking: N mutex (mutual exclusion) N semaphores N condition variables Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Threads and Processes Threads have own. . . Threads share a lot Threads in Linux, Unix hello.c: a simple threaded program Compile POSIX Threads pthread_create() pthread_create() Problem with threads: Race Condition Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 89/112 Introduction Multitasking Start of Process Race Condition Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Race Conditions Critical Sections Race Condition — one possibility Example — another possibility Solution: Synchronisation File Locking Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 90/112 Race Conditions race condition — where outcome of computation depends on sheduling I an error in coding I Example: two threads both access same list with code like this: if ( list.numitems > 0 ) { // Oh, dear, better not change to // other thread here! remove_item( list ); // not here! // ...and not here either: --list.numitems; } I Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Race Conditions Critical Sections Race Condition — one possibility Example — another possibility Solution: Synchronisation File Locking Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 91/112 Critical Sections I I I I I critical resource — a device, file or piece of data that cannot be shared critical section — part of program only one thread or process should access contains a critical resource N i.e., you lock data, not code All the code in the previous slide is a critical section Consider the code: very_important_count++; executed by two threads on a multiprocessor machine (SMP = symmetric multiprocessor) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Race Conditions Critical Sections Race Condition — one possibility Example — another possibility Solution: Synchronisation File Locking Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 92/112 Race Condition — one possibility Introduction thread 1 read very_important_count (5) add 1 (6) write very_important_count (6) thread 2 Multitasking Start of Process Scheduler Process States top Process Control Blocks read very_important_count (6) add 1 (7) write very_important_count (7) System Calls A shell program IPC Threads Race Condition Race Conditions Critical Sections Race Condition — one possibility Example — another possibility Solution: Synchronisation File Locking Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 93/112 Example — another possibility Introduction thread 1 read very_important_count (5) thread 2 Multitasking Start of Process Scheduler read very_important_count (5) add 1 (6) add 1 (6) write very_important_count (6) write very_important_count (6) Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Race Conditions Critical Sections Race Condition — one possibility Example — another possibility Solution: Synchronisation File Locking Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 94/112 Solution: Synchronisation I I I Solution is to recognise critical sections use synchronisation, i.e., locking, to make sure only one thread or process can enter critical region at one time. Methods of synchronisation include: N file locking N semaphores N monitors N spinlocks N mutexes Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Race Conditions Critical Sections Race Condition — one possibility Example — another possibility Solution: Synchronisation File Locking Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 95/112 File Locking I I I For example, an flock() system call can be used to provide exclusive access to an open file The call is atomic N It either: I completely succeeds in locking access to the file, or I it fails to lock access to the file, because another thread or process holds the lock I No “half-locked” state N No race condition Alternatives can result in race conditions; for example: N thread/process 1 checks lockfile N thread/process 2 checks lockfile a very short time later N both processes think they have exclusive write access to the file N file is corrupted by two threads/processes writing to it at the same time Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Race Conditions Critical Sections Race Condition — one possibility Example — another possibility Solution: Synchronisation File Locking Synchronisation Summary and References OSSI — ver. 1.5 Processes - p. 96/112 Introduction Multitasking Start of Process Methods of Synchronisation What is it? mutex, semaphore, condition variables, monitor, spinlock Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References OSSI — ver. 1.5 Processes - p. 97/112 Synchronisation I I I Synchronisation is a facility that enforces N mutual exclusion and N event ordering Required when multiple active processes or threads can access shared address spaces or shared I / O resources even more critical for SMP (Symmetric Multiprocessor) systems N kernel can run on any processor N all processors are of equal importance (there is no one CPU that is the “boss”) N SMP systems include PCs with more than one CPU, as you might find in the Golden Shopping Centre Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References OSSI — ver. 1.5 Processes - p. 98/112 Semaphores I I A variable with three opererations: N initialise to non-negative value N down (or wait) operation: I decrement variable I if variable becomes negative, then process or thread executing the down operation is blocked I has nothing to do with the wait system call for a parent process to get status of its child N up (or signal) operation: I increment the semaphore variable; I if value is not positive, then a process or thread blocked by a down operation is unblocked. A semaphore also has a queue to hold processes or threads waiting on the semaphore. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References OSSI — ver. 1.5 Processes - p. 99/112 Semaphores — 2 I I The up and down semaphore operations are atomic N the up and down operations cannot be interrupted N each routine is a single, indivisible step Using semaphores—pseudocode /∗ only one process can enter critical section at one time: ∗/ semaphore s = 1; down( s ); /∗ critical section ∗/ up( s ); Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References I Initialise semaphore to number of processes allowed into critical section at one time OSSI — ver. 1.5 Processes - p. 100/112 Mutex—POSIX and Win32 Threads I I I I mutual exclusion Easier to use than semaphores (see slide 99) When only one thread or process needs to write to a resource N all other writers refused access A special form of the more general semaphore N Can have only two values; N sometimes called binary semaphores. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References OSSI — ver. 1.5 Processes - p. 101/112 mutex — I POSIX Threads Example (1) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls It is good practice to put the mutex together with the data it proects I I have removed the error checking from this example to save space—in real code, always check library calls for error conditions #include #include pthread_mutex_t mutex; /∗ protects access to value ∗/ int value; /∗ Access protected by mutex ∗/ } data = { PTHREAD_MUTEX_INITIALIZER, 0 }; struct { A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References OSSI — ver. 1.5 Processes - p. 102/112 mutex — POSIX Threads Example (2) Introduction Multitasking #define NUM_THREADS 5 void ∗thread( void ∗t_id ) { int i; for ( i = 0; i < 200; ++i ) { pthread_mutex_lock( &data.mutex ); ++data.value; printf( "thread %d: data value = %d\n", t_id, data.value ); pthread_mutex_unlock( &data.mutex ); } pthread_exit( NULL ); Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References } OSSI — ver. 1.5 Processes - p. 103/112 mutex — int POSIX Threads Example (3) Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References main() { pthread_t threads[ NUM_THREADS ]; int rc, t; for ( t = 0; t < NUM_THREADS; t++ ) { printf( "Creating thread %d\n", t ); pthread_create( &threads[ t ], NULL, thread, ( void ∗ ) t ); } pthread_exit( NULL ); } OSSI — ver. 1.5 Processes - p. 104/112 POSIX I I Condition Variables Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References I I I Lets threads sleep till a condition about shared data is true Basic operations: N signal the condition (when condition is true) N wait for the condition I suspend the thread till another thread signals the condition Always associated with a mutex Very useful Missing from Windows: See http: //www.cs.wustl.edu/~schmidt/win32-cv-1.html OSSI — ver. 1.5 Processes - p. 105/112 Monitors I I I I A higher level structure for synchronisation Implemented in Java, and some libraries main characteristics: N data in monitor is accessible only to procedures in monitor N a process or thread enters monitor by executing one of its procedures N Only one process or thread may be executing in the monitor at one time. Can implement with mutexes and condition variables. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References OSSI — ver. 1.5 Processes - p. 106/112 Spinlocks I I I I I Used in operating system kernels in SMP systems Linux uses kernel spinlocks only for SMP systems a very simple single-holder lock if can’t get the spinlock, you keep trying (spinning) until you can. Spinlocks are: N very small and fast, and N can be used anywhere Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Synchronisation Semaphores Semaphores — 2 POSIX and Win32 Threads Example 1 Threads Example 2 Threads Example 3 Condition Variables Monitors Spinlocks Summary and References OSSI — ver. 1.5 Processes - p. 107/112 Introduction Multitasking Start of Process Summary and References Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References Summary — Process States, Scheduling Summary — Processes and Threads Summary — Synchronisation References OSSI — ver. 1.5 Processes - p. 108/112 Summary — Process States, Scheduling I I I Scheduler changes processes between ready to run and running states N context switch: when scheduler changes process or thread Most processes are blocked, i.e., sleeping: waiting for I / O N understand the process states N why a process moves from one state to another Communication between processes is not trivial; IPC methods include N pipes N shared memory N messages N signals N semaphores Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References Summary — Process States, Scheduling Summary — Processes and Threads Summary — Synchronisation References OSSI — ver. 1.5 Processes - p. 109/112 Summary — Processes and Threads I I I With Linux and Unix, main process system calls are fork(), exec() and wait() — understand the function of each of these Windows provides CreateProcess() and various WaitFor...() Win32 API calls N The WaitFor...() calls have a purpose similar to that of the wait() system call in Linux and Unix Threads are lightweight processes N part of one process N share address space N can share data easily N sharing data requires synchronisation, i.e., locking Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References Summary — Process States, Scheduling Summary — Processes and Threads Summary — Synchronisation References OSSI — ver. 1.5 Processes - p. 110/112 Summary — Synchronisation I I I When two threads of execution can both write to same data or I / O, N Need enforce discipline N Use synchronisation We looked at the following methods of synchronisation: N semaphore N mutex N condition variable N monitor N spinlock There are other methods we have not examined here. Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References Summary — Process States, Scheduling Summary — Processes and Threads Summary — Synchronisation References OSSI — ver. 1.5 Processes - p. 111/112 References There are many good sources of information in the library and on the Web about processes and threads. Here are some I recommend: I A good online tutorial about POSIX threads: http: //www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html Introduction Multitasking Start of Process Scheduler Process States top Process Control Blocks System Calls A shell program IPC Threads Race Condition Synchronisation Summary and References Summary — Process States, Scheduling Summary — Processes and Threads Summary — Synchronisation References I http://www.humanfactor.com/pthreads/ provides links to a lot of information about POSIX threads I The best book about POSIX threads is Programming with POSIX Threads, David Butenhof, Addison-Wesley, May 1997. Even though it was written so long ago, David wrote much of the POSIX threads standard, so it really is the definitive work. It made me laugh, too! I Operating Systems: A Modern Perspective: Lab Update, 2nd Edition, Gary Nutt, Addison-Wesley, 2002. A nice text book that emphasises the practical (like I do!) I Microsoft MSDN provides details of Win32 API calls and provides examples of code. I William Stallings, Operating Systems, Fourth Edition, Prentice Hall, 2001, chapters 3, 4 and 5 I Deitel, Deitel and Choffnes, Operating Systems, Third Edition, Prentice Hall, 2004, ISBN 0-13-1182827-4, chapters 3, 4 and 5 I Paul Rusty Russell, Unreliable Guide To Locking http: //kernelnewbies.org/documents/kdoc/kernel-locking/lklockingguide.html I W. Richard Stevens, Advanced Progamming in the UNIX Environment, Addison-Wesley, 1992 I Eric S. Raymond, The Art of UNIX Programming, Addison-Wesley, 2004, ISBN 0-13-142901-9. OSSI — ver. 1.5 Processes - p. 112/112