Processes Jinkyu Jeong (
[email protected]) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu
SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
OS Internals shell User space
shell
ps
ls
trap
Kernel space
System Call Interface
Memory Management
I/O Management
scheduler IPC synchronization
(device drivers)
Protection
File System Management
Process Management
Hardware Control (Interrupt handling, etc.)
Hardware SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
2
What is a Process? • An instance of a program in execution • The basic unit of protection • A process is identified using its process ID (PID) • A process includes – CPU context (registers) – OS resources (address space, open files, etc.) – Other information (PID, state, owner, etc.) SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
3
Process vs. Program Memory
PC
Disk
Code Data Heap
code data program
SP Stack
SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
4
Running a Process
Fetch I Mem[PC] Decode I Execute I Update PC
PC
Code
Data
SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
5
Running Multiple Processes Code A
Data A Code B
Data B Code C
Data C SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
6
Interleaving Multiple Processes Code A
Data A Code B
Data B Code C
Data C SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
7
Virtualizing the CPU Code A
Data A
OS creates the illusion that each process has its own CPU (and memory)
Code B
Data B OS Code
OS Data SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
8
Example #include #include
int main() { int pid; if ((pid = fork()) == 0) /* child */ printf (“Child of %d is %d\n”, getppid(), getpid()); else /* parent */ printf (“I am %d. My child is %d\n”, getpid(), pid); } SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
9
Example Output
% ./a.out I am 31098. My child is 31099. Child of 31098 is 31099. % ./a.out Child of 31100 is 31101. I am 31100. My child is 31101.
SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
10
Process Hierarchy • Parent-child relationship – One process can create another process – Unix calls the hierarchy a “process group” – Windows has no concept of process hierarchy
• Browsing a list of processes: – ps in Unix – Task Manager (taskmgr) in Windows SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
$ cat file1 | wc
sh cat
wc
11
Process Creation • fork() – Creates a new process cloning the parent process • Parent inherits most of resources and privileges: open files, UID, etc. • Child also duplicates the parent’s address space
– Parent may either wait for the child to finish (using wait()), or it may continue in parallel – Shells or GUIs use this system call internally
• exec() – Replaces the current process image with a new program – Windows: CreateProcess() = fork() + exec() SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
12
Process Termination • Normal exit (c) • Error exit (voluntary) • Fatal error (involuntary) – Segmentation fault – illegal memory access – Protection fault – Exceed allocated resources, etc.
• Killed by another process (involuntary) – By receiving a signal
• Zombie process: terminated, but not removed SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
13
Process APIs • exit() – Caller process terminates its execution – Return from main() is identical – Parameter denotes whether this exit is normal (0) or error (1)
• kill() – Send signal to a process – Parameters are directives to go to sleep, to die, and other useful imperatives
• wait() – Wait for state changes in any child of the calling process • Usually termination of a child
– Release resources associated with the terminated child – waitpid() for a specific child SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
14
Simplified Shell int main(void) { char cmdline[MAXLINE]; char *argv[MAXARGS]; pid_t pid; int status; while (getcmd(cmdline, sizeof(buf)) >= 0) { parsecmd(cmdline, argv); if (!builtin_command(argv)) { if ((pid = fork()) == 0) { if (execv(argv[0], argv) < 0) { printf(“%s: command not found\n”, argv[0]); exit(0); } } waitpid(pid, &status, 0); } } } SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
15
Process State Transitions Created
exit Scheduled
Ready
Running Time slice exhausted
I/O or event completion
I/O or event wait
Blocked
SSE3044: Operating Systems, Fall 2016, Jinkyu Jeong (
[email protected])
16
Process State – Linux Example R: Runnable S: Sleeping T: Traced or Stopped D: Uninterruptible Sleep Z: Zombie