Upload
meryl-walters
View
215
Download
2
Embed Size (px)
Citation preview
Copyright ©: Nahrstedt, Angrave, Abdelzaher 1
Processes
Tarek AbdelzaherVikram Adve
Copyright ©: Nahrstedt, Angrave, Abdelzaher
2
Users, Programs, Processes
Users have accounts on the system Users launch programs
Many users may launch same program
One user may launch many instances of the same program
Processes: an executing program (the unit of
work of a modern computer)
Copyright ©: Nahrstedt, Angrave, Abdelzaher
3
Process States
Possible process states Running (occupy CPU) Blocked Ready (does not occupy CPU) Other states: suspended, terminated
Question: in a single processor machine, how many process can be in running state?
Copyright ©: Nahrstedt, Angrave, Abdelzaher
4
Linux: 5 State Process Model
Add states for creating and deleting process
Add transitions Timeout, Dispatch, Event Occurs
Copyright ©: Nahrstedt, Angrave, Abdelzaher
5
How to List all Processes? On Windows: run Windows task
manager Hit Control+ALT+delete Click on the “processes” tab
On UNIX $ ps –e Try “man ps” to understand more
about its usage.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
6
Process Execution and Context Switching
Copyright ©: Nahrstedt, Angrave, Abdelzaher
7
Process Identification UNIX identifies processes via a unique Process ID
Each process also knows its parent process ID since each process is created from a parent process.
Root process is the ‘init’ process ‘getpid’ and ‘getppid’ – functions to return process ID (PID)
and parent process ID (PPID)
Example 1#include <stdio.h>#include <unistd.h>int main (void) {
printf(“I am process %ld\n”, (long)getpid());printf(“My parent id is %ld\n”, (long)getppid());return 0;
}
Copyright ©: Nahrstedt, Angrave, Abdelzaher
8
Creating a Process – fork()
fork() creates a duplicate of the calling process: Both processes continue with return from fork() Child gets exact copy of code, stack, file descriptors,
heap, globals, and program counter Child gets new pid, ppid, time; no signals, file locks, …
fork() returns -1 if fork fails 0 in child process child’s PID in parent process
Copyright ©: Nahrstedt, Angrave, Abdelzaher
9
Creating a Process in Unix
Example 2
#include <stdio.h>#include <unistd.h>
int main(void) { pid_t x; x = fork(); if (x == 0) printf(“In child: fork() returned %ld\n”, (long) x); else printf(“In parent: fork() returned %ld\n", (long) x);}
What does this print?
Copyright ©: Nahrstedt, Angrave, Abdelzaher
10
Chain and Fan
ChildChildParent
Parent
Child Child… …
pid_t childpid = 0;for (i=1;i<n;i++) if (childpid = fork()) break;
pid_t childpid = 0;for (i=1;i<n;i++) if ((childpid = fork()) <=0) break;
Chain
Fan
Copyright ©: Nahrstedt, Angrave, Abdelzaher
11
Process Operations: Creation
A new process needs resources such as CPU, memory, file descriptors, I/O devices Child can get resources from OS or from parent Child address space is duplicate of parent process Child process is given a subset of parent
resources Prevents too many processes from overloading system
Execution possibilities are Parent continues concurrently with child Parent waits until child has terminated
Copyright ©: Nahrstedt, Angrave, Abdelzaher
12
Process Termination
Normal exit (voluntary) End of main() exit(0)
Error exit (voluntary) exit(2) or abort()
Fatal error (involuntary) Divide by 0, seg fault, exceeded resources
Killed by another process (involuntary) Signal: kill(procID)
Copyright ©: Nahrstedt, Angrave, Abdelzaher
13
Process Operations: Termination
When a process terminates: Open files are flushed and closed Tmp files are deleted Child’s resources are de-allocated
File descriptors, memory, semaphores, file locks, …
Parent process is notified via a signal Exit status is available to parent via wait()
Copyright ©: Nahrstedt, Angrave, Abdelzaher
14
Process Hierarchies Parent creates a child process, a child
process can create its own processes Forms a hierarchy
UNIX calls this a "process group" Windows has no concept of process
hierarchy all processes are created equal
Copyright ©: Nahrstedt, Angrave, Abdelzaher
15
wait(), waitpid() System Calls
wait() causes parent process to wait (block) until some child finishes
wait() returns child’s pid and exit status to parent
waitpid() waits for a specific child
errno Cause
ECHILD Caller has no unwaited-for children
EINTR Function was interrupted by signal
EINVAL Options parameter of waitpid was invalid
Copyright ©: Nahrstedt, Angrave, Abdelzaher
16
Waiting for a child to finish(Try “man –s 2 wait”)
#include <errno.h>#include <sys/wait.h>
pid_t childpid;
childpid = wait(NULL);if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);