Question
Make these 2 C programS: From the command prompt the user will run the following: HW3 numForks fileName numForks can only be 1 or 4
- Make these 2 C programS:
- From the command prompt the user will run the following: "HW3 numForks fileName"
- "numForks" can only be "1" or "4" - it indicates the number of processes that will be run.
- "fileName" is the name of the binary random access file that will be used.
- If "1" is entered then the program calls exec on another c program that will return the max and min of the file to the user.
- if "4" is entered then the program calls fork an appropriate number of times and calls exec on another program the appropriate times and ways and will return the max and min of the file to the user. Your program will need to use pipes to communicate between forked processes.
- Regardless of "1" or "4" being entered, the program will report how much time it took to process the file.
- From the command prompt the user will run the following: "HW3 numForks fileName"
- When the program runs your name is printed out.
- The first c program will take care of the command line arguments and possibly deal with forking.
- A second c program will actually figure out the min and max from the file.
- more programs may be created, but use caution to not confuse yourself.
- Create a Makefile that compiles all of your c programs when the user types 'make' from the command prompt. programs must compile with 'make'.
develop on your own Linux version to be more comfortable with the environment.
The following is for help and guidance:
The code used for generating the file:
#include
Example code of how to read from the file:
#include
Example of exec and pipe:
program that will be called from exec called "printstuff":
#include
int main(int argc, char *argv[]) { printf("argc = %d ",argc); printf("argv[0] = %s ",argv[0]); if (argc != 2) { printf("Incorrect number of arguments, you must pass in a number. "); return -1; } else { printf("%s ",argv[1]); } int i; int max = atoi(argv[1]); for(i = 0; i < max; i++) { printf("%i \t",i); } printf(" "); return 5; }
Exec example (calls the above program):
#include
int main() { printf("This is before the fork. "); int cp[2]; if (pipe(cp) < 0) { printf("didn't work, couldn't not establish pipe. "); return -1; }
int pid = fork(); if (pid == 0) { printf("this is the child. not the original "); close(1); //close stdout dup2(cp[1], 1); //move stdout to pipe of cp[1] close(0); //close stdin close(cp[0]); //close pipe in execl("printStuff","printStuff", "5",(char *) 0); //note: All the arguments in exec have to be strings. } else {
close(cp[1]); //if you don't close this part of the pipe then the while loop (three lines down) will never return printf("this is the parent. the 'original.' "); char ch; while( read(cp[0], &ch, 1) == 1) { printf("%c",ch); //write(1, &ch, 1); //outcount++; } printf("all done. "); //execl("printStuff","printStuff", "10",(char *) 0); } printf("This is after the fork. "); return 0; }
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