processesmflatt/past-courses/cs5460/lecture3.pdf · detecting process termination • unix: wait()...

28
Processes Creating processes • Unix: fork() + exec() • Windows: CreateProcess() Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1

Upload: others

Post on 14-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Processesmflatt/past-courses/cs5460/lecture3.pdf · Detecting process termination • Unix: wait() • Windows: GetExitCodeProcess() Today: process communication 1. Starting A Processes

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

New Homework

HW2:

Implement a simple Unix shell

63