



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Note: Submit before midnight of April 26. More importantly, no late submission is accepted.
Requirements:
Write a C program which reads the file âinput.txtâ and counts the occurrence frequency of each digit (0,
1, 2, ..., 9) and letter (A/a, B/b, âŚ, Z/z). Then, use fork() to duplicate a child process to write the most
frequent digit and letter at the end of the file âinput.txtâ. The parent process output the occurrence
frequency of each digit and letter to the terminal (using âprintfâ). You must invoke the function
âmostfrequentâ (which is defined as follows) in your program (it can be used for calculating the most
frequent digit and letter).
int mostfrequent(int * a, int length)/length is the length of the array. The function returns the index of the largest value of array a./
{
int index=0; int max=a[0]; int i; for(i=1;i<length;i++) if (a[i]>max) { max = a[i]; index = i; } return index;
}
You can download the demo of Project 3 from the following link.
http://www.cs.gsu.edu/~chunyuai/3320/project/project
Running example:
[chunyuai@grid ~]$ cat input.txt
Connecting workds.
Most acaemic writting is make up of complex sentences.
Appendix
/counting occurring frequency/ #include <stdio.h>
int mostfrequent(int * a, int length)/length is the length of the array. The function returns the index of the largest value of array a./ { int index=0; int max=a[0]; int i; for(i=1;i<length;i++) if (a[i]>max) { max = a[i]; index = i; } return index; }
int main() { char c; int i, nwhite, nother; int ndigit[10];
nwhite = nother = 0; for (i=0; i<10; ++i) ndigit[i] = 0; while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[câ'0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digits = "); for (i=0; i<10; ++i) printf("%d ",ndigit[i]); printf(", white space = %d, other = %d\n",nwhite, nother); printf("The most frequent digit is '%c'\n",'0'+mostfrequent(ndigit,10)); }
/*Process and file read and write. */ #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
int main(int argc, char * argv[]) {
int fd; char c; int i; int pid; char str[1000];
if(argc>1) /argc is the number of command line parameters./ fd=open(argv[1], O_RDWR, 0); /argv[1] is the first command line parameter./ else fd=open("input.txt", O_RDWR, 0);/if no command line parameter, open file input.txt/ if (fd==â1) { printf("Open file failed.\n"); return(0); } else { while (read(fd,&c,1)) { printf("I read a letter '%c'\n",c); } } pid=fork(); /duplicate a process/ if (pid==0) /child process/ { memset(str,'\0',1000); /init str to be empty./ strcat(str,"\nThe child process writes the file.\n"); write(fd,str,strlen(str)); close(fd); } else /parent process/ { printf("\n The parent process outputs to the terminal.\n" ); } return(1);