Find the answer to your Linux question:
Results 1 to 8 of 8
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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

  2. #2
    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.

  3. #3
    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?

  4. $spacer_open
    $spacer_close
  5. #4
    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.

  6. #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?

  7. #6
    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.

  8. #7
    Yes, try commenting the close file statement. and let the delay be there. And you will see both the processes print the file.

  9. #8
    ok i modified the code to be 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;
    	}
    }
    and the output was this:
    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
    so process 26017 is the only one that reads because it read first. and i have no idea why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •