Assignment 1: Process Creation and File Operations (40 Points) 1 Objectives: Pra
ID: 3743967 • Letter: A
Question
Assignment 1: Process Creation and File Operations (40 Points) 1 Objectives: Practice with system calls to: a) perform file operations; and b) create and use processes. 2 Description: Overview: You will write a program (says wordcount.c) to find out the number of words in multiple text files. Specifically, the program will first determine the number of files to be processe Then, the program will create multiple processes with each process being responsible for one file to count its words. The typical format to run the program with input parameters is as follows: /wordcount File_1 File 2 File n Details: First, the program needs to determine the number of files to be processed. This can be done with the argc parameter of the main function; Then, the argv parameter can be used to retrieve the name for each file After that, fork) system call will be used to create multiple processes (one for each file). For each child process, it can simply invoke a function to count the number of words inside a specific file and print out the result like note that, no inter-process communication is required as child processes will report (i.e., print out) their results individually Child process for File_x: number of words is XXX The main process should wait until all child processes report their results; then, the main process report the end of the program with the print out: All n files have been counted! For word counting, you could simply use the space character as the delimiter. Anything that are not separated by the space will be counted as a single word. For instance, the example "The first program is a Hello-world." will be reported as 6 words (where Hello-world is counted as a single word). You could compare your results using the wc utility that is available on Linux machines. 3 Submission Requirements The assignment needs to be submitted on Blackboard. The submission includes two parts: 1. Source codes [35 points]: This assignment should have only one source file (wordcount.c) and multiple header files (as you lik. Your program needs to have proper comments (e.g explanations for functions and variables etc.). It will be graded based on both functional correctness and clearity of the necessary comments. 2. Report.txt [5 points]: In this text file, you need to report: a) the status of your program (completed or not; partial credit will be given even the program is not completed); b) the design of your program (what and how system calls are utilized etc.); c) what help (if any) you get from TA and/or your classmates; and d) comments and suggestions to improve this assignment 3. Submission: Please have all the above files zipped into a single file assignl-abc123.zip and upload the file on Blackboard, where abc123 should be your UTSA IDExplanation / Answer
#include <stdio.h>
#include<stdlib.h>
int count_words(char *filename,int ppid);
int main(int argc , char **argv)
{
//to store process id of the process
int *pid_arr;
int i,count,pid,parent_pid,status;
if(argc < 2)
{
printf("Usage: ./exename file1 file2 etc.. ");
return -1;
}
//create the proces equal to argc-1
pid =(int*)malloc((argc-1)*sizeof(int));
count = argc;
parent_pid=getpid();
for(i = 1; i < count; i++)
{
pid = fork();
if(pid == 0)
{
int words = count_words(argv[i],parent_pid);
if(words > 0)
{
printf("Child process for %s :number of words is %d ",argv[i],words);
}
}
else
{
if(parent_pid == getpid()) //parent process
wait(&status);
}
}
return 0;
}
int count_words(char *filename,int ppid)
{
FILE *fp;
int count=0;
char str[20];
//execute this function only if child process of parent, no gradchild is allowed to execute this function!
if(ppid == getppid())
{
fp = fopen(filename,"r");
if(fp == NULL)
{
printf("Not able to open %s ",filename);
return -1;
}
while(fscanf(fp,"%s",str)!=EOF)
{
count++;
}
return count;
}
else
{
return -1;
}
}
-----------------------------------------------------------
before executing above program , have some files to give as command line args, I have below files
//file1.txt
There are conventions for what sorts of status values certain programs should return.
The most common convention is simply 0 for success and 1 for failure.
Programs that perform comparison use a different convention: they use status 1 to indicate a mismatch,
and status 2 to indicate an inability to compare. Your program should follow an existing convention if
an existing convention makes sense for it.
---------------------------------------------
//file2.txt
When a program exits, it can return to the parent process a small amount of information about the cause of termination,
using the exit status. This is a value between 0 and 255 that the exiting process passes as an argument to exit.
--------------------------------------------------
//file3.txt
Warning: Don’t try to use the number of errors as the exit status.
-----------------------------------------------------
//output
Child process for file1.txt :number of words is 66
Child process for file2.txt :number of words is 43
Child process for file3.txt :number of words is 13