Question
C Programming: Assignment : Write a program that will accept a number of arguments from the command-line - between 1 and 7, inclusive - each
C Programming:
Assignment: Write a program that will accept a number of arguments from the command-line - between 1 and 7, inclusive - each a unique integer between 0-9.
The program (parent process) will request shared memory for these integers, attach the shared memory to its own address space and fill the space (using an array) with the integers from the command-line. The parent will display the initial state of the shared memory and then spawn a number of child processes equal to the number of command-line arguments, and wait for each child to complete its tasks. Upon completion of all children, the parent will acknowledge said completion by storage and display of their respective exit codes, display the final state of the shared memory, detach the shared memory, remove the shared memory, and then terminate. Each child process will receive a unique ID upon initiation - an integer between 1 and the number of command-line arguments (maximum 7).
Each child will display the initial state of the shared memory and their private memory - their unique ID. Next each child will locate the array element (index) associated with their unique ID and then multiply that array element by their unique ID, storing the result in the same array element. Each child will then display the current state of the shared memory, provide their exit status/code, and exit.
Output: Each step (function?) that a process (parent or child) takes (starting, validating, requesting/removing shared memory, attaching/detaching shared memory, forking/completing child processes, exiting, etc.) will output a trace statement in order to show which statements are currently being executed. The parent will prefix each trace statement with 'Parent: ' and each child will prefix their trace statements with 'Child ID: ', where ID is their unique ID.
Input: Command-line input will be validated, as normal.
Requirements: Style requirements as discussed in class expected. Efficiency should always be considered. Always select the most appropriate loop/decision structures and variable/constant types. Functions should focus on a single task. Main() should be high-level tasks only.
Command-line could look something like:
$ ./SharedProcess 1 2 3 4 5 6 7
Include instructions on how to properly compile/execute your program to include the number of command-line argument(s).
Appropriate error handling should be included for the spawning of processes and for each step of the shared memory process. User header file is recommended. Use of MakeFile is required for this assignment. Refer to the Process Control and Fork() links bellow for details and options in spawning processes and sharing memory.
fork() System Call: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
Shared Memory protocol: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/process.html
exec() System Call: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/exec.html
The Open Group - exec functions - IEEE Std 1003.1: http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
Processes environment: http://www.thegeekstuff.com/2012/03/linux-processes-environment/
Processes memory layout: http://www.thegeekstuff.com/2012/03/linux-processes-memory-layout/
C - process control functions: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
C - fork function: http://www.thegeekstuff.com/2012/05/c-fork-function/
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