Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.
  • 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 #include #include #include int main(int argc, char * argv[]) { const char * filename="test.txt"; FILE * ft= fopen(filename, "wb") ; srand(time(NULL)); if (ft) { int i; for(i = 0; i < 1000000; i++){ int randomnum = rand() % 1000000; fwrite(&randomnum,sizeof(int),1,ft); } fclose( ft ) ; } return 0; }

Example code of how to read from the file:

#include #include #include #include #include int main(int argc, char * argv[]) { const char * filename="test.txt"; FILE * ft= fopen(filename, "rb") ; if (ft) { int pid = getpid(); fseek (ft,0,SEEK_END); //go to end of file long size = ftell(ft); //what byte in file am I at? fseek (ft,0,SEEK_SET); //go to beginning of file int num = (int)size / (int)sizeof(int); printf("size of the file: %li ,sizeof(int) = %i , the number of numbers = %i ", size, (int) sizeof(int), num); int i; for(i = 0; i < num; i++){ int temp = 0; fread(&temp,sizeof(int),1,ft); printf("%i: %i\t",pid,temp); } fclose( ft ) ; } printf(" "); return 0; }

Example of exec and pipe:

program that will be called from exec called "printstuff":

#include #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 #include #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

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

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions

Question

a. How do you think these stereotypes developed?

Answered: 1 week ago

Question

7. Describe phases of multicultural identity development.

Answered: 1 week ago