Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

first code proc_manager.c second code CommandNode.c CommandNode.h result required #include #include #include #include #include #include #include #include #include CommandNode.h int main(int arge, char *argv[]) {

first code proc_manager.c

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

second code CommandNode.c

image text in transcribed

CommandNode.himage text in transcribed

result image text in transcribed

required

image text in transcribed

#include #include #include #include #include #include #include #include #include "CommandNode.h" int main(int arge, char *argv[]) { // Checks if more than 1 or no file name was entered if (arge 2) { printf("Invalid command input. Argument should only have 1 file "); exit(EXIT_FAILURE); } // Initialize variables for reading file FILE * fp; char * line = NULL; size_t len = ; ssize_t read; char newString[20][20]; int i, j, rowNum; // Initialize the linkedlist and necessary variables to help create the list CommandNode "head = NULL; CommandNode "currNode = NULL; CommandNode "prevNode = (Command Node*) mall sizeof(CommandNode)); int index Node = @; // Open the file specified by the terminal fp = fopen(argv[1], "r"); // Exit program if the file couldn't be opened if (fp == NULL) { exit(EXIT_FAILURE); } // Read each line of the file while ((read = getline(&line, &len, fp)) != -1) { // Replace in to NULL if (line[strlen(line) line[strlen(line) 1] == ' ') { 1] = 0; // Insert the command Line into a 2D array j=8; rowNum=; for(i = 0; i index) + 1); sprintf(errFileName, "%d.err", (currNode->index) + 1); int outFile = open(outFileName, O_RDWR | O_CREAT | O_APPEND); int errFile = open(errFileName, O_RDWR | O_CREAT | O_APPEND); chmod(outFileName, S_IRWXU); chmod(errFileName, S_IRWXU); dup2 (outFile, STDOUT_FILENO); dup2(errFile, STDERR_FILENO); // Begin timer and store start time into the current node clock_gettime(CLOCK_MONOTONIC, &start); currNode -> starttime = start.tv_sec; // Child process if (pid -- e) { currNode -> active = true; char *commands [20]; int index = @; // Add only the strings from command into a 10 commands array while (rownum command[index]) != '\0') { commands[index] = currNode -> command[index]; index += 1; } // End the command array with NULL at the end commands[index] = NULL; fprintf(stdout, "Starting command %d: child %d pid of parent %d ", currNode->index, getpid(), getppid()); fflush(stdout); // Execute the command execup(commands [@], commands); // If anything went wrong, print an error message perror(commands[0]); exit(2); } 1/ Parent process else { currNode -> PID = pid; } currNode = GetNextCommand(currNode); } // Parent waits for all children to finish execution int status; while ((pid = wait(&status)) >= 0) { CommandNode *finished Node = Find Command (head, pid); finishedNode -> active = false; // Calculate time taken for command to run clock_gettime(CLOCK_MONOTONIC, &finish); elapsed = (finish.tv_sec - (finishedNode -> starttime)); // Repoint stdout and stderr to the appropriate files char outFileName[10]; char errFileName[10]; sprintf(outFileName, "%d.out", (finishedNode->index) + 1); sprintf(errFileName, "%d.err", (finishedNode->index) + 1); int outFile = open(outFileName, O_RDWR | O_CREAT | O_APPEND); int errFile = open(errFileName, O_RDWR | O_CREAT O_APPEND); dup2 (outFile, STDOUT_FILENO); dup2(errFile, STDERR_FILENO); // Output termination status if (WIFEXITED(status)) { fprintf(stderr, "Exit with exitcode = %d ", WEXITSTATUS (status)); } else if (WIFSIGNALED(status)) { fprintf(stderr, "Killed with signal %d ", WTERMSIG (status)); } // Output process time fprintf(stdout, "Finished at %ld, runtime duration Xf ", finish.tv_sec, elapsed); fflush(stdout); // If child took more than 2 seconds, restart the process if (elapsed > 2) { pid = fork(); // Print fork error message if fork went wrong if (pid starttime = start.tv_sec; // Child node if (pid == 0) { finishedNode -> active = true; char *commands [20]; int index = 0; // Add only the strings from command into a ID commands array while (rowNum command[index]) != '\0') { commands[index] = finished Node -> command[index]; index += 1; } // End the command array with NULL at the end commands [index] = NULL; fprintf(stdout, "Starting command %d: child %d pid of parent %d ", currNode->index, getpid(), getppid()); fflush(stdout); // Execute the command execup(commands[@], commands); // If anything went wrong, print an error message perror( commands[0]); exit(2); } // Parent process else { finishedNode -> PID = pid; } } // If process executes less than 2 seconds else { fprintf(stderr, "Spawning too fast "); } } return ; } #include #include #include "CommandNode.h" //create a new command node. usually nextCmd can be NULL and function Insert CommandAfter can be called to insert after head node. void CreateCommandNode (CommandNode* thisNode, char cmd [20] [20], int ind, CommandNode* nextCmd, int commandLen) { // Copy cmd into thisNodes's command for(int i = 0; i command[i], cmd[i]); } thisNode->index = ind; thisNode->nextCommandPtr = nextCmd; return; } //insert node newNode after thisNode void Insert CommandAfter(CommandNode* thisNode, CommandNode* newNode) { CommandNode* tmpNext = NULL; tmpNext = thisNode->nextCommandPtr; thisNode->nextCommandPtr = new Node; newNode->nextCommandPtr = tmpNext; return; } //get next command node in Linked List CommandNode* GetNextCommand(CommandNode* thisNode) { return thisNode->nextCommandPtr; } //find a command based on the pid CommandNode* FindCommand(CommandNode* cmd, int pid) { CommandNode* tmpNext = cmd; while (tmpNext != NULL) { if (tmpNext->PID == pid) { return tmpNext; } tmpNext = tmpNext->nextCommandPtr; } return NULL; include typedef struct command_struct { char command[20][20]; int index; int PID; int starttime; bool active; struct command_struct* nextCommandPtr; } CommandNode; void CreateCommandNode (CommandNode* thisNode, char cmd [20][20], int ind, CommandNode* nextCmd, int commandLen); void Insert CommandAfter(CommandNode* thisNode, CommandNode* newNode); CommandNode* GetNextCommand(CommandNode* thisNode); CommandNode* FindCommand(CommandNode* cmd, int pid); cs149@cs149-VirtualBox:-/ass3$ gcc -o pro_manager pro_manager.c CommandNode.c -Wall -Werror -I. cs149@cs149-VirtualBox:~/ass 3$/pro_manager comfile cs149@cs149-VirtualBox:~/ass3$ cat 1.out cat: 1.out: No such file or directory $ ./proc_manager cmdfile $ cat 2353.out Starting command 1: child 2353 pid of parent 2234 Finished child 2353 pid of parent 2234 $ cat 2353.err Exited with exitcode = 0 $ cat 2363.out Starting command 2: child 2363 pid of parent 2234 cat cmdfile sleep 5 ls -latr sleep 3 pwd sleep 1 WC /etc/passwd #include #include #include #include #include #include #include #include #include "CommandNode.h" int main(int arge, char *argv[]) { // Checks if more than 1 or no file name was entered if (arge 2) { printf("Invalid command input. Argument should only have 1 file "); exit(EXIT_FAILURE); } // Initialize variables for reading file FILE * fp; char * line = NULL; size_t len = ; ssize_t read; char newString[20][20]; int i, j, rowNum; // Initialize the linkedlist and necessary variables to help create the list CommandNode "head = NULL; CommandNode "currNode = NULL; CommandNode "prevNode = (Command Node*) mall sizeof(CommandNode)); int index Node = @; // Open the file specified by the terminal fp = fopen(argv[1], "r"); // Exit program if the file couldn't be opened if (fp == NULL) { exit(EXIT_FAILURE); } // Read each line of the file while ((read = getline(&line, &len, fp)) != -1) { // Replace in to NULL if (line[strlen(line) line[strlen(line) 1] == ' ') { 1] = 0; // Insert the command Line into a 2D array j=8; rowNum=; for(i = 0; i index) + 1); sprintf(errFileName, "%d.err", (currNode->index) + 1); int outFile = open(outFileName, O_RDWR | O_CREAT | O_APPEND); int errFile = open(errFileName, O_RDWR | O_CREAT | O_APPEND); chmod(outFileName, S_IRWXU); chmod(errFileName, S_IRWXU); dup2 (outFile, STDOUT_FILENO); dup2(errFile, STDERR_FILENO); // Begin timer and store start time into the current node clock_gettime(CLOCK_MONOTONIC, &start); currNode -> starttime = start.tv_sec; // Child process if (pid -- e) { currNode -> active = true; char *commands [20]; int index = @; // Add only the strings from command into a 10 commands array while (rownum command[index]) != '\0') { commands[index] = currNode -> command[index]; index += 1; } // End the command array with NULL at the end commands[index] = NULL; fprintf(stdout, "Starting command %d: child %d pid of parent %d ", currNode->index, getpid(), getppid()); fflush(stdout); // Execute the command execup(commands [@], commands); // If anything went wrong, print an error message perror(commands[0]); exit(2); } 1/ Parent process else { currNode -> PID = pid; } currNode = GetNextCommand(currNode); } // Parent waits for all children to finish execution int status; while ((pid = wait(&status)) >= 0) { CommandNode *finished Node = Find Command (head, pid); finishedNode -> active = false; // Calculate time taken for command to run clock_gettime(CLOCK_MONOTONIC, &finish); elapsed = (finish.tv_sec - (finishedNode -> starttime)); // Repoint stdout and stderr to the appropriate files char outFileName[10]; char errFileName[10]; sprintf(outFileName, "%d.out", (finishedNode->index) + 1); sprintf(errFileName, "%d.err", (finishedNode->index) + 1); int outFile = open(outFileName, O_RDWR | O_CREAT | O_APPEND); int errFile = open(errFileName, O_RDWR | O_CREAT O_APPEND); dup2 (outFile, STDOUT_FILENO); dup2(errFile, STDERR_FILENO); // Output termination status if (WIFEXITED(status)) { fprintf(stderr, "Exit with exitcode = %d ", WEXITSTATUS (status)); } else if (WIFSIGNALED(status)) { fprintf(stderr, "Killed with signal %d ", WTERMSIG (status)); } // Output process time fprintf(stdout, "Finished at %ld, runtime duration Xf ", finish.tv_sec, elapsed); fflush(stdout); // If child took more than 2 seconds, restart the process if (elapsed > 2) { pid = fork(); // Print fork error message if fork went wrong if (pid starttime = start.tv_sec; // Child node if (pid == 0) { finishedNode -> active = true; char *commands [20]; int index = 0; // Add only the strings from command into a ID commands array while (rowNum command[index]) != '\0') { commands[index] = finished Node -> command[index]; index += 1; } // End the command array with NULL at the end commands [index] = NULL; fprintf(stdout, "Starting command %d: child %d pid of parent %d ", currNode->index, getpid(), getppid()); fflush(stdout); // Execute the command execup(commands[@], commands); // If anything went wrong, print an error message perror( commands[0]); exit(2); } // Parent process else { finishedNode -> PID = pid; } } // If process executes less than 2 seconds else { fprintf(stderr, "Spawning too fast "); } } return ; } #include #include #include "CommandNode.h" //create a new command node. usually nextCmd can be NULL and function Insert CommandAfter can be called to insert after head node. void CreateCommandNode (CommandNode* thisNode, char cmd [20] [20], int ind, CommandNode* nextCmd, int commandLen) { // Copy cmd into thisNodes's command for(int i = 0; i command[i], cmd[i]); } thisNode->index = ind; thisNode->nextCommandPtr = nextCmd; return; } //insert node newNode after thisNode void Insert CommandAfter(CommandNode* thisNode, CommandNode* newNode) { CommandNode* tmpNext = NULL; tmpNext = thisNode->nextCommandPtr; thisNode->nextCommandPtr = new Node; newNode->nextCommandPtr = tmpNext; return; } //get next command node in Linked List CommandNode* GetNextCommand(CommandNode* thisNode) { return thisNode->nextCommandPtr; } //find a command based on the pid CommandNode* FindCommand(CommandNode* cmd, int pid) { CommandNode* tmpNext = cmd; while (tmpNext != NULL) { if (tmpNext->PID == pid) { return tmpNext; } tmpNext = tmpNext->nextCommandPtr; } return NULL; include typedef struct command_struct { char command[20][20]; int index; int PID; int starttime; bool active; struct command_struct* nextCommandPtr; } CommandNode; void CreateCommandNode (CommandNode* thisNode, char cmd [20][20], int ind, CommandNode* nextCmd, int commandLen); void Insert CommandAfter(CommandNode* thisNode, CommandNode* newNode); CommandNode* GetNextCommand(CommandNode* thisNode); CommandNode* FindCommand(CommandNode* cmd, int pid); cs149@cs149-VirtualBox:-/ass3$ gcc -o pro_manager pro_manager.c CommandNode.c -Wall -Werror -I. cs149@cs149-VirtualBox:~/ass 3$/pro_manager comfile cs149@cs149-VirtualBox:~/ass3$ cat 1.out cat: 1.out: No such file or directory $ ./proc_manager cmdfile $ cat 2353.out Starting command 1: child 2353 pid of parent 2234 Finished child 2353 pid of parent 2234 $ cat 2353.err Exited with exitcode = 0 $ cat 2363.out Starting command 2: child 2363 pid of parent 2234 cat cmdfile sleep 5 ls -latr sleep 3 pwd sleep 1 WC /etc/passwd

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

Data Analytics Systems Engineering Cybersecurity Project Management

Authors: Christopher Greco

1st Edition

168392648X, 978-1683926481

More Books

Students also viewed these Databases questions

Question

7-16 Compare Web 2.0 and Web 3.0.

Answered: 1 week ago