LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2012, 06:24 AM   #1
ravangrid
LQ Newbie
 
Registered: Jun 2009
Posts: 12

Rep: Reputation: 0
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.
 
Old 02-26-2012, 06:30 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
In the Writer application, after you perform the fwrite(), perform an fflush(). See if that makes a difference.
 
1 members found this post helpful.
Old 02-26-2012, 06:42 AM   #3
ravangrid
LQ Newbie
 
Registered: Jun 2009
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 03-04-2012, 08:53 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading and writing to pipes, file descriptors, and file streams cmartin0 Programming 1 02-13-2012 03:03 AM
shortlist processes which are writing / reading to / from disks pbhat Linux - Server 3 04-04-2010 08:55 AM
writing and reading from a file! sahel Programming 1 12-27-2005 01:33 PM
vb.net reading and writing to a text file simultaneously mrobertson Programming 3 09-08-2005 12:30 PM
Help reading and writing to file xiste Programming 1 04-15-2005 12:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration