60-256 System Programming: Pipes I
Content
COMP-2560 System Programming:
Pipes I
Courtesy of Dr. B. Boufama
modified by Dan Wu
School of Computer Science University of Windsor
Instructor: Dr. Dan Wu
Pipes I
1
Copyright @ 2019, 2020, 2021 all rights reserved
Copyright @ 2019, 2020, 2021 all rights reserved
Content
Content
1
Review
Signals and files Example
2
Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Pipes I
2
Copyright @ 2019, 2020, 2021 all rights reserved
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
Signals and files Example
Signals and files
Some basic Interprocess communication (IPC) can be achieved using:
Signals: using the kill() system call.
Files: by passing open files across the fork()/exec()
The following is an example that illustrates the use of signals and files allowing a child process to provide its parent with data…
Pipes I
3
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
Signals and files Example
Example 1…(main) (review.c)
#include
#include
#include
#include
void action(int dummy){sleep(1);} void child(FILE *);
void parent(FILE *, pid_t);
int main(int argc, char *argv[]){ FILE *fp;
pid_t pid; int childRes;
fp = fopen(“/tmp/ipoc.txt”, “w+”); setbuf(fp, NULL);
if((pid=fork()) == 0) child(fp);
parent(fp, pid);
}
Pipes I
4
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
Signals and files Example
Example 1…(parent)
void parent(FILE *fp, pid_t pid){ int childRes, n=0;
while(1){
signal(16, action); pause(); rewind(fp);
fread(&childRes, sizeof(int), 1, fp); printf(“\nParent: child result: %d\n”, childRes); if(++n>5){
printf(“Parent: work done, bye bye\n”); unlink(“/tmp/ipoc.txt”);
kill(0, SIGTERM);
}
printf(“Parent: waiting for child\n\n”); kill(pid, 16);
}
}
Pipes I
5
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
Signals and files Example
Example 1…(child)
void child(FILE *fp){ int value;
while(1){
sleep(1);
value = random()%100; rewind(fp);
fwrite(&value, sizeof(int), 1, fp); printf(“Child: waiting for parent\n\n”); signal(16, action);
kill(getppid(), 16); pause();
}
}
Pipes I
6
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Unnamed pipes
Unnamed pipes
(Unnamed) Pipes, known as pipe, are a mechanism for interprocess communication.
Pipes are used by shells to connect one utility’s standard output with the standard input of another utility.
Example: ps -ef | grep netscape | wc -l
A process creates a pipe, forks then, uses the pipe to exchange information with its child.
Limitations
Pipes are the oldest form of Unix IPC. They have two limitations:
They are half-duplex: data flows in one direction only They can be used only between processes that have a common ancestor.
Pipes I
7
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
The pipe() system call
pipe()
Synopsis: int pipe(int fd[2])
returns 0 when successful and -1 otherwise
pipe() creates a pipe and returns two file descriptors:
fd[0] and fd[1]
fd[0] is open for reading,
fd[1] is open for writing.
A pipe is a one-way communication channel between two related processes.
Pipes I
8
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Rules when reading from and writing to a pipe
Rules
When reading from or writing to a pipe, the following rules apply:
If a process reads from a pipe whose write-end has been closed, after all data has been read, read() returns 0.
If a process reads from an empty pipe whose write-end is still open, it sleeps until some input becomes available.
If a process tries to read from a pipe more bytes than are present, read() reads all available bytes and returns the number of bytes read.
If a process writes to a pipe whose read-end has been closed, the write operation fails and the writer process receives a SIGPIPE.
In case of multiple processes writing to the same pipe, a write
of up to PIPE BUF bytes is guaranteed to be atomic.
→ data from different writer processes will not be interleaved.
Pipes I
9
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Example 2: child is a writer, parent is a reader…(main) pipe_ex1.c
#include
#include
#include
void child(int *); void parent(int *);
int main(int argc, char *argv[]){ int fd[2];
if(pipe(fd) == -1) exit(1);
if(fork() == 0) child(fd);
else
parent(fd); exit(0);
}
Pipes I
10
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Example 2: child is a writer, parent is a reader…(parent)
void parent(int *fd){ char ch;
close(fd[1]);
printf(“Child has sent the message:\n”); do{
read(fd[0], &ch, 1);
printf(“%c”, ch); if(ch == ’\n’)
break;
}while(1);
}
Pipes I
11
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Example 2: child is a writer, parent is a reader…(child)
void child(int *fd){
char message[255]=”Hello, here is my data…\n”;
close(fd[0]); write(fd[1], message, 26);
}
Pipes I
12
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Pipe1.c
Pipe2.c
Pipe3.c
Pipesize.c
Pipes I
13
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
The dup2() system call
dup2()
Synopsis: int dup2(int fd, int fd2);
The dup2() function causes the file descriptor fd2 to refer to the same file as fd.
The fd argument is a file descriptor referring to an open file.
fd2 is a non-negative integer less than the current value for the maximum number of open file descriptors allowed the calling process.
If fd2 already refers to an open file, not fd, it is closed first.
If fd2 refers to fd, or if fd is not a valid open file descriptor, fd2 will not be closed first.
If successful, dup2() returns a non-negative integer representing the file descriptor fd2.
Otherwise, -1 is returned.
After the call: dup2(fd, 0);
reading from stdin will mean reading from the file whose
descriptor is fd.
Pipes I
Just curious, what is dup()?
14
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Example 3: implementing the shell pipe mechanism (join.c)
See the join.c code posted
}
Pipes I
15
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Example 4: 2-player game with a referee…(main) (game.c)
int main(int argc, char *argv[]){ int fd1[2], fd2[2], fd3[2], fd4[2]; char turn=’T’;
printf(“This is a 2-player game with a referee\n”); pipe(fd1);
pipe(fd2); if(!fork())
player(“TOTO”, fd1, fd2);
close(fd1[0]); // parent only write to pipe 1 close(fd2[1]); // parent only reads from pipe 2
pipe(fd3); pipe(fd4); if(!fork())
player(“TITI”, fd3, fd4);
close(fd3[0]); // parent only write to pipe 3 close(fd4[1]); // parent only reads from pipe 4
while(1){
printf(“\nReferee: TOTO plays\n\n”); write(fd1[1], &turn, 1);
read(fd2[0], &turn, 1);
printf(“\nReferee: TITI plays\n\n”); write(fd3[1], &turn, 1);
read(fd4[0], &turn, 1);
}
}
Pipes I
16
Copyright @ 2019, 2020, 2021 all rights reserved
Review Unnamed pipes
The pipe() system call Rules
Example
The dup2() system call Examples
Example 4: 2-player game with a referee…(player)
void player(char *s, int *fd1, int *fd2){ int points=0;
int dice;
long int ss=0; char turn; close(fd1[1]);
close(fd2[0]); while(1){
read(fd1[0], &turn, 1);
printf(“%s: playing my dice\n”, s); dice =(int) time(&ss)%10 + 1; printf(“%s: got %d points\n”, s, dice); points+=dice;
printf(“%s: Total so far %d\n\n”, s, points); if(points >= 50){
printf(“%s: game over I won\n”, s); kill(0, SIGTERM);
}
sleep(5); // to slow down the execution write(fd2[1], &turn, 1);
}
}
Pipes I
17
Copyright @ 2019, 2020, 2021 all rights reserved