View
216
Download
0
Embed Size (px)
Citation preview
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
Announcement
Assignment 1 is up on the website.
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
Interprocess Communication
Pipes
Shared Memory
Message Queues
Sockets
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?
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
Pipes
pipesfd[0] contains the read file descriptor
pipesfd[1] contains the write file descriptor
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
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
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
Two way communication ...
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
Named Pipes
mknod sample p
prw-r--r--
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
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
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
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
18
Sockets
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
20
(a) Three processes each with one thread(b) One process with three threads
Environment (resource) execution
THREADS
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
22
Threads in the same process share resourcesEach thread executes separately
THREADS
23
Threads can be in one of the following states: Running, Blocked Ready Terminated
24
Thread idEach thread has its own stack. Why?
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
26
THREADS
27
Motivation
Spread sheetsLarge database accessSpell check, grammar checkServersetc.
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
29
Ability of threads to share a set of resources, so that they can work closely together
30
Thread Usage: word processor
31
Thread Usage: word processor
Closely collaborate with each other
Threads unavoidable.
Programming model simpler
32
Single threaded web server
33
Thread Usage: Web Server
34
Multithreaded kernels Solaris has a set of threads for interrupt
handling
35
Real OS permutations
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?
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
38
A user-level threads package
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
40
Threads in user space
Customized scheduling algorithms
41
Threads in user space
However, there are disadvantages as well…
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
43
Many-to-One
Many User-Level Threads Mapped to Single Kernel Thread.
Used on Systems That Do Not Support Kernel Threads.
44
Many-to-one Model
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
46
Threads in Kernel
When thread blocks the kernel can select from same process or different one
Overhead of creation etc is larger
47
One-to-One
Each User-Level Thread Maps to Kernel Thread.
Examples
Windows 95/98/NT/XP Linux Solaris 9
48
One-to-one Model
49
Many-to-many Model
Example- Version older than Solaris 9
50
Hybrid Implementations
Multiplexing user-level threads onto kernel- level threads
51
Process creation is time consuming
52
Library procedures to: Create threads Exit when finished Wait for another thread Yield – voluntarily give up CPU