Top Banner
Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe
28

Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Dec 29, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Unix Pipes

Pipe sets up communication channel between two (related) processes.

37

Two processes connected by a pipe

Page 2: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

One process writes to the pipe, the other reads from the pipe.

Looks exactly the same as reading from/to a file. System call:

int fd[2] ;pipe(&fd[0]) ;

fd[0] now holds descriptor to read from pipefd[1] now holds descriptor to write into pipe

Page 3: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Simple Example

#include <unistd.h> /* for Solaris */#include <fcntl.h>#include <stdio.h>char *message = "This is a message!!!" ;main(){ char buf[1024] ; int fd[2]; pipe(fd); /*create pipe*/ if (fork() != 0) { /* I am the parent */ write(fd[1], message, strlen (message) + 1) ; } else { /*Child code */ read(fd[0], buf, 1024) ; printf("Got this from MaMa!!: %s\n", buf) ; }}

Page 4: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Create a Pipeline

Sometimes useful to connect a set of processes in a pipeline.

Process

c

Process

DPipePipe

Process A writes to pipe AB, Process B reads from AB and writes to BC

Process C reads from BC and writes to CD …..

Page 5: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Inherited File Descriptors

Process created with three files: stdin, stdout, and stderr (file descriptors 0, 1, 2).

write(1, “EEEE”, 10) ; // goes to stdout (terminal) or printf

read(0, buf, 4) ; //read from terminal

Page 6: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Redirecting stdin and stdout

Can due on command line: ./a.out > tmp // write output to tmp rather than

terminal. ./a.out < foo // read from foo not terminal.

When processing in a pipeline useful to redirect stdout of pipe n to stdin of pipe n+1.

Key: When Unix allocates file descriptor, it always chooses the lowest available.

If close stdin and open new file, e.g., close(0), fd = open(“foo”, ….) then fd = 0 and all subsequent reads from stdin will read instead from “foo”.

Page 7: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

main()

{

int fd ;

printf("this goes to stdout\n");

close(1); /* close stdout */

fd = open("foo",O_CREAT | O_WRONLY); /* fd = 1! */

printf("fd = %d\n", fd) ; //goes to file "foo"

printf("Should not see this either!\n") ;//ditto

}

Page 8: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Dup System Call

int dupfd = dup(fd); Duplicates a file descriptor -- “aliased”

Both point to same file, that being the argument to dup.

Reads/writes from fd and dupfd going to the same file.

Useful for situation such as: want to write some output to standard out,

then to a file, then back to standard out -- all using printf.

Redirecting I/O for pipeline.

Page 9: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

pipeline (process1, process2) /* program names */

char *process1, *process2 ;

{ char buf[1024] ;

int fd[2];

pipe(&fd[0]); /*create pipe*/

if (fork() != 0) { /* parent */

close(fd[0]); //don't need.

close(STD_OUTPUT);

dup(fd[1]); /* sets stdout to this pipe end */

close(fd[1]) ; //don’t need. Already set stdout to pipe.

execl(process2,process2,0);

}

Page 10: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

else {

/* child */

close(fd[1]);

close(STD_INPUT);

dup(fd[0]); /* replace stdin */

close(fd[0]);

execl(process1,process1,0);

}

Page 11: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

fork()

exec exec

stdout

stdin

Page 12: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Single and Multithreaded Processes

Page 13: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Benefits

Responsiveness: Separate thread to handle user input.

Web server spawns separate thread to handle incoming request.

Resource Sharing: e.g., one code, data segment.

Economy: Much cheaper to create and switch than processes.

Utilization of MP Architectures: Assign each thread to a separate processor if available.

Page 14: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Lightweight process. Threads share all process resources.

State: Thread ID, program counter, register set, and stack.

User-level threads and kernel-level threads.

Page 15: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

User Threads Thread management done by user-level

threads library. Advantages:

Very fast: Does not involve kernel in creation, scheduling, or switching.

Disadvantages: When a thread blocks, whole process blocks.

Page 16: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Kernel Threads Supported by the Kernel.

Kernel creates, schedules, and switches threads. Advantage:

When one thread blocks, the whole process does not have to block.

Thus can overlap I/O and computation. Disadvantage:

Slower since kernel is involved.

Page 17: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Multithreading Models Many-to-One

One-to-One

Many-to-Many

Page 18: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Many-to-One Many user-level threads mapped to

single kernel thread.

Used on systems that do not support kernel threads.

Page 19: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Many-to-One Model

Page 20: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

One-to-One Each user-level thread maps to

kernel thread.

Examples- Windows 95/98/NT/2000- OS/2

Page 21: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

One-to-one Model

Page 22: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Many-to-Many Model Allows many user level threads to be

mapped to many kernel threads. Allows the operating system to

create a sufficient number of kernel threads.

Solaris 2 Windows NT/2000 with the ThreadFiber package

Page 23: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Many-to-Many Model

Page 24: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Threading Issues Semantics of fork() and exec()

system calls. Thread cancellation.

Page 25: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Pthreads a POSIX standard (IEEE 1003.1c)

API for thread creation and synchronization.

API specifies behavior of the thread library, implementation is up to development of the library.

Common in UNIX operating systems.

Page 26: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Solaris 2 Threads

Page 27: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Solaris Process

Page 28: Unix Pipes Pipe sets up communication channel between two (related) processes. 37 Two processes connected by a pipe.

Windows 2000 Threads Implements the one-to-one

mapping. Each thread contains

- a thread id- register set- separate user and kernel stacks- private data storage area