Question
C program Question: This assignment is for the class to have hands-on experience with creating process, understanding the principles of process trees and parent-child relationships
C program Question:
This assignment is for the class to have hands-on experience with creating process, understanding the principles of process trees and parent-child relationships between processes and inter-process communication as processes are one of the most fundamental elements of any Operating System.
To complete this assignment, you have to write a C program that would serve as an enhanced terminal.
First of all, your program should terminate when it reads stop command, otherwise expecting for a command in a loop, which continues until some variable that you can use as a condition (that stop modifies) changes its value. (Note, it should perform a clean exit).
Second feature of your program, upon reading the onechild command, your shell should be able to spawn a child process and report parent and child process ids. Hint: fork(), getpid() and getppid() can be used for that
Thirdly, upon receiving addnumbers command, your program should be able to sum the numbers from consecutive input, until a zero is typed in. This should be done by setting up two pipes, forking a process, and in parent process start receiving the inputs, sending them to the child process. Child process should be reading from the pipe until zero is encountered,storing the sum into a temporary variable. After zero is read, the child process should send the total sum to the parent process and parent process should output the result
Fourth item to complete is: Upon reading exec input into the command line, your program should read arguments until an ; is encountered. After that, the command should be executed. To do this, your process should read the commands into an array of char* (i.e. char**) (up to five arguments). Further, your process should call the fork syscall, the parent should wait(NULL) for the child process, which in turn should execute the command using either of execvp(), exec(), execl(), execlp() syscall. Note, make sure you initialize your buffer strings to NULL explicitly as well as allocate and deallocate memory appropriately.
Final item is to extend your code from subtask number two, such that upon entering processtree #, MAXIMUM DEPTH IS 5 a process tree should be printed. Each process in the tree should report its pid, parents pid and generation (i.e. the child of the first parent will belong to the first generation, its children will belong to the second generation etc.). Another important aspect, each process should be tabulated as many times as there are generations from the parent process. You should use a for loop for spawning the processes, and a variable as the generation counter. To achieve similar output, the parent branch after calling fork(), should only wait for the children.
input/output format (For this assignment, you dont have to make sure that input/output redirection works. Please refer):
Please input your shell command: onechild Hello, I am a parent process 9103 Hello, I am a child process 9104 Please input your shell command: onechild Hello, I am a parent process 9103 Hello, I am a child process 9105 Please input your shell command: processtree Please input generation number 2 I am 9103, and I am a parent I am 9114 the child of 9103, i is 0, generation is 1 I am 9115 the child of 9114, i is 1, generation is 2 I am 9116 the child of 9103, i is 1, generation is 1 Please input your shell command: exec Please input exec subcommand ls ; a.out input.txt output.txt out.txt pipes.c solution.c Please input your shell command: addnumbers Please input numbers, (0) to terminate 54 Please input numbers, (0) to terminate 7 Please input numbers, (0) to terminate 12 Please input numbers, (0) to terminate 0 Sum is 73 Please input your shell command: stop
-------------------------------------------------
Please input your shell command: processtree 5 Please input generation number I am 9399, and I am a parent I am 9400 the child of 9399, i is 0, generation is 1 I am 9401 the child of 9400, i is 1, generation is 2 I am 9402 the child of 9401, i is 2, generation is 3 I am 9403 the child of 9402, i is 3, generation is 4 I am 9404 the child of 9403, i is 4, generation is 5 I am 9405 the child of 9402, i is 4, generation is 4 I am 9406 the child of 9401, i is 3, generation is 3 I am 9407 the child of 9406, i is 4, generation is 4 I am 9408 the child of 9401, i is 4, generation is 3 I am 9409 the child of 9400, i is 2, generation is 2 I am 9410 the child of 9409, i is 3, generation is 3 I am 9411 the child of 9410, i is 4, generation is 4 I am 9412 the child of 9409, i is 4, generation is 3 I am 9413 the child of 9400, i is 3, generation is 2 I am 9414 the child of 9413, i is 4, generation is 3 I am 9415 the child of 9400, i is 4, generation is 2 I am 9416 the child of 9399, i is 1, generation is 1 I am 9417 the child of 9416, i is 2, generation is 2 I am 9418 the child of 9417, i is 3, generation is 3 I am 9419 the child of 9418, i is 4, generation is 4 I am 9420 the child of 9417, i is 4, generation is 3 I am 9421 the child of 9416, i is 3, generation is 2 I am 9422 the child of 9421, i is 4, generation is 3 I am 9423 the child of 9416, i is 4, generation is 2 I am 9424 the child of 9399, i is 2, generation is 1 I am 9425 the child of 9424, i is 3, generation is 2 I am 9426 the child of 9425, i is 4, generation is 3 I am 9427 the child of 9424, i is 4, generation is 2 I am 9428 the child of 9399, i is 3, generation is 1 I am 9429 the child of 9428, i is 4, generation is 2 I am 9430 the child of 9399, i is 4, generation is 1 Please input your shell command: stop
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