Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Group members: Praneetha Sripada and Diya Chandra
- //How to compile the program:
- //First we need to compile the code gcc osproject1.c --std=c99
- //Next to run the file: ./a.out <number of file> <inputfiles name> <outputfile name>
- //Input files are input1.txt. input2.txt and input3.txt
- #include<stdio.h>//import standard i/p-o/p stream
- #include<stdlib.h>//import standard library
- #include<sys/signal.h>//import standard signalling
- #include<ctype.h>//import character library
- int fork(void);//fork function to create process
- int getpid(void);//stores pid of child
- int getppid(void);//stores pid of parent
- int wait(int *);//wait signal for parent to wait till end of process
- int createProcess(char[], char[]);//to create child process
- int freq(char[], char[]);//to find frequency of each alphabet occuring in the input file
- int tolower(int);
- int main(int argc, char *argv[]) {
- //argc is length of commandline statement,
- //char* is the length of buffer message & argv[] is the argument array
- if (argc <= 1) {//if n is not given
- printf("Arguments are not given.\n");//buffer error message
- exit(2);//exit if loop
- }
- int no_of_files = atoi(argv[1]);//value of n stored in no_of_files
- if (no_of_files + 3 != argc) {//condition to check if n value is correct
- printf("Input files count are not given properly.\n");//buffer error message
- exit(3);//exit if loop
- }
- int i = 0;
- for (; i < no_of_files; i++) {//call fork process to create child process
- createProcess(argv[i + 2], argv[no_of_files + 2]);//file list in commandline listed after n
- }
- printf("Exiting the Parent Process and all Child Processes are done.\n");//buffer message before
- //terminating
- return 0;
- }
- //Spawn New Child Processes
- int createProcess(char input[], char out[]) {
- int status = 1;
- if (fork() == 0) {//invoke fork function for creation of child
- printf("Child process: %d, parent process :%d\n", getpid(), getppid());//print child and parent pids'
- //frequency count function for the file
- freq(input, out);//call frequency function to find frequency
- exit(0);//exit process
- }
- //wait till the end of the processes
- wait(&status);
- return 0;
- }
- int freq(char rf[], char wf[]) {
- FILE *writefile;//declare readfile and writefile variables of 'file' type
- char ch;//variable to store each of the characters:a-z
- int count[26];//array to store each of characters:a-z
- int j = 0;
- for (; j < 26; j++) {
- count[j] = 0;
- }//creation of array having 26 contiguous memory spaces for each of
- //the 26 alphabets
- FILE *readfile = fopen(rf, "r");//file is read
- if (readfile == NULL) {//file not found
- fprintf(stderr, "File doesn't exist. Check the file!!\n");//standard error message when file not found
- return 0;
- }
- while ((ch = fgetc(readfile)) != EOF) {//condition to check we are not at the end of file
- if (ch == ' ' && ch == '\n') continue;//ignore spaces and new line statements in file
- count[tolower(ch) - 'a'] = count[tolower(ch) - 'a'] + 1;//move to next consecutive character
- }
- printf("Opening file: %s and reading data\n", rf);/*buffer message to guide user on the filename being read
- read characters and store the count in array.*/
- fclose(readfile);//close file after reading
- writefile = fopen(wf, "a");//file is written
- fprintf(writefile, "%s\n", rf);
- //write the frequency to the output file
- int i = 0;
- for (; i < 26; i++) {
- fprintf(writefile, "%c -> %d\n", i + 'a', count[i]);
- }
- fclose(writefile);//close file after writing
- printf("Writing frequency of Characters to output file is completed.\n");//buffer message to indicate
- //process completion
- //free(count);//free up the memory
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement