explain pipes in computer systems based on this explanation: Pipes : mechanism allowing…
- User
explain pipes in computer systems based on this explanation:
Pipes : mechanism allowing two processes to communicate Ø Very Old first Inter Process Communication (IPC) mechanisms in early UNIX, but exist also in Windows Ø Must a relationship (such as parent – child) exist between the communicating processes? Ø Can the pipes communicate over a network, or must the communicating processes reside on the same machine? Ø Pipes are unidirectional: you write at the write end, and read from the read end. Ø For bidirectional communication, use a socket, or two pipes. Ø You can use a pipe for bidirectional communication if both processes keep both ends of the pipe open. You need to define a protocol for whose turn it is to talk. This is highly impractical
- Assistant
- User
Program a Ping Pong between two processes in which they exchange an integer starting at 0 and they add one until 10 is reached. Ø The father starts sending a 0 and stops when reaching 10 Ø The child starts reading and stops when receiving -1 Ø Use two pipes: fd and fd_back
- Assistant
- User
i did my version of it but for some reason it doesn't work:
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> // for sleep function : waits for seconds #include <semaphore.h> #include <fcntl.h> #include <sys/types.h>
int main() {
int fd[2]; int fd_back[2]; int signal = 0; int sum = -1; pipe(fd); pipe(fd_back);
int pid = fork();
if (pid == 0) { close(fd[1]); close(fd_back[0]);
while (sum != -1) {
read(fd[0], &sum, sizeof(sum));
sum++;
write(fd_back[1], &sum, sizeof(sum));
} close(fd[0]); close(fd_back[1]);
} else {
close(fd[0]); close(fd_back[1]);
while (sum < 10) {
read(fd_back[0], &sum, sizeof(sum));
sum++; printf("sum: %d", sum); write(fd[1], &sum, sizeof(sum));
}
sum = -1;
write(fd[1], &sum, sizeof(signal)); close(fd_back[0]); close(fd[1]);
wait(NULL);
}
printf("sun: %d", sum); return 0; }
- Assistant
- User
what is the syscall dup2?
- Assistant
- User
it seems to me that is like if you where using the write system call to write a content from a buffer to a file descriptor
- Assistant
Yes, you're correct in observing that using for redirecting a file descriptor, followed by writing content (using , , or any other output function), operates under a principle that could be seen as akin to directly writing content from a buffer to a file descriptor. However, and serve distinct roles in this process, each contributing a specific part to the overall task of getting data from your program to a file or another output stream.
- User
then explain this code:
int main( ) { int fd[2]; int pid; char* myargv[] = {“ps”,”aux”, NULL}; pipe( fd ); pid = fork(); if ( pid > 0 ) { // parent close( fd[1] ); dup2( fd[0], 0 ); close( fd[0] ); } else { // child close( fd[0] ); dup2( fd[1], 1 ); close( fd[1] ); execvp( "ps", myargv); }
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.