Advertisement

11.01.2007 at 04:03PM PDT, ID: 22933734 | Points: 500
[x]
Attachment Details

How can I have multiple children proccesses read the same file at the same time?

Asked by habsfan50 in C Programming Language

Tags: , , ,

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
 
Loading Advertisement...
 
[+][-]11.01.2007 at 08:37PM PDT, ID: 20198294

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 01:00AM PDT, ID: 20198810

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 01:00AM PDT, ID: 20198811

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 01:57AM PDT, ID: 20198952

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 01:59AM PDT, ID: 20198961

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 02:02AM PDT, ID: 20198979

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 02:07AM PDT, ID: 20198991

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.02.2007 at 02:09AM PDT, ID: 20199001

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.20.2008 at 03:48PM PST, ID: 20943386

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628