LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Writing to and reading from a common file simultaneously via two different processes (https://www.linuxquestions.org/questions/programming-9/writing-to-and-reading-from-a-common-file-simultaneously-via-two-different-processes-931355/)

ravangrid 02-26-2012 06:24 AM

Writing to and reading from a common file simultaneously via two different processes
 
Dear All,

There are two processes. Process 1 writes to a file. Process 2 reads from the same file. This is simultaneous. But, the first process locks the file. So I am unable to do it. Here is my code.

// Writer

Code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

typedef struct parameter
{
        int count;
}param;

int main()
{
FILE *fp;
int i;
param p;

fp = fopen("data.txt","w+");
       
if(fp == NULL)
{
        printf("\nCannot open file.");
        exit(1);
}
               
i = 0;
while(1)
{
p.count = i++;               
printf("\nWriting count = %d",p.count);
                fwrite(&p,sizeof(p),1,fp);               
sleep(5);
}
       
fclose(fp);
return(0);
}

//Reader

Code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

typedef struct parameter
{
int count;
}param;

int main()
{
FILE *fp;
int x;
param p;       
               
fp = fopen("data.txt","r");
       
if(fp == NULL)
{
        printf("\nCannot open file.");
        exit(1);
}       
       
while(1)
{               
x = fread(&p,sizeof(p),1,fp );               
printf("\nReading:count =%d",p.count);               
sleep(1);
}       
       
printf("\nOut of while...");
fclose(fp);
return(0);
}

I have searched about it and stumbled across file-locking, but don't know how to code it. Then I came across one process writing and renaming the file etc. Any help will be appreciated. Thanks.

PS: I want two processes to communicate via a common file. I know it can be done via sockets, but this time I want to do it via common file because I want to learn how to do it this way properly.

dwhitney67 02-26-2012 06:30 AM

In the Writer application, after you perform the fwrite(), perform an fflush(). See if that makes a difference.

ravangrid 02-26-2012 06:42 AM

Thanks dwhitney67 for the reply.
Yes, it works after fflush(fp).
// I searched and learned that fflush flushes out the buffer associated with a stream.
Thanks.

The writer process opens a file and then enters an infinite loop. So the other process should not be allowed to open the file and get a file handle (?). But I guess that it was allowed in this case. Could you please explain it? Also, please tell me if there is a better way to do this via common file?

Thanks in advance.

ta0kira 03-04-2012 08:53 PM

Is there a reason you aren't using a named pipe? A named pipe will allow the reader to block for more data (instead of requiring a spinlock), with the drawback being that only one process can read from it. If you stick with the regular file, you should use advisory locks (see fcntl F_GETLK, etc.), anyway; otherwise, you might get partial-line reads from the reader while the writer is writing. Lastly, consider using "a+" instead of "w+".
Kevin Barry


All times are GMT -5. The time now is 02:52 AM.