Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this assignment, you will write a C program that will serve as a calculator shell. The assignment is designed in two parts. Part I
In this assignment, you will write a C program that will serve as a calculator shell. The assignment is designed in two parts. Part I is intended to have you experiment with processes. Part II will have you use pipes for inter-process-communication. Note: you can easily get the outputs I am asking you for without using processes and pipes. However, that is not the point of the assignment: you will not get any points if you do not create processes to run the commands you encounter in both Parts 1 and 2. In Part 2, you must, likewise, use pipes to communicate the results computed by children processes to their parent. Part I (50%): In this part of the assignment, you will learn to program with processes. The purpose of this part is to program a shell for a calculator. The shell is a process that will run constantly until termination of the program through the command "exit. The shell issues its prompt: Calculator>" and waits for a command. When a command is issued in the shell, the shell executes the procedure associated with the command. Your shell is a very simple one that can take 5 commands: A, S, M, D, and exit, which correspond to Add, Subtract, Multiply, Divide and terminate the shell. Each of the first four commands must be followed by two numbers for S and D and as many numbers as desired for A and M. When one of the first four commands is entered by the user, the shell reads that command and creates a new process that executes the corresponding function. This new process runs while the process running the shell waits. When the function terminates, the process terminates too. For example, if A is the command read by the shell, a new process will be called that will execute a function called, say, "add whose purpose is to read all the arguments that follow, add them together and output the result. Upon receiving the exit signal, the shell process breaks out of its waiting loop and reads the new command. If the shell reads the command "exit, the program is terminated. You will need to use the following system calls: fork(), wait(), exec() (or another function from the exec() family), exit(). Example of a run: Calculator> 35 8 4 2 1 23 Calculator> D 936 Error: Too many arguments Calculator> D 9 3 3 Calculator> exit Part II (50%): So far, only one operation can be specified on the command line. We would like to expand the functionality of the shell so that it can accept the following kind of commands and obtain the following outputs: Calculator> 358 D 9321 22 Calculator> D 52 A 592 S51 M 321 2 Calculator> exit Please note that when A or M occur, all the arguments that follow must be considered as their own arguments. In the second instance, for example, the shell doesn't stop adding when Sand, later, M are encountered. S and D, however, are structured to take only 2 arguments in all cases. To better explain what should happen in your program, we can use parse trees to represent the operations: 31 -8 & 2 52 5 92 2 51321 The way this functionality will have to be implemented is through the use of pipes which will communicate the results of children processes to their parents. For example, in the first expression, the process running the shell will spawn a process that will run the function add. Upon reading its arguments, the process running "add" will encounter the command D and spawn a new process that will run the function "divide". Prior to finishing, the process running the function "divide" will communicate the result of the division to its parent through a pipe. The process running the function add will read the pipe. The process running the function "divide" will then end. The process running "add" will then add the result read from the pipe and continue adding the other arguments until encountering the end-of-line character. In the second expression, there will be an extra process in the tree of processes when either S or M are encountered. At those points, the shell will have spawned a process running the function "divide". This process will have spawned a process running the function "add". This process will successively spawn a process running the function "subtract" and, once that process ends, a process running the function "multiply". In this assignment, you will write a C program that will serve as a calculator shell. The assignment is designed in two parts. Part I is intended to have you experiment with processes. Part II will have you use pipes for inter-process-communication. Note: you can easily get the outputs I am asking you for without using processes and pipes. However, that is not the point of the assignment: you will not get any points if you do not create processes to run the commands you encounter in both Parts 1 and 2. In Part 2, you must, likewise, use pipes to communicate the results computed by children processes to their parent. Part I (50%): In this part of the assignment, you will learn to program with processes. The purpose of this part is to program a shell for a calculator. The shell is a process that will run constantly until termination of the program through the command "exit. The shell issues its prompt: Calculator>" and waits for a command. When a command is issued in the shell, the shell executes the procedure associated with the command. Your shell is a very simple one that can take 5 commands: A, S, M, D, and exit, which correspond to Add, Subtract, Multiply, Divide and terminate the shell. Each of the first four commands must be followed by two numbers for S and D and as many numbers as desired for A and M. When one of the first four commands is entered by the user, the shell reads that command and creates a new process that executes the corresponding function. This new process runs while the process running the shell waits. When the function terminates, the process terminates too. For example, if A is the command read by the shell, a new process will be called that will execute a function called, say, "add whose purpose is to read all the arguments that follow, add them together and output the result. Upon receiving the exit signal, the shell process breaks out of its waiting loop and reads the new command. If the shell reads the command "exit, the program is terminated. You will need to use the following system calls: fork(), wait(), exec() (or another function from the exec() family), exit(). Example of a run: Calculator> 35 8 4 2 1 23 Calculator> D 936 Error: Too many arguments Calculator> D 9 3 3 Calculator> exit Part II (50%): So far, only one operation can be specified on the command line. We would like to expand the functionality of the shell so that it can accept the following kind of commands and obtain the following outputs: Calculator> 358 D 9321 22 Calculator> D 52 A 592 S51 M 321 2 Calculator> exit Please note that when A or M occur, all the arguments that follow must be considered as their own arguments. In the second instance, for example, the shell doesn't stop adding when Sand, later, M are encountered. S and D, however, are structured to take only 2 arguments in all cases. To better explain what should happen in your program, we can use parse trees to represent the operations: 31 -8 & 2 52 5 92 2 51321 The way this functionality will have to be implemented is through the use of pipes which will communicate the results of children processes to their parents. For example, in the first expression, the process running the shell will spawn a process that will run the function add. Upon reading its arguments, the process running "add" will encounter the command D and spawn a new process that will run the function "divide". Prior to finishing, the process running the function "divide" will communicate the result of the division to its parent through a pipe. The process running the function add will read the pipe. The process running the function "divide" will then end. The process running "add" will then add the result read from the pipe and continue adding the other arguments until encountering the end-of-line character. In the second expression, there will be an extra process in the tree of processes when either S or M are encountered. At those points, the shell will have spawned a process running the function "divide". This process will have spawned a process running the function "add". This process will successively spawn a process running the function "subtract" and, once that process ends, a process running the function "multiply
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