SHARE
TWEET

OS PROJECT DIYA

a guest Sep 17th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Group members: Praneetha Sripada and Diya Chandra
  2. //How to compile the program:
  3. //First we need to compile the code gcc osproject1.c --std=c99
  4. //Next to run the file: ./a.out <number of file> <inputfiles name> <outputfile name>
  5. //Input files are input1.txt. input2.txt and input3.txt
  6. #include<stdio.h>//import standard i/p-o/p stream
  7. #include<stdlib.h>//import standard library
  8. #include<sys/signal.h>//import standard signalling
  9. #include<ctype.h>//import character library
  10.  
  11. int fork(void);//fork function to create process
  12. int getpid(void);//stores pid of child
  13. int getppid(void);//stores pid of parent
  14. int wait(int *);//wait signal for parent to wait till end of process
  15. int createProcess(char[], char[]);//to create child process
  16. int freq(char[], char[]);//to find frequency of each alphabet occuring in the input file
  17. int tolower(int);
  18. int main(int argc, char *argv[]) {
  19.     //argc is length of commandline statement,
  20.     //char* is the length of buffer message & argv[] is the argument array
  21.     if (argc <= 1) {//if n is not given
  22.         printf("Arguments are not given.\n");//buffer error message
  23.         exit(2);//exit if loop
  24.     }
  25.     int no_of_files = atoi(argv[1]);//value of n stored in no_of_files
  26.     if (no_of_files + 3 != argc) {//condition to check if n value is correct
  27.         printf("Input files count are not given properly.\n");//buffer error message
  28.         exit(3);//exit if loop
  29.     }
  30.     int i = 0;
  31.     for (; i < no_of_files; i++) {//call fork process to create child process
  32.         createProcess(argv[i + 2], argv[no_of_files + 2]);//file list in commandline listed after n
  33.     }
  34.     printf("Exiting the Parent Process and all Child Processes are done.\n");//buffer message before
  35.     //terminating
  36.     return 0;
  37. }
  38.  
  39. //Spawn New Child Processes
  40. int createProcess(char input[], char out[]) {
  41.     int status = 1;
  42.     if (fork() == 0) {//invoke fork function for creation of child
  43.         printf("Child process: %d, parent process :%d\n", getpid(), getppid());//print child and parent pids'
  44.         //frequency count function for the file
  45.         freq(input, out);//call frequency function to find frequency
  46.         exit(0);//exit process
  47.     }
  48.     //wait till the end of the processes
  49.     wait(&status);
  50.     return 0;
  51. }
  52.  
  53. int freq(char rf[], char wf[]) {
  54.     FILE *writefile;//declare readfile and writefile variables of 'file' type
  55.     char ch;//variable to store each of the characters:a-z
  56.     int count[26];//array to store each of characters:a-z
  57.     int j = 0;
  58.     for (; j < 26; j++) {
  59.         count[j] = 0;
  60.     }//creation of array having 26 contiguous memory spaces for each of
  61.     //the 26 alphabets
  62.     FILE *readfile = fopen(rf, "r");//file is read
  63.     if (readfile == NULL) {//file not found
  64.         fprintf(stderr, "File doesn't exist. Check the file!!\n");//standard error message when file not found
  65.         return 0;
  66.     }
  67.     while ((ch = fgetc(readfile)) != EOF) {//condition to check we are not at the end of file
  68.         if (ch == ' ' && ch == '\n') continue;//ignore spaces and new line statements in file
  69.         count[tolower(ch) - 'a'] = count[tolower(ch) - 'a'] + 1;//move to next consecutive character
  70.     }
  71.     printf("Opening file: %s and reading data\n", rf);/*buffer message to guide user on the filename being read
  72.     read characters and store the count in array.*/
  73.  
  74.     fclose(readfile);//close file after reading
  75.     writefile = fopen(wf, "a");//file is written
  76.     fprintf(writefile, "%s\n", rf);
  77.     //write the frequency to the output file
  78.     int i = 0;
  79.     for (; i < 26; i++) {
  80.         fprintf(writefile, "%c -> %d\n", i + 'a', count[i]);
  81.     }
  82.     fclose(writefile);//close file after writing
  83.     printf("Writing frequency of Characters to output file is completed.\n");//buffer message to indicate
  84.     //process completion
  85.     //free(count);//free up the memory
  86.     return 0;
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top