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);
}
}
}