Results 1 to 8 of 8
i'm tring to make 2 processes each read from the same file but only one of them read the file.
Code:
FILE * fileptr1;
fileptr1 = fopen("file1.txt","rt");
pid2=fork();
while(1)
{
...
- 03-19-2007 #1Just Joined!
- Join Date
- Mar 2007
- Posts
- 4
read a file using fork()
i'm tring to make 2 processes each read from the same file but only one of them read the file.
Code:FILE * fileptr1; fileptr1 = fopen("file1.txt","rt"); pid2=fork(); while(1) { fscanf(fileptr1,"%s",temp1); if(feof(fileptr1)==0) { printf("%i",getpid()); //id of current process printf(" Read: "); printf("%s",temp1); //what was just read printf("\n"); } else break; } if(pid2==0) fclose(fileptr1); //close reading file
- 03-19-2007 #2Just Joined!
- Join Date
- Mar 2007
- Posts
- 5
One of the reason is that you are using the same descriptor in the parent and the child process. Now when one of the process reads the file for the other one the if condition is false and so it does not read the file.
- 03-19-2007 #3Just Joined!
- Join Date
- Mar 2007
- Posts
- 4
i don't understand why is the if statement false? i thought that when you open a file before you fork both processes have a copy of the pointer to the file and should be able to read. i'm pretty sure i'm wrong do to the fact that this program doesn't work but can anyone explain to me what is really going on?
- 03-20-2007 #4Just Joined!
- Join Date
- Mar 2007
- Posts
- 5
From what I think is that the child process shares the open descriptors. Now when you open the file the file descriptor points(like a pointer) to the first character in the file. Due to scheduling by the kernel any of the process(child or parent) can run first. When the fscanf function is called some data is read and the descriptor points to the location after the data read and goes on til EOF and so it traverses through the file once and only once.
- 03-20-2007 #5Just Joined!
- Join Date
- Mar 2007
- Posts
- 5
From what I think is that the child process shares the open descriptors. Now when you open the file the file descriptor points(like a pointer) to the first character in the file. Due to scheduling by the kernel any of the process(child or parent) can run first. When the fscanf function is called some data is read and the descriptor points to the location after the data read and goes on til EOF and so it traverses through the file once and only once. I hope i explain the behavior you see. Do I?
- 03-20-2007 #6Just Joined!
- Join Date
- Mar 2007
- Posts
- 4
so you mean on process never gets a chance to read because one process might always get picked? i put a sleep(10) after the last printf("\n"); but that still didn't help still only one process reads the file. I also made the tried making the file huge but that didn't work either.
- 03-20-2007 #7Just Joined!
- Join Date
- Mar 2007
- Posts
- 5
Yes, try commenting the close file statement. and let the delay be there. And you will see both the processes print the file.
- 03-20-2007 #8Just Joined!
- Join Date
- Mar 2007
- Posts
- 4
ok i modified the code to be this:
and the output was this:Code:#include <stdio.h> #include <fcntl.h> main( ) { FILE *fileptr; char temp1[100]; char buff2[20]; int pid2; FILE * fileptr1; fileptr1 = fopen("file1.txt","rt"); pid2=fork(); printf("%i",getpid()); printf("just created \n"); while(1) { printf("%i",getpid()); printf("in the while loop\n"); fscanf(fileptr1,"%s",temp1); if(feof(fileptr1)==0) { printf("%i",getpid()); //id of current process printf(" Read: "); printf("%s",temp1); //what was just read printf("\n"); sleep(5); } else break; } }
so process 26017 is the only one that reads because it read first. and i have no idea why.Code:26017just created 26017in the while loop 26017 Read: m+n 26016just created 26016in the while loop 26017in the while loop 26017 Read: o+p 26017in the while loop 26017 Read: q+r+s 26017in the while loop 26017 Read: a+b+c+d 26017in the while loop


Reply With Quote