Matt Ramsay CS 375 EXAM 2 Part 1 Output: csserver:/home/mr56/cs375/exam2 > parent 1 75000 Multiples of 3 between 3 and 15000 add to 37507500 This total written to /home/mr56/tmp/file8771.out Multiples of 3 between 15003 and 30000 add to 112507500 This total written to /home/mr56/tmp/file8772.out Multiples of 3 between 30003 and 45000 add to 187507500 This total written to /home/mr56/tmp/file8773.out Multiples of 3 between 45003 and 60000 add to 262507500 This total written to /home/mr56/tmp/file8774.out Multiples of 3 between 60003 and 75000 add to 337507500 This total written to /home/mr56/tmp/file8775.out First fifth added to 37507500 Second fifth added to 112507500 Third fifth added to 187507500 Fourth fifth added to 262507500 Last fifth added to 337507500 Total added to 937537500 Parent Code: #include <iostream> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <libgen.h> #include <sys/stat.h> #include <sys/types.h>
#include <fcntl.h> #include <sys/wait.h> #include <dirent.h> #include <errno.h> #include <pwd.h> #include <grp.h> #include <time.h> using namespace std; int main(int argc, char *argv[]) if(argc!= 3) cout << "You didn't enter enough commmand line arguments, you are a fuck-stick\n"; DIR *dp; // file descriptor struct dirent *de; // another file descriptor struct stat statbuf; // struct for stat command int total = 0; // sum of numbers char inputsum[16] = ' '; // temp sum int blah, beread, fd; // file desciptors and counters int lower = atoi(argv[1]); // lower bound int upper = atoi(argv[2]); // upper bound int division = (upper-lower)/5; // divide by 5 if((upper-lower)%5!= 0) division++; char path[16] = ' '; // strings to save file names char blah1[27] = ' '; char blah2[27] = ' '; char blah3[27] = ' '; char blah4[27] = ' '; char blah5[27] = ' '; char first[5] = ' '; char second[5] = ' '; char third[5] = ' '; char fourth[5] = ' '; char fifth[5] = ' '; char firstb[5] = ' '; char secondb[5] = ' '; char thirdb[5] = ' '; char fourthb[5] = ' '; char fifthb[5] = ' ';
char file1[35] = ' '; char file2[35] = ' '; char file3[35] = ' '; char file4[35] = ' '; char file5[35] = ' '; pid_t pid1, pid2, pid3, pid4, pid5; //fork file descriptors sprintf(path, "/home/mr56/tmp/*"); //writing proper names into strings sprintf(first,"%d",lower); sprintf(firstb,"%d",division); sprintf(second,"%d",(division+1)); sprintf(secondb,"%d",(division*2)); sprintf(third,"%d",((division*2)+1)); sprintf(thirdb,"%d",(division*3)); sprintf(fourth,"%d",((division*3)+1)); sprintf(fourthb,"%d",(division*4)); sprintf(fifth,"%d",((division*4)+1)); sprintf(fifthb,"%d",upper); pid1 = fork(); // fork first process if(pid1 == 0) execl("exam2", "exam2", first, firstb, 0); // exec child pid2 = fork(); // fork second process if(pid2 == 0) execl("exam2", "exam2", second, secondb, 0); // exec child pid3 = fork(); // fork third process if(pid3 == 0) execl("exam2", "exam2", third, thirdb, 0); // exec child pid4 = fork(); // fork fourth process if(pid4 == 0) execl("exam2", "exam2", fourth, fourthb, 0); //exec child pid5 = fork(); // fork fifth process if(pid5 == 0)
execl("exam2", "exams", fifth, fifthb, 0); // exec child int stat_val; pid_t child_pid; pid1 = wait(&stat_val); // wait for children to finish pid2 = wait(&stat_val); pid3 = wait(&stat_val); pid4 = wait(&stat_val); pid5 = wait(&stat_val); if((dp = opendir("/home/mr56/tmp")) == NULL) //open directory cout << "Error opening /home/mr56/tmp" << endl; de = readdir(dp); // read first two files '.' and '..' de = readdir(dp); // and disregard if((de = readdir(dp))!= NULL) // save files from children to file1 - file 5 sprintf(file1, "%s", de->d_name); if((de = readdir(dp))!= NULL) sprintf(file2, "%s", de->d_name); if((de = readdir(dp))!= NULL) sprintf(file3, "%s", de->d_name); if((de = readdir(dp))!= NULL)
sprintf(file4, "%s", de->d_name); if((de = readdir(dp))!= NULL) sprintf(file5, "%s", de->d_name); closedir(dp); close directory sprintf(blah1, "/home/mr56/tmp/%s", file1); fd = open(blah1, O_RDONLY); //open first file beread = read(fd, inputsum, 12); // read partial sum blah = atoi(inputsum); //change to int cout << endl << "First fifth added to " << blah << endl; // add to total //close file //same for next four files sprintf(blah2, "/home/mr56/tmp/%s", file2); fd = open(blah2, O_RDONLY); beread = read(fd, inputsum, 12); blah = atoi(inputsum); cout << "Second fifth added to " << blah << endl; sprintf(blah3, "/home/mr56/tmp/%s", file3); fd = open(blah3, O_RDONLY); beread = read(fd, inputsum, 12); blah = atoi(inputsum); cout << "Third fifth added to " << blah << endl; sprintf(blah4, "/home/mr56/tmp/%s", file4); fd = open(blah4, O_RDONLY); beread = read(fd, inputsum, 12);
blah = atoi(inputsum); cout << "Fourth fifth added to " << blah << endl; sprintf(blah5, "/home/mr56/tmp/%s", file5); fd = open(blah5, O_RDONLY); beread = read(fd, inputsum, 12); blah = atoi(inputsum); cout << "Last fifth added to " << blah << endl; cout << "Total added to " << total << endl << endl; sprintf(path, "/home/mr56/tmp/%s", file1); // delete files made by children sprintf(path, "/home/mr56/tmp/%s", file2); sprintf(path, "/home/mr56/tmp/%s", file3); sprintf(path, "/home/mr56/tmp/%s", file4); sprintf(path, "/home/mr56/tmp/%s", file5); Child Code: #include <iostream> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <libgen.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <sys/wait.h> using namespace std; int main(int argc, char *argv[]) if(argc!= 3)
cout << "You didn't enter enough commmand line arguments, you are a fuck-stick\n"; char filename[35] = ' '; // string for filename char outputsum[20] = ' '; // string for number int fd, written; // file descriptors int sum = 0; // sum int id = getpid(); // id of child int lower = atoi(argv[1]); // lower bound int upper = atoi(argv[2]); // upper bound sprintf(filename,"/home/mr56/tmp/file%d.out",id); // make filename string if(lower < 3) // start at first multiple of three in range lower = 3; if(lower % 3 == 1) lower += 2; else if(lower % 3 == 2) lower += 1; for(int i = lower; i <= upper; i++) // loop for sum sum += i; i += 2; sprintf(outputsum, "%d", sum); //write sum to string fd = open(filename, O_RDWR O_CREAT); // open file and creates if not there chmod(filename, 777); // make file writeable written = write(fd, outputsum, 20); //write total to file //close file chmod(filename, 777); // unnesessary crap // output cout << endl << endl << "Multiples of 3 between " << lower << " and " << upper << " add to " << sum << endl; cout << "This total written to " << filename << endl;
Makefile: finish:parent.o exam2.o g++ -o parent parent.cpp g++ -o exam2 exam2.cpp parent.o:parent.cpp g++ -c parent.cpp exam2.o:exam2.cpp g++ -c exam2.cpp