Code:In the following program,I have doubt in the second printf which is in the child process.Actually in file opreations,file descriptor will be shared among parent and child process. So,We should get output as 4107 in second printf. But instead of that i got 11. Please clarify me why it is happening!!! code: ==== #include <stdio.h> #include <memory.h> #include <stdlib.h> main() { FILE *fp; char buff[11]; int pid , i ; fp = fopen ( "book.txt" , "w+" ); for ( i = 0 ; i < 4096 ; i++ ) fprintf(fp , "A" ); for ( i = 0 ; i < 4096 ; i++ ) fprintf(fp , "B" ); for ( i = 0 ; i < 4096 ; i++ ) fprintf(fp , "C" ); for ( i = 0 ; i < 4096 ; i++ ) fprintf(fp , "D" ); fclose(fp); fp = fopen("book.txt","r"); pid = fork(); if ( pid == 0 ) { printf("Child starts...ftell -> %ld\n",ftell(fp)); fread ( buff , sizeof(buff) , 1 , fp ); buff[10] = '\0'; printf("Child -> After child fread -> ftell -> %ld %s\n",ftell(fp),buff); sleep(5); printf("Child -> After parent read ->ftell -> %ld %s\n",ftell(fp),buff); fread(buff,sizeof(buff),1,fp); buff[10] = '\0'; printf("Child -> After 2nd child fread -> ftell -> %ld %s\n",ftell(fp),buff); } else { sleep(1); printf("Initially in parent -> ftell -> %ld \n",ftell(fp)); fread(buff,sizeof(buff),1,fp); buff[10] = '\0'; printf("After parent read -> ftell -> %ld %s\n",ftell(fp),buff); } fclose(fp); } Thanks in advance. Regards, Rajisankar



LinkBack URL
About LinkBacks



