0

Question

Hello guys, I am trying to make two processes read the same file. That is easy, but I need the two processes to read the file in such a way that they will not read the same data, aka when one process reads and changes the position of the pointer, it then changes the pointer on the other process till EOF. How do I achieve this?

My current implementation will read the file for each process but it then reads from a-z on both.

I want the input of

acdef

to then give the output of

program1 read: a
program2 read: b
program1 read: c
program2 read: d
program1 read: e
program2 read: f
EOF found, exiting program

Background

the of background of the project I am building for this operating systems course. This project will execute a program named program1 then that program will create 9 pipes one semaphore, one shared memory which counts the numbers of characters that have been read and then also takes in command line arguments for the filename. The first program will also after creating everything else spawn 9 children and pass them all the previous information, one pipe going from program1 to the specific child, the semaphore, the shared memory location, the filename.

Each child will have synchronized access to a file, shared memory. Each child will then read 1-10 characters at random and then increment the shared memory by that amount. The children then will send the name of the program that currently has access and the characters that were read and write them into the pipe. If they get to end of line they will then also write the total number of read character read in the same string that gotten written into the pipe. When the specific child does this it will then pass the execution to program1 for execution.

When program one gets execution privileges by the semaphore then it will read the string sent through all the nonblocking pipes. Then writes that string to the output file and then frees the semaphore for any of the children to use again till the eof has been reached.

Program Attempt

This is a sample program to show what is going wrong. I am trying to pass the file name to the child Then both the parent and the child are trying to read the file at the same time in synchronous.

Parent

#define  key (3000)

int main() 
{ 


    FILE *fPTR;
    int c, semID;

    struct sembuf operations[1];


    pid_t PID1;

    int argCount = 2;
    int size = 20;

    union semun {


        int value;
        struct semid_ds *buffer;
        ushort *array;

    } arg;


    char **args = (char **) malloc( (argCount) * (sizeof (char *) ));
    args[0] =     (char *)  malloc( (size)     * (sizeof (char  ) ));
    args[1] =     (char *)"inputTest.txt";



    semID = semget(key, 3, 0666 | IPC_CREAT);


    fPTR = fopen("inputTest.txt", "r");


    if(semctl(semID, 0, SETVAL, arg) < 0){


        fprintf(stderr, "was unable to set value of sempahore 0" );
        exit(0);
    }



    PID1 = fork();



    if(PID1 == 0){

        sprintf(args[0], "%d", semID);


        if(execvp("./TestReadingChild", args) == -1){

            printf("failed to execute TestReadingChild\n");
            exit(0);
        }

    }

    if(PID1 >= 0){


        while(1){


            if(semctl(semID, 0, GETVAL, arg) == 0){

                if((c = fgetc(fPTR)) != EOF){

                    printf("Program1 read: %c\n", (char) c);
                }else{

                    fclose(fPTR);
                    break;
                }





                operations[0].sem_num = 0;
                operations[0].sem_op = 1;
                operations[0].sem_flg = 0;

                semop(semID, operations, 1);


            }


        }
    }


}

Child

#define  key (3000)

int main(int argc, char *argv[]) 
{ 

    char *fileName = argv[1];
    FILE *fPTR;
    int semID = atoi(argv[0]);
    int c;

    struct sembuf operations[1];

    pid_t PID1;

    int argCount = 2;
    int size = 20;

    union semun {


        int value;
        struct semid_ds *buffer;
        ushort *array;

    } arg;

    char **args = (char **) malloc( (argCount) * (sizeof (char *) ));
    args[0] =     (char *)  malloc( (size)     * (sizeof (char  ) ));
    args[1] =     (char *)  fileName;


    semID = semget(key, 3, 0666 | IPC_CREAT);
    fPTR = fopen(fileName, "r");


    while(1){


        if(semctl(semID, 0, GETVAL, arg) == 1){

            if((c = fgetc(fPTR)) != EOF){

                printf("Program2 read: %c\n", (char) c);
            }else{
                fclose(fPTR);
                break;
            }





            operations[0].sem_num = 0;
            operations[0].sem_op = -1;
            operations[0].sem_flg = 0;

            semop(semID, operations, 1);
        }


    }
}
Fel
  • 15
  • 5
  • make a shared memory between two processes and put file pointer into that – Willy Cole Dec 11 '19 at 06:30
  • To do then I would take it I would have to make the size of the mmap be sizeof(FILE)? The. It would just update the filepointer location every time I would end up reading the character than I suppose. – Fel Dec 11 '19 at 09:07
  • no! just put the file pointer on shared memory, i.e. u need to allocate a memory in shm with sizeof(FILE *) which is just an integer value; but beware of shm concurrency when two processes want to access it. u can do this with semaphores – Willy Cole Dec 11 '19 at 09:42

1 Answers1

0

make a shared memory between two processes and put file pointer into that.

to do so, just put the file pointer on shared memory, i.e. u need to allocate a memory in shm with sizeof(FILE *) which is just an integer value; but beware of shm concurrency when two processes want to access it. u can do this with semaphores.

Willy Cole
  • 134
  • 9
  • Awesome, sounds good. Seems like a pretty easy solution to my problem. Thanks. Now that I already know how to use the shared memory – Fel Dec 11 '19 at 17:23
  • Okay so then how do I assign the file pointer to the memory address? That one is kinda weird to me. – Fel Dec 11 '19 at 17:34
  • @Fel u should use `shm` apis like `shm_open` and `mmap`; see https://stackoverflow.com/questions/43923518/how-to-put-a-variable-in-a-shared-memory – Willy Cole Dec 12 '19 at 06:56