Question: Use the sample code below on Ubuntu system. Then create a diagram that visually describes the input/output structure of the executing program. how processes and
Use the sample code below on Ubuntu system. Then create a diagram that visually describes the input/output structure of the executing program. how processes and handles; show the file descriptor table as presented above in the File I/O section:
- at point A (after the pipe is created)
- at point B (after the parent forks)
- at point C (after the parent and child have duplicated descriptors)
- at point D (after parent and child close descriptors)
#include #include #include #include
#define READ 0 #define WRITE 1 #define MAX 1024
int main() { int fd[2]; ssize_t num; pid_t pid; char str[MAX];
if (pipe (fd) < 0) { perror ("plumbing problem"); exit(1); } // point A
if ((pid = fork()) < 0) { perror ("fork failed"); exit(1); } // point B
else if (!pid) { dup2 (fd[WRITE], STDOUT_FILENO); // point C close (fd[READ]); close (fd[WRITE]); // point D fgets (str, MAX, stdin); write (STDOUT_FILENO, (const void *) str, (size_t) strlen (str) + 1); exit (0); }
dup2 (fd[READ], STDIN_FILENO); // point C close (fd[READ]); close (fd[WRITE]); // point D num = read (STDIN_FILENO, (void *) str, (size_t) sizeof (str)); if (num > MAX) { perror ("pipe read error "); exit(1); } puts (str); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
