Question: Write a C program to implement a concurrent version of the wc command. You are to use fork, exec and wait/waitpid to achieve this. Name

Write a C program to implement a concurrent version of the wc command. You are to use fork, exec and wait/waitpid to achieve this. Name your C file concurrentwc.c. Your program must take 1 or more filenames as arguments(say n). Also you are expected to do error checking for all system calls and other parts wherever necessary like arguments check etc.

Your program should spawn n (n is the number of filenames from argv) number of child processes.

Each child process must call exec and run wc on each file which was supplied to your program. You are free to use execl or execv etc.

Your parent process must use wait or waitpid to collect children and examine their exit statuses. Make sure to print the exit status once the child process is collected, which can be done using WEXITSTATUS macro. Also note that you should only use this macro if WIFEXITED returns true (or a non-zero number in this case).

Example: If you run your program like this: ./concurrent file1 file2 file3 then you should spawn 3 child processes and each child in turn should exec and run wc command, so

child 1 would run(after exec call): wc file1

child 2 would run(after exec call): wc file2

child 3 would run(after exec call): wc file3

Hints:

Firstly you will have to create the above hierarchy as shown in the figure, which can be done using a for loop like we did in lab(master slave fork example).

Secondly you may have to figure out how to make sure that child 1 opens file1, child 2 opens file2 etc. The trick is to use some pointer arithmetic on argv.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!