Question
In this assignment, you will be using the pipe(), fork(), exec(), and wait() system calls.You will demonstrate that you understand how to properly establish inter-process
In this assignment, you will be using the pipe(), fork(), exec(), and wait() system calls.You will demonstrate that you understand how to properly establish inter-process communications using the pipe() system call, interpret the output of the fork() system call, properly use the exec() system call, and use the wait() system call to terminate correctly.
For this assignment, you will create a program that will execute the GNU Basic Calculator, send it commands via a pipe to the stdin of bc, which commands bc will execute and display the results via it's stdout. This will be accomplished in four stages:
- Your program will initialize one pipe using the pipe() system call and call the fork() system call
- The child process will attach the pipe using the dup2() system call to stdin and start bc
- The parent process will read data from stdin or a file and send commands to the child process via the pipe.
- The parent process will wait for the child process to terminate, collect the child process return code, and display the return code to stdout.
Just like the first assignment, you will read three numbers in at a time (this code will be provided for you). You will then send a command to the child process to multiply the first two numbers, and divide that multiplication by the third number (Formula: (n1 * n2) / n3). The child process (running bc) will perform the calculation. Your program will then repeat the process for every group of three numbers it reads.
code:
#include
void child_func(int *pipe) {
/* Duplicate the pipe for STDIN */
/* Close both ends of the pipe */
/* Use one of the exec() variants to start the bc program */
/* If there's an error, report the error and make the exit system call */
}
int main(int argc, char *argv[]) {
printf("You need to finish this program ");
/* First, create your pipe */
/* Second, fork a child process */
/* As the child process, call the child function */
/* As the parent process, open a file pointer */
FILE *fp = stdin;
if(argc > 1) {
fp = fopen(argv[1], "r");
if(fp == NULL) {
perror("Unable to open file");
return 1;
}
}
/* Next, read numbers in groups of three */
int n1, n2, n3;
while(3 == fscanf(fp, "%d %d %d", &n1, &n2, &n3)) {
/* Now, what should you do with these numbers? */
/* Probably format them and write them to the pipe */
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started