Download presentation
Presentation is loading. Please wait.
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 page 4 - Brett L. Huber
2
2 Announcement §Assignment 1 is up on the website.
3
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); }
4
4 Interprocess Communication Pipes Shared Memory Message Queues Sockets
5
5 Pipes cat file1 file2 | sort §Only processes with common ancestor §One-way communication §Data write order What is write end of pipe connected to? What is read end of pipe connected to?
6
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); }
7
7 Pipes pipesfd[0] contains the read file descriptor pipesfd[1] contains the write file descriptor
8
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; }
9
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); }
10
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); }
11
11 §Two way communication...
12
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.
13
13 Named Pipes § mknod sample p §prw-r--r--
14
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
15
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
16
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.
17
17 Message Queues §With pipes l Parse bytes l FIFO §Message queues can be public or private l “Message based” l Several processes may read / write to a queue l Messages may be read by type
18
18 Sockets
19
19 Threads §Traditionally a process has an address space and a single thread of execution Process: thread + address space l Keeps track of what to execute next l Registers hold current working variables l It has a stack (execution history) §Threads allow multiple executions to take place in the same process environment
20
20 (a) Three processes each with one thread (b) One process with three threads Environment (resource) execution THREADS
21
21 Threads §Multiple threads is analogous to multiple processes running in parallel §Light weight process vs Heavy weight process §Threads share l an address space l open files l other resources
22
22 §Threads in the same process share resources §Each thread executes separately THREADS
23
23 §Threads can be in one of the following states: l Running, l Blocked l Ready l Terminated
24
24 §Thread id §Each thread has its own stack. Why?
25
25 §Thread id §Each thread has its own stack. Why? l Threads may resume out of order: Different execution history Cannot use a single LIFO stack to save state
26
26 THREADS
27
27 Motivation §Spread sheets §Large database access §Spell check, grammar check §Servers §etc.
28
28 Benefits §Responsiveness Substantial computing + substantial I/O leads to overlap §Resource Sharing §Economy Process creation is time consuming. l Solaris Creating a process is 30 times slower (In some system 100 times faster) Context switch is 5 times slower §Utilization of MP Architectures
29
29 §Ability of threads to share a set of resources, so that they can work closely together
30
30 Thread Usage: word processor
31
31 Thread Usage: word processor Closely collaborate with each other Threads unavoidable. Programming model simpler
32
32 Single threaded web server
33
33 Thread Usage: Web Server
34
34 §Multithreaded kernels l Solaris has a set of threads for interrupt handling
35
35 Real OS permutations
36
36 Threads §Not independent as processes §Same address space §Share same global variables §Can read, write another thread’s stack §No protection between threads: l What if a thread runs endlessly? l Necessary?
37
37 Threads in user space l Threads package entirely in user space l Kernel thinks it is managing a traditional single threaded process l Private thread table + scheduling
38
38 A user-level threads package
39
39 Threads in user space l If a thread has to wait for another thread scheduler can choose another ready thread to run l Thread yield l No trap needed l No context switch etc l Scheduler is just like a local procedure and much faster than making a kernel call
40
40 Threads in user space l Customized scheduling algorithms
41
41 Threads in user space l However, there are disadvantages as well…
42
42 Threads in user space l Blocking system calls? Read from keyboard – (e.g. in our word processor example) Disk read l No clock interrupts l Multithreaded webserver : threads often block
43
43 Many-to-One §Many User-Level Threads Mapped to Single Kernel Thread. §Used on Systems That Do Not Support Kernel Threads.
44
44 Many-to-one Model
45
45 Threads in Kernel l When a thread wants to create a new thread or terminate an existing one – it makes a kernel call l Kernel thread table Registers State Etc
46
46 Threads in Kernel l When thread blocks the kernel can select from same process or different one l Overhead of creation etc is larger
47
47 One-to-One §Each User-Level Thread Maps to Kernel Thread. §Examples §Windows 95/98/NT/XP §Linux § Solaris 9
48
48 One-to-one Model
49
49 Many-to-many Model Example - Version older than Solaris 9
50
50 Hybrid Implementations Multiplexing user-level threads onto kernel- level threads
51
51 §Process creation is time consuming
52
52 §Library procedures to: l Create threads l Exit when finished l Wait for another thread l Yield – voluntarily give up CPU
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.