Write in C/C++! Multiple Processes Controller Write a code called runsim.c (or r
ID: 3839009 • Letter: W
Question
Write in C/C++!
Multiple Processes Controller
Write a code called runsim.c (or runsim.cpp) to create and manage a group of processes that are running in parallel. The runsim program fork()s up to pr_limit processes at a time. The value for pr_limit is obtained as a command line argument. The children of runsim will run new programs. The name for a new executable program to run is obtained from standard input along with any command line arguments for that program. There are as many lines in standard input as the total number of processes to be run; there can be duplicate lines. The total number of processes to be run is >= pr_limit.
Here is a possible outline for the code:
How the runsim code will be tested
A test program will take two command line arguments: a sleep time and a repeat factor. The program loops repeat factor times. In its main loop, it sleeps the specified sleep time (see function sleep(), described section 3 of the man pages) and then outputs a message with its process ID to stderr.
To use the testing program with the runsim program, a data file that looks like the following is created:
and then runsim is executed:
Explanation / Answer
Please let me know if you have any doubt in below written code
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
const char* USAGE = "Usage: mycat [-n] [file ...] [@ file] [file ...] ";
const mode_t OPENMODE = S_IRUSR|S_IWUSR|S_IXUSR|
S_IRGRP|S_IWGRP|S_IXGRP|
S_IROTH|S_IWOTH|S_IXOTH;
const int OPENFLAGS = O_CREAT|O_TRUNC|O_WRONLY;
void writeLineNumber(int output, int number) {
char *stringValue;
sprintf(stringValue, "%d: ", number);
write(output, stringValue, strlen(stringValue));
}
void readfile(char* filename, int output, char numLines) {
int fd = open(filename, O_RDONLY);
int lineNum = 1;
if (fd < 0) {
write(output, "Failed to read file: ", 21);
write(output, filename, strlen(filename));
write(output, " ", 1);
return;
}
// Write the first line number, if it is desired
if (numLines) { writeLineNumber(output, lineNum); }
char *character;
while(read(fd, character, 1)) {
write(output, character, 1);
if (*character == ' ' && numLines) {
lineNum++;
writeLineNumber(output, lineNum);
}
}
// Newline at the end
write(output, " ", 1);
}
void readline(char *line) {
char *input;
int amt = read(0, input, 255);
int index;
for (index = 0; index < amt; index++) {
line[index] = input[index];
}
line[index] = 0;
}
void readInput(int output, char numLines) {
char *line;
char **input;
int inputIndex = 0;
while (1) {
readline(line);
if (!strcmp(line, ":q ")) { break; }
input[inputIndex] = line;
inputIndex++;
}
while (*input) {
write(output, *input, strlen(*input));
input++;
}
}
int main(int argc, char** argv) {
char numberLines = 0;
int output = 0; // By default, write to standard out
// Must have at least two arguments
if (argc < 2) {
write(0, USAGE, strlen(USAGE));
}
// Pre parse arguments
int argIndex;
for (argIndex = 1; argIndex < argc; argIndex++) {
if (!strcmp(argv[argIndex], "-n")) {
numberLines = 1;
}
else if(*argv[argIndex] == '@') {
// Jump to next character (from @ to ...)
argv[argIndex]++;
// If form is @filename
if (*argv[argIndex]) {
output = open(argv[argIndex], OPENFLAGS, OPENMODE);
}
// Else, from is @ filename (separate argument)
else {
output = open(argv[argIndex + 1], OPENFLAGS, OPENMODE);
}
// If output file cannot be opened
if (output < 0) {
output = 0;
write(output, "Couldn't open file to write to ", 31);
}
}
}
// Drop first argument
argv++;
// Now loop through and actually read
while (*argv) {
// If the argument is not -n
if (strcmp(*argv, "-n")) {
// If its an @ (specify write file)
// We skip it, and optionally jump an arugment
if (**argv == '@') {
// If the write file is not attached, skip the next argument
if (!*argv[1]) { argv++; }
}
// If just a dash, read a line of input
else if (!strcmp(*argv, "-")) {
readInput(output, numberLines);
}
// Else, just read the specified file
else {
readfile(*argv, output, numberLines);
}
}
// Go to next argument
argv++;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.