Operating Systems Lecture 3: The Process API
Nipun Batra Aug 9, 2019 Memory in C
2 Memory in C
2 Process Execution
Disk
3 Process Execution
Program (hello.c) Disk
3 Process Execution
Compile
Program (hello.c) Disk
3 Process Execution
Compile
Program Executable (hello.c) (hello.out) Disk
3 Process Execution
Executable Disk (hello.out)
4 Process Execution
Executable Disk (hello.out)
5 Process Execution
Memory
Executable Disk (hello.out)
5 Process Execution
Memory
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Address Memory Space
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Memory
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Code Memory
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Code Static Memory data
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Code Static Memory data Heap
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Code Static Memory data Heap Stack
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Code Memory CPU Static data Heap Stack
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Program Code Counter Memory CPU Static data Heap Stack
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Program Code Counter Memory CPU Static data Heap Stack
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Program Code Counter Memory CPU Static 1. Fetch data Heap Stack
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Program Code Counter Memory CPU Static 1. Fetch data 2. Decode Heap Stack
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Program Code Counter Memory CPU Static 1. Fetch data 2. Decode Heap 3. Execute Stack
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Program Code Counter Memory CPU Static 1. Fetch data 2. Decode Heap 3. Execute Stack 4. Update PC
Load from Executable disk to memory Disk (hello.out)
5 Process Execution
Program Code Counter Memory CPU Static 1. Fetch data 2. Decode Heap 3. Execute Stack 4. Update PC
Load from Executable disk to memory Disk (hello.out)
5 CPU Virtualisation
6 CPU Virtualisation
• Goal: Provide an illusion of many CPUs
6 CPU Virtualisation
• Goal: Provide an illusion of many CPUs • How: Time sharing between processes
6 CPU Virtualisation
• Goal: Provide an illusion of many CPUs • How: Time sharing between processes
P1 P2 P3 P1
6 CPU Virtualisation
• Goal: Provide an illusion of many CPUs • How: Time sharing between processes
P1 P2 P3 P1
23% P1 P2 51% P3 Unused 19%
7% 6 CPU Virtualisation
• Goal: Provide an illusion of many CPUs • How: Time sharing between processes
P1 P2 P3 P1
23% P1 P2 Space sharing for 51% P3 memory Unused 19%
7% 6 CPU Virtualisation II
P1
Running
7 CPU Virtualisation II Want to run
P2 P3 P1
P1
Running
8 CPU Virtualisation II Want to run
P2 P3 P1
P1 OS Running Scheduler
9 CPU Virtualisation II Want to run
P2 P3 P1
P1 OS Running Scheduler
Next P = f(run time, metric, type of process, …)
9 CPU Virtualisation II Want to run
P2 P3 P1
P1 P2 OS Running Should Scheduler run
10 CPU Virtualisation II Want to run
P2 P3 P1
P1 P2 OS Running Should Scheduler run Low level mechanisms (Context switch)
11 CPU Virtualisation II Want to run How to run P2 P3 P1
P1 P2 OS Running Should Scheduler run Low level mechanisms Which program (Context to run switch)
12 Process API
13 Process API
• Create process:
13 Process API
• Create process: • Double click
13 Process API
• Create process: • Double click • Run on command line
13 Process API
• Create process: • Double click • Run on command line • Destroy processes:
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait:
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes • Status:
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes • Status: • How long run, what state it is in
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes • Status: • How long run, what state it is in • Does top, ps give us this info?
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes • Status: • How long run, what state it is in • Does top, ps give us this info? • Misc.:
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes • Status: • How long run, what state it is in • Does top, ps give us this info? • Misc.: • Suspend
13 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes • Status: • How long run, what state it is in • Does top, ps give us this info? • Misc.: • Suspend
13 Process States
Scheduled Running Ready
Descheduled
I/O initiate I/O done
Blocked
14 Process States
Running Ready Blocked
P1
P0
0 1 2 3 4 5 6 7 Time
15 Process States
Running Ready Blocked
P1
P0
0 1 2 3 4 5 6 7 8 Time
16 Process API
• Create process: • Double click • Run on command line • Destroy processes: • Task manager • Command line • Wait: • Don’t run process till other process completes • Status: • How long run, what state it is in • Does top, ps give us this info? • Misc.: • Suspend
17 Process API
• Create process: • fork() • exec() • Wait: • wait()
18 The fork() System Call
> man fork
19 fork() demo
20 fork() demo 1. fork_demo_1.c : Get the PID of current process
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process?
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process? 4. See the above in Activity Monitor
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process? 4. See the above in Activity Monitor 5. fork_demo_3.c: Add sleep to view more details in Activity Monitor
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process? 4. See the above in Activity Monitor 5. fork_demo_3.c: Add sleep to view more details in Activity Monitor 6. fork_demo_4.c: Use fork() to create child process
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process? 4. See the above in Activity Monitor 5. fork_demo_3.c: Add sleep to view more details in Activity Monitor 6. fork_demo_4.c: Use fork() to create child process 7. fork_demo_5.c: Add sleep to above and find these processes on Activity Monitor
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process? 4. See the above in Activity Monitor 5. fork_demo_3.c: Add sleep to view more details in Activity Monitor 6. fork_demo_4.c: Use fork() to create child process 7. fork_demo_5.c: Add sleep to above and find these processes on Activity Monitor 8. Show the same using ps command
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process? 4. See the above in Activity Monitor 5. fork_demo_3.c: Add sleep to view more details in Activity Monitor 6. fork_demo_4.c: Use fork() to create child process 7. fork_demo_5.c: Add sleep to above and find these processes on Activity Monitor 8. Show the same using ps command 1. (ps -p 42693 -o pid,ppid)
20 fork() demo 1. fork_demo_1.c : Get the PID of current process 2. fork_demo_2.c : Get the PID of parent process 3. Use ps (man ps to find more) to find what’s the parent process? 4. See the above in Activity Monitor 5. fork_demo_3.c: Add sleep to view more details in Activity Monitor 6. fork_demo_4.c: Use fork() to create child process 7. fork_demo_5.c: Add sleep to above and find these processes on Activity Monitor 8. Show the same using ps command 1. (ps -p 42693 -o pid,ppid) 9. Fun: Keep finding parent process
20 The fork() System Call
Parent …. …. main(){ fork() …. ….}
21 The fork() System Call
Code Static Address Parent data Space of …. Heap Parent …. main(){ Stack fork() …. ….}
21 The fork() System Call
Code Static Address Parent data Space of …. Heap Parent …. main(){ Stack fork() …. ….}
22 The fork() System Call
Code
Address Static data Parent Space of Parent …. Heap …. main(){ Stack fork() …. ….}
23 The fork() System Call
Code
Static Address Code data Space of Static Heap Child Address data Space of Stack Child Parent Heap Parent …. …. Stack …. …. main(){ main(){ fork() fork() …. …. ….} ….}
24 The fork() System Call
Code
Static Address Code data Space of Static Heap Child Address data Space of Stack Child Parent Heap Parent …. …. Stack …. …. main(){ main(){ fork() fork() …. …. ….} ….}
25 fork() Code Usage in Linux repo
26 The wait() System Call
Run fork_demo_4 again. Different order? > man 2 fork
27 The wait() System Call
> man 2 fork Wait? Why does man fork not work?
28 The wait() System Call
29 The wait() System Call
1. wait_demo_1.c :wait for child to exit
29 The wait() System Call
1. wait_demo_1.c :wait for child to exit 2. But wait, which wait to use?
29 The wait() System Call
30 The wait() System Call
1. wait_demo_1.c :wait for child to exit
30 The wait() System Call
1. wait_demo_1.c :wait for child to exit 2. But wait, which wait to use?
30 The wait() System Call
1. wait_demo_1.c :wait for child to exit 2. But wait, which wait to use? 3. Run wait_demo_2.c
30 The wait() System Call
1. wait_demo_1.c :wait for child to exit 2. But wait, which wait to use? 3. Run wait_demo_2.c 4. Run wait_demo_3.c and find out NULL/0,’\0’
30 The exec() System Call
> man 3 exec
31 The exec() System Call
32 The exec() System Call
1. exec_demo_1.c : Execute other process
32 The exec() System Call
1. exec_demo_1.c : Execute other process 2. exec_demo_2.c : Execute other process with arguments
32 The exec() System Call
1. exec_demo_1.c : Execute other process 2. exec_demo_2.c : Execute other process with arguments 3. exec_demo_3.c: Pass arguments around!
32 The exec() System Call
1. exec_demo_1.c : Execute other process 2. exec_demo_2.c : Execute other process with arguments 3. exec_demo_3.c: Pass arguments around! 4. exec_demo_4.c: Get wc of exec_demo_4.c
32 The exec() System Call
1. exec_demo_1.c : Execute other process 2. exec_demo_2.c : Execute other process with arguments 3. exec_demo_3.c: Pass arguments around! 4. exec_demo_4.c: Get wc of exec_demo_4.c 5. exec_demo_5.c: Get wc of any file
32 The exec() System Call
1. exec_demo_1.c : Execute other process 2. exec_demo_2.c : Execute other process with arguments 3. exec_demo_3.c: Pass arguments around! 4. exec_demo_4.c: Get wc of exec_demo_4.c 5. exec_demo_5.c: Get wc of any file 6. man wc to understand what we get
32 The exec() System Call
1. exec_demo_1.c : Execute other process 2. exec_demo_2.c : Execute other process with arguments 3. exec_demo_3.c: Pass arguments around! 4. exec_demo_4.c: Get wc of exec_demo_4.c 5. exec_demo_5.c: Get wc of any file 6. man wc to understand what we get 7. exec_demo_6.c: Local variables accessible in child!
32