Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

t below is the wc.c file #include wc.h #include #include int main(int argc, char **argv) { long fsize; FILE *fp; count_t count; struct timespec begin,

image text in transcribedt

below is the wc.c file

#include "wc.h"

#include

#include

int main(int argc, char **argv)

{

long fsize;

FILE *fp;

count_t count;

struct timespec begin, end;

int nChildProc = 0;

/* 1st arg: filename */

if(argc

printf("usage: wc [# processes] [crash rate] ");

return 0;

}

/* 2nd (optional) arg: number of child processes */

if (argc > 2) {

nChildProc = atoi(argv[2]);

if(nChildProc

if(nChildProc > 10) nChildProc = 10;

}

/* 3rd (optional) arg: crash rate between 0% and 100%. Each child process has that much chance to crash*/

if(argc > 3) {

crashRate = atoi(argv[3]);

if(crashRate

if(crashRate > 50) crashRate = 50;

printf("crashRate RATE: %d ", crashRate);

}

printf("# of Child Processes: %d ", nChildProc);

printf("crashRate RATE: %d ", crashRate);

count.linecount = 0;

count.wordcount = 0;

count.charcount = 0;

// start to measure time

clock_gettime(CLOCK_REALTIME, &begin);

// Open file in read-only mode

fp = fopen(argv[1], "r");

if(fp == NULL) {

printf("File open error: %s ", argv[1]);

printf("usage: wc ");

return 0;

}

// get a file size

fseek(fp, 0L, SEEK_END);

fsize = ftell(fp);

printf("%d ", ftell(fp));

/* word_count() has 3 arguments.

* 1st: file descriptor

* 2nd: starting offset

* 3rd: number of bytes to count from the offset

*/

count = word_count(fp, 0, fsize);

fclose(fp);

clock_gettime(CLOCK_REALTIME, &end);

long seconds = end.tv_sec - begin.tv_sec;

long nanoseconds = end.tv_nsec - begin.tv_nsec;

double elapsed = seconds + nanoseconds*1e-9;

printf(" ========= %s ========= ", argv[1]);

printf("Total Lines : %d ", count.linecount);

printf("Total Words : %d ", count.wordcount);

printf("Total Characters : %d ", count.charcount);

printf("======== Took %.3f seconds ======== ", elapsed);

return(0);

}

and below is another file wc_core.c for reference

/*************************************************

* C program to count no of lines, words and *

* characters in a file. *

*************************************************/

#include "wc.h"

#include

#include

int crashRate = 0;

count_t word_count(FILE* fp, long offset, long size)

{

char ch;

long rbytes = 0;

count_t count;

// Initialize counter variables

count.linecount = 0;

count.wordcount = 0;

count.charcount = 0;

printf("[pid %d] reading %ld bytes from offset %ld ", getpid(), size, offset);

if(fseek(fp, offset, SEEK_SET)

printf("[pid %d] fseek error! ", getpid());

}

while ((ch=getc(fp)) != EOF && rbytes

// Increment character count if NOT new line or space

if (ch != ' ' && ch != ' ') { ++count.charcount; }

// Increment word count if new line or space character

if (ch == ' ' || ch == ' ') { ++count.wordcount; }

// Increment line count if new line character

if (ch == ' ') { ++count.linecount; }

rbytes++;

}

srand(getpid());

printf("%d", getpid());

if(crashRate > 0 && (rand()%100

{

printf("[pid %d] crashed. ", getpid());

abort();

}

return count;

}

Multi-process Word Counting Program (50%) The main problem of a single-process program is the scalability. It cannot scale large number of words. To address the problem, you will convert the word counting program into the multi- process model. The main process creates multiple child processes and effectively divides work between child processes. The child process sends the result to the main process via Inter-process communication channel. We will use pipe in this project. The main process waits children processes and reads the result via IPC channel, and prints out the total on the screen. You will modify "wce to a multi-process model. The program receives the number of child processes and an input file name through the command-line argument. o 1 argument target file - 2nd argument # of child processes (default is 0) 3rd argument crash rate (default is 0%) Example:./wc./large.Dxt 4 Explain your program structure and IPC in README.pdf file. Only "pdf" format will be accepted. It is slightly modified from the code in http://www.opentechguides.com/how- to/article/6/72/c-file-counts.html Crash-handling (50%) If one or more child processes crash before they complete the job (before sending the result through pipe), the final output will be incorrect. In this project, we will monitor the exit status of each child. If there is one or more child processes crashed, we will create new child processes to complete the job. The parent process should wait for all child processes and monitor their exit status (hint: use waitpid). If an exit status of a child process is abnormal termination by a signal, the parent process creates a new child process to re-do the incomplete job. You can use 3rd command-line arguments integer between 1 and 50) to trigger crash. 50 means each child process has 50% chance to be killed abnormally by signal. O means 0% chance to crash. Explain how your program handles crash in README pdf file. Submission Submit a tarball file using the following command % tar czvfp1.tar.gz README.pdf Makefile we_multi. 1. README.pdf file with: a. Your name b. Explain your design of multi-process structure and IPC c Explain how your program handles crash. 2. Your code should be compiled in odin machine. 3. Submit a tarball through ELC. Multi-process Word Counting Program (50%) The main problem of a single-process program is the scalability. It cannot scale large number of words. To address the problem, you will convert the word counting program into the multi- process model. The main process creates multiple child processes and effectively divides work between child processes. The child process sends the result to the main process via Inter-process communication channel. We will use pipe in this project. The main process waits children processes and reads the result via IPC channel, and prints out the total on the screen. You will modify "wce to a multi-process model. The program receives the number of child processes and an input file name through the command-line argument. o 1 argument target file - 2nd argument # of child processes (default is 0) 3rd argument crash rate (default is 0%) Example:./wc./large.Dxt 4 Explain your program structure and IPC in README.pdf file. Only "pdf" format will be accepted. It is slightly modified from the code in http://www.opentechguides.com/how- to/article/6/72/c-file-counts.html Crash-handling (50%) If one or more child processes crash before they complete the job (before sending the result through pipe), the final output will be incorrect. In this project, we will monitor the exit status of each child. If there is one or more child processes crashed, we will create new child processes to complete the job. The parent process should wait for all child processes and monitor their exit status (hint: use waitpid). If an exit status of a child process is abnormal termination by a signal, the parent process creates a new child process to re-do the incomplete job. You can use 3rd command-line arguments integer between 1 and 50) to trigger crash. 50 means each child process has 50% chance to be killed abnormally by signal. O means 0% chance to crash. Explain how your program handles crash in README pdf file. Submission Submit a tarball file using the following command % tar czvfp1.tar.gz README.pdf Makefile we_multi. 1. README.pdf file with: a. Your name b. Explain your design of multi-process structure and IPC c Explain how your program handles crash. 2. Your code should be compiled in odin machine. 3. Submit a tarball through ELC

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 Visualization A Practical Introduction

Authors: Kieran Healy

1st Edition

0691181624, 978-0691181622

More Books

Students also viewed these Databases questions

Question

Discuss what would be covered in a first interview with an athlete.

Answered: 1 week ago

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