There are \'S\' number of sections in a course. In each section,\'y\' number of
ID: 3765513 • Letter: T
Question
There are 'S' number of sections in a course. In each section,'y' number of quizzes are conducted. The average for each quiz in all the sections need to be determined. To solve this, Write a program in C which has the main process as Director process, which reads a file containing marks of all quizzesand creates 'x' number of manager processes. Each manager process will take care of solving for a section. Each manager will create 'y' number of worker processes and pass one quiz to each of them and calculate and print the average ?
Read 'x' and 'y' as input from tthe user. All quizzes are graded out of 20 points. Assume the number of students in all the sections is the same and is 10 per section. The input file should contain the data according to the value of 'x' and 'y' for x=2 and y= 2
The Process tree will look like the following (for x=2 and y=2)
DIRECTOR
MANAGER 1 MANAGER 2
WORKER 1 WORKER 2 WORKER 3 WORKER 4
note: The parent process is responsible for opening and closing the input text file. It stores the values in a 2-D integer array with 10 rows.
You may need to use the following c functions
fopen(), fscanf(), fseek(), fclose(). Use Unix method for their description
Thank you
18 13 11 5 14 9 10 9 5 20 18 11 12 17 2 13 10 19 6 15 9 2 9 10 11 15 13 12 16 8 17 13 18 14 14 10 15 10 5 8Explanation / Answer
File Handling in C with Examples (fopen, fread, fwrite, fseek)
by Himanshu Arora on July 9, 2012
As with any OS, file handling is a core concept in Linux. Any system programmer would learn it as one of his/her initial programming assignments. This aspect of programming involves system files.
Through file handling, one can perform operations like create, modify, delete etc on system files. Here in this article I try to bring in the very basic of file handling. Hope this article will clear the top layer of this multilayer aspect.
File handling functions
In this article, we will cover the following functions that are popularly used in file handling :
fopen()
The fopen() function is used to open a file and associates an I/O stream with it. This function takes two arguments. The first argument is a pointer to a string containing name of the file to be opened while the second argument is the mode in which the file is to be opened. The mode can be :
The fopen() function returns a FILE stream pointer on success while it returns NULL in case of a failure.
fread() and fwrite()
The functions fread/fwrite are used for reading/writing data from/to the file opened by fopen function. These functions accept three arguments. The first argument is a pointer to buffer used for reading/writing the data. The data read/written is in the form of ‘nmemb’ elements each ‘size’ bytes long.
In case of success, fread/fwrite return the number of bytes actually read/written from/to the stream opened by fopen function. In case of failure, a lesser number of byes (then requested to read/write) is returned.
fseek()
The fseek() function is used to set the file position indicator for the stream to a new position. This function accepts three arguments. The first argument is the FILE stream pointer returned by the fopen() function. The second argument ‘offset’ tells the amount of bytes to seek. The third argument ‘whence’ tells from where the seek of ‘offset’ number of bytes is to be done. The available values for whence are SEEK_SET, SEEK_CUR, or SEEK_END. These three values (in order) depict the start of the file, the current position and the end of the file.
Upon success, this function returns 0, otherwise it returns -1.
fclose()
The fclose() function first flushes the stream opened by fopen() and then closes the underlying descriptor. Upon successful completion this function returns 0 else end of file (eof) is returned. In case of failure, if the stream is accessed further then the behavior remains undefined.
The code
The code above assumes that you have a test file “test.txt” placed in the same location from where this executable will be run.
Initially the content in file is :
Now, run the code :
Again check the contents of the file test.txt. As you see below, the content of the file was modified.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.