52
1 Tuesday, June 13, 2006 ... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man page 4 - Brett L. Huber

1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

1

Tuesday, June 13, 2006

... Our continuing mission: To seek out knowledge of C, to explore strange

UNIX commands, and to boldly code where no one has man page 4

- Brett L. Huber

Page 2: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

2

Announcement

Assignment 1 is up on the website.

Page 3: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

3

Output?int main(int argc, char** argv){ int session = 0; char str[] = "a = "; char ptr[] = "x = "; printf("Start.\n"); ++session; fork(); printf("%s %i . \n", str, session); fork(); printf("%s %i . \n", ptr, session);}

Page 4: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

4

Interprocess Communication

Pipes

Shared Memory

Message Queues

Sockets

Page 5: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

5

Pipescat file1 file2 | sortOnly processes with common ancestorOne-way communicationData write order

What is write end of pipe connected to?

What is read end of pipe connected to?

Page 6: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

6

Pipes

pipe() system call

Takes an array of 2 integers

int pipesfd[2];

int ret=pipe(pipesfd);

if (ret==-1){

perror(“pipe”);

exit(1);

}

Page 7: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

7

Pipes

pipesfd[0] contains the read file descriptor

pipesfd[1] contains the write file descriptor

Page 8: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

8

int main(int argc, char* argv[]){ int data_pipe[2]; int pid; int rc; rc = pipe(data_pipe); if (rc == -1) { perror("pipe"); exit(1); } pid = fork(); switch (pid) { case -1:/* fork failed. */ perror("fork"); exit(1); case 0:/* inside child process. */ do_child(data_pipe); default:/* inside parent process. */ do_parent(data_pipe); } return 0;}

Page 9: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

9

void do_parent(int data_pipe[]){ char ch; int rc;

close(data_pipe[0]);

while ((ch = getchar()) > 0) { /* write the character to the pipe. */ rc = write(data_pipe[1], &ch, 1); if (rc == -1) { perror("Parent: write"); close(data_pipe[1]); exit(1); } }

close(data_pipe[1]); exit(0);}

Page 10: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

10

void do_child(int data_pipe[]) {

char ch;/*data received from parent*/ int rc;/* return status of read(). */

close(data_pipe[1]);

while ((rc = read(data_pipe[0], &ch, 1)) > 0) {

putchar(ch); } close(data_pipe[0]);

exit(0);}

Page 11: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

11

Two way communication ...

Page 12: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

12

Named Pipes

Limitation of anonymous pipes: related

processes

IPC in Unrelated Processes?

Named Pipe or FIFO

If file opened for reading, it is blocked until

another process opens it for writing.

• Named pipes use between two processes on same system.

Page 13: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

13

Named Pipes

mknod sample p

prw-r--r--

Page 14: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

14

int main(int argc, char* argv[]){ FILE* pfile; while (1) { pfile = fopen("/export/home/sample", "w");

if (!pfile) { perror("fopen"); exit(1); } fprintf(pfile, "hello from write process"); fclose(pfile);

sleep(1); } return 0;}

writefifo.c

Page 15: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

15

int main(int argc, char* argv[]){ char buf[200]; FILE* pfile; while (1) { pfile = fopen("/export/home/sample", "r"); if (!pfile) { perror("fopen"); exit(1); } fgets(buf, 200, pfile); printf("%s\n", buf);

sleep(1); } return 0;}

readfifo.c

Page 16: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

16

Run the previous as separate processes …

Another way of making named pipes:

int mkfifo(const char *path, mode_t mode);

Upon successful completion, 0 is returned. Otherwise -1 is returned and errno is set to indicate the error.

Page 17: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

17

Message Queues

With pipes Parse bytes FIFO

Message queues can be public or private “Message based” Several processes may read / write to a

queue Messages may be read by type

Page 18: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

18

Sockets

Page 19: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

19

Threads

Traditionally a process has an address space and a single thread of execution

Process: thread + address space Keeps track of what to execute next Registers hold current working variables It has a stack (execution history)

Threads allow multiple executions to take place in the same process environment

Page 20: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

20

(a) Three processes each with one thread(b) One process with three threads

Environment (resource) execution

THREADS

Page 21: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

21

Threads

Multiple threads is analogous to multiple processes running in parallel

Light weight process vs Heavy weight process

Threads share an address space open files other resources

Page 22: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

22

Threads in the same process share resourcesEach thread executes separately

THREADS

Page 23: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

23

Threads can be in one of the following states: Running, Blocked Ready Terminated

Page 24: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

24

Thread idEach thread has its own stack. Why?

Page 25: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

25

Thread idEach thread has its own stack. Why?

Threads may resume out of order: • Different execution history

• Cannot use a single LIFO stack to save state

Page 26: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

26

THREADS

Page 27: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

27

Motivation

Spread sheetsLarge database accessSpell check, grammar checkServersetc.

Page 28: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

28

BenefitsResponsiveness

Substantial computing + substantial I/O leads to overlap

Resource Sharing

Economy Process creation is time consuming. Solaris

• Creating a process is 30 times slower (In some system 100 times faster)

• Context switch is 5 times slower

Utilization of MP Architectures

Page 29: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

29

Ability of threads to share a set of resources, so that they can work closely together

Page 30: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

30

Thread Usage: word processor

Page 31: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

31

Thread Usage: word processor

Closely collaborate with each other

Threads unavoidable.

Programming model simpler

Page 32: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

32

Single threaded web server

Page 33: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

33

Thread Usage: Web Server

Page 34: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

34

Multithreaded kernels Solaris has a set of threads for interrupt

handling

Page 35: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

35

Real OS permutations

Page 36: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

36

Threads

Not independent as processesSame address spaceShare same global variablesCan read, write another thread’s stackNo protection between threads:

What if a thread runs endlessly? Necessary?

Page 37: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

37

Threads in user space

Threads package entirely in user space Kernel thinks it is managing a traditional single

threaded process Private thread table + scheduling

Page 38: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

38

A user-level threads package

Page 39: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

39

Threads in user space

If a thread has to wait for another thread scheduler can choose another ready thread to run

Thread yield No trap needed No context switch etc Scheduler is just like a local procedure and

much faster than making a kernel call

Page 40: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

40

Threads in user space

Customized scheduling algorithms

Page 41: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

41

Threads in user space

However, there are disadvantages as well…

Page 42: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

42

Threads in user space

Blocking system calls?• Read from keyboard

– (e.g. in our word processor example)

• Disk read

No clock interrupts

Multithreaded webserver : threads often block

Page 43: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

43

Many-to-One

Many User-Level Threads Mapped to Single Kernel Thread.

Used on Systems That Do Not Support Kernel Threads.

Page 44: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

44

Many-to-one Model

Page 45: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

45

Threads in Kernel

When a thread wants to create a new thread or terminate an existing one – it makes a kernel call

Kernel thread table• Registers

• State

• Etc

Page 46: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

46

Threads in Kernel

When thread blocks the kernel can select from same process or different one

Overhead of creation etc is larger

Page 47: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

47

One-to-One

Each User-Level Thread Maps to Kernel Thread.

Examples

Windows 95/98/NT/XP Linux Solaris 9

Page 48: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

48

One-to-one Model

Page 49: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

49

Many-to-many Model

Example- Version older than Solaris 9

Page 50: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

50

Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads

Page 51: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

51

Process creation is time consuming

Page 52: 1 Tuesday, June 13, 2006... Our continuing mission: To seek out knowledge of C, to explore strange UNIX commands, and to boldly code where no one has man

52

Library procedures to: Create threads Exit when finished Wait for another thread Yield – voluntarily give up CPU