Hello, I am trying to have a parent process fork many children processes and have each of these child processes read from a file I have opened at the same time. I do not want any of the children to fork and create new children processes, only the initial parent. I only want each child process to read a row corresponding to when they were created. For example, If the parent fork's once, then this child process only reads row 1. The next time the parent forks, this new child reads row 2, and etc,.
The file called "input.dat" is simple and in the following format : In this case I want to create 3 child processes to read their corresponding row
4 3 2 2 <- first child process reads this row
7 8 9 12 <- second child process reads this row
6 1 8 7 <- third child process reads this row
After reading these numbers, I have to do some calculations with only the numbers in their row and then write them to another file called "input.dat". Tthe end the parent process will wait until all the children processes are terminated. When they are all terminated the parent will read the "input.dat" file and start doing calculations based on these numbers.
Below is what I have so far:
Basically my problem is with fgets() I think. I am trying to loop to the row number corresponding to the number when the child process was created. When this row number is reached I will have my buffer full with the data from this line. The contents of the "input.dat" file are : 1 2 8 2
5 6 4 3
2 3 4 4
7 1 6 9
When I run the code below my output on the screen is this:
Here is buffer = 1 2 8 2 Here is buffer = 1 2 8 2
Here is buffer = The output should be -----> Here is buffer = 5 6 4 3
Here is buffer = 7 1 6 9 Here is buffer = 2 3 4 4
Here is buffer = Here is buffer = 7 1 6 9
Is the problem because one child process might be using fgets() on the file at the same time another child process is trying to use fgets() and only one is allowed at a time? If this is the case, how can I have all the children reading from the same file without any corruption? Also, writing to the file I would need child process one to write to the file first, then process two, etc. How could I do this? I just need help with reading and writing to the file, the rest I can manage. Thanks very much for all the help.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
void child( int, FILE*);
int main(){
int childpid;
FILE *fd1;
fd1 = fopen("input.dat","r");
//Open output.dat file later for writing
for ( int i = 1; i < 5; i++){
childpid = fork();
if (childpid == 0) { //child process
child( i,fd1 );
break;
}
}
// Close file streams
} //end of main
void child( int child_id, FILE *ptr){
char buffer[10];
//read from file
for (int j = 0; j < child_id; j++)
{
fgets(buffer, 10, ptr);
}
int len = strlen(buffer);
if ( buffer[len-1] == '\n')
buffer[len-1] = 0;
printf("Here is buffer = %s \n",buffer);
// Later I will tokenize the buffer to extract the number, this is why I remove the newline
// Do Calculations
// Write to input.dat file
} // end of child function
Start Free Trial