![Page 1: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/1.jpg)
Processes
Creating processes
• Unix: fork() + exec()
• Windows: CreateProcess()
Detecting process termination
• Unix: wait()
• Windows: GetExitCodeProcess()
Today: process communication
1
![Page 2: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/2.jpg)
Starting A Processes
fork.c
fork_parent.c and fork_child.c
2
![Page 3: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/3.jpg)
Process Control
• What happens if a parent exits before a child?
• Can a process terminate another process?
• Who cleans up?
3
![Page 4: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/4.jpg)
Communication
How can processes communicate?
• Command-line arguments
• Files
• Pipes
• Sockets
• Messages
• Shared memory
4-5
![Page 5: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/5.jpg)
File Descriptors
fgets(buffer, n, stdin)
⇒ read(0, buffer, n)
printf("hi")
⇒ write(1, "hi", 2)
fprintf(stderr, "hi")
⇒ write(2, "hi", 2)
6
![Page 6: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/6.jpg)
File Descriptors
fd
0fd
1fd
2process
OS
7
![Page 7: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/7.jpg)
File Descriptors
fd
0fd
1fd
2fd
0fd
1fd
2process
OS
18
![Page 8: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/8.jpg)
Pipes
int fds[2];pipe(fds);
fd
7fd
8process
OS
19
![Page 9: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/9.jpg)
Pipes
int fds[2];pipe(fds);
fd
7fd
8fd
7fd
8process
OS
30
![Page 10: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/10.jpg)
Pipes
int fds[2];pipe(fds);
fd
8fd
7process
OS
41
![Page 11: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/11.jpg)
Using Pipes
pipe() and dup2():
pipe_parent.c and pipe_child.c
42
![Page 12: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/12.jpg)
Buffering
Beware of pipe buffer limits
echo_parent.c and echo_child.c
43
![Page 13: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/13.jpg)
Message Passing
main() { ... q_id = msgget(); if (fork() != 0) producer(); else consumer();}
producer() { while (1) { ...
produce item nextp ... msgsnd(q_id, nextp); }}
consumer() { while (1) { msgrcv(q_id, nextp); ...
consume item nextp ... }}
can also use msgtok(ftok()) after fork()44
![Page 14: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/14.jpg)
Message Passing vs. Pipes
Can you build message passing on top of pipes?
45
![Page 15: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/15.jpg)
Shared Memory
main() { ... buffer = shmget(); if (fork() != 0) producer(); else consumer();}
producer() { while (1) { ...
produce item nextp ... while (((in+1) % n) == out); buffer[in] = nextp; in = (in+1) % n; }}
consumer() { while (1) { while (in == out); nextp = buffer[in]; out = (out+1) % n; ...
consume item nextp ... }}
can also use shmget(ftok()) after fork()
Lots of problems with this code...46-47
![Page 16: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/16.jpg)
Shared Memory vs. Message Passing vs. Pipes
Can you build shared memory on messagepassing?On pipes?
How about the other direction?
48-49
![Page 17: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/17.jpg)
Etc.
• Direct vs. indirect messages
• Sockets
• RPC
50
![Page 18: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/18.jpg)
From Processes Threads
For processes to cooperate:
• Create several processes
• Arrange a way to share data
• Context switch back and forth between theprocesses
Unfortunately, these operations are relativelyinefficient for the machine and inconvenient forprogrammers.
51-52
![Page 19: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/19.jpg)
From Processes Threads
What is shared between processes?
• Code is shared if we never call exec()
• Data is shared since that’s the point
• Privileges are shared
• Resources are shared (open files and sockets)
What is not shared?
• Execution state: PC, SP, registers
53-54
![Page 20: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/20.jpg)
Threads
Key idea: Separate the concept of a process fromits execution context.
• Process: address space, privileges, kernelresources
• Thread: execution state (PC, SP, registers)
55
![Page 21: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/21.jpg)
Processes and Threads
56
![Page 22: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/22.jpg)
Threads
• Every thread belongs to a particular process
• Processes are like containers that can hold manythreads (and all threads die when their processexits)
• Threads are the unit of CPU scheduling
• Switching between threads in the same process ismore efficient than switching between processes
57
![Page 23: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/23.jpg)
Kernel vs. User Threads
• A kernel thread is a thread that the OS knowsabout
a.k.a. lightweight process
OS schedules threads instead of processes
• A user thread is a thread that the OS doesn’tknow about
Faster and more flexible in principle
Not in parallel, problems with blocking systemcalls
• A mixture ⇒ many-to-many models
58
![Page 24: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/24.jpg)
User Threads
59
![Page 25: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/25.jpg)
Kernel Threads
60
![Page 26: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/26.jpg)
Using Threads
item buffer[n];
main() { ... pthread_create(producer); consumer();}
producer() { while (1) { ...
produce item nextp ... while (((in+1) % n) == out); buffer[in] = nextp; in = (in+1) % n; }}
consumer() { while (1) { while (in == out); nextp = buffer[in]; out = (out+1) % n; ...
consume item nextp ... }}
61
![Page 27: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/27.jpg)
Threads for Now
For now, it’s enough to know that threads exist.
Later, we’ll get back to this topic, along with thesynchornization tools that you need to make it work.
62
![Page 28: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes](https://reader035.vdocuments.mx/reader035/viewer/2022063005/5fb44d74432b3f06026adc8e/html5/thumbnails/28.jpg)
New Homework
HW2:
Implement a simple Unix shell
63