Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 2 - The fork() and exec() System Calls with IPC Objective: In this assignment, you will be writing a program that performs the pipe(),

Assignment 2 - The fork() and exec() System Calls with IPC

Objective:

In this assignment, you will be writing a program that performs 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.

Details:

Your program will make use of the GNU Basic Calculator (bc). This program allows for simple math and can be operated via an interactive mode, commands stored in a file, or command sent via stdin. A typical operation can be performed from the command line like this:

$ echo '5 * 6' | bc 30

The GNU Basic Calculator by default performs all operations without any decimal part. This can cause problems with division as the answer will appear incomplete. Here is the output of bc with regular integer division:

$ echo "100 / 7" | bc 14

To fix this, the scale=# command should be used to specify the number of decimal places to show. Here is the output of bc with fixed-point division:

$ echo "scale=4; 100 / 7" | bc 14.2857

Please note, that for this example the semicolon is necessary to separate the scale command from the actual division command. In your program, you will not need to use the semicolon as you will be sending the scale command at the start, followed by several calculations.

For this assignment, you will create a program that will execute the GNU Basic Calculator, send it commands via stdin, which bc will display via it's stdout. This will be accomplished in four stages:

  1. Your program will initialize one pipe using the pipe() system call and call the fork() system call
  2. The child process will attach the pipe using the dup2() system call to stdin and start bc
  3. The parent process will read data from stdin or a file (as practiced in Assignment 1) and send commands to the child process via the pipe.
  4. 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. 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.

Hints:

  • The pipe() system call takes one argument - a two element array of integers. The first array element is a file descriptor for the output end of the pipe, thus it is read-only. The second array element is a file descriptor for the input end of the pipe, thus it is write-only.
  • The fork() system call will create a new process, and both the parent and the child process will continue executing the next line in your program. The only difference between the child and the parent processes is the value returned by the fork() system call. Make sure you store that return value in a variable and evaluate it.
  • In order to change the stdin file descriptor for the child process to the output end of your pipe, you need to call the dup2() system call. This call takes two parameters - the first parameter is the file descriptor you are trying to duplicate and the second parameter is the file descriptor you want to replace. In other words, you will call it like this: dup2(pipe[0], STDIN_FILENO)
  • There are several ways for the parent process to write to the input end of the pipe. The most basic way is to use the write() system call - but this will require you to create a buffer and format your data into the buffer. You can also turn the file descriptor into a file pointer by using the fdopen() library function. In the option of your instructor, the simplest way is to use the dprintf() system call. Use the man pages to learn more about it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions