LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-03-2012, 04:23 AM   #1
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Rep: Reputation: Disabled
IPC using file pointer


Take the general scenario of a parent creating a child process. Parent process declares a file pointer *fp.
then it points it to a file say 'new1' by
fp = fopen("new1","w+"). Opening it for both reading and writing.
Now the parent process creates a child process. While parent process waits for the child, child uses
execlp("./child_new",0); calling a c file child_new.
In child_new we have same File pointer declared. Now can we use that file pointer to make changes in same file which we did in parent??

Can processes share a file pointer?
I read somewhere that we can but I tried and it is showing some segmentation fault.


******Parent************************
Code:
#include<stdio.h>
#include<fcntl.h>
main()
{
   FILE *fp;
fp = fopen("new1","w+");
if(fork()==0)
{
	printf("\nIn child process\n");
	execlp("./child_new",0);
	exit(1);
}
else  {
	wait();
	printf("In parent");
	exit(1);
      }
}
***********child****************
Code:
#include<stdio.h>
#include<fcntl.h>
main()
{
	FILE *fp;
	printf("In child file");
	fseek(fp,0L,0);
}
 
Old 06-03-2012, 01:03 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vikasbansal27 View Post
... it is showing some segmentation fault.
...
So, what did you do to debug the segmentation fault ? I.e. have you tried web search for, say, 'gdb debugging segmentation fault' ?
...
Opening file for both reading and writing at the same time is generally speaking a bad idea. In the end it's much easier to have two files:

from_parent_to_child - parent writes, child reads;
form_child_to_parent - child writes, parent read.

Think also about using shared memory for IPC.
 
Old 06-03-2012, 01:55 PM   #3
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hey thank you for telling me about gdb, that sure will help but in this program, I '//' both fseek and fp, then it was doing fine. So there is some problem with sharing file pointers. I searched on the internet, but there is not much about sharing file pointer technique.
 
Old 06-03-2012, 02:12 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vikasbansal27 View Post
...there is not much about sharing file pointer technique.
Start from something really simple and safe. E.g. create a text file with known contents, write your parent -> child code, write it in a manner that the parent simply reads the file line by line and outputs its contents to screen, then the child is created, the child rewinds the file and also outputs the text file contents to screen line by line.
 
Old 06-03-2012, 03:14 PM   #5
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
I made a very simple program..but I am getting an unusual output this program should show the 10 characters of file 'first' two times. But it is showing it three times.

Code:
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
FILE *fp;
char c;
int i,pid;
fp=fopen("first","r");
c=getc(fp);	
printf("\nIn Parent...\n");

for(i=0;i<10;i++)
{
	printf("%c",c);
	c=getc(fp);
}

pid=fork();
if(pid==0)
 {
	printf("\nIn Child...\n");
	fseek(fp,0,0);
	c=getc(fp);
	for(i=0;i<10;i++)
	{
		printf("%c",c);
		c=getc(fp);
	}
printf("\n");
 }
else {
	wait(NULL);
	exit(0);
}

}
first contains 'VikasBansal' as text.
output is

In Parent...
VikasBansa
In Child...
VikasBansa
VikasBansa

Last edited by vikasbansal27; 06-05-2012 at 06:10 AM.
 
Old 06-03-2012, 03:20 PM   #6
vikasbansal27
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
It is executing the loop just above fork() TWICE. But if I place a printf statement between them, it runs normal.
 
  


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
[SOLVED] IPC using file pointer vikasbansal27 Programming 3 05-31-2012 12:46 AM
Holding file pointer of deleted file postiwala Linux - Software 1 02-16-2012 11:11 AM
External File pointer C programming exvor Programming 4 06-07-2007 01:57 PM
switching between descriptor and file pointer? Thinking Programming 7 12-14-2005 12:46 AM
Getting file path from FILE pointer in C barisdemiray Programming 3 10-22-2004 12:58 AM

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

All times are GMT -5. The time now is 03:20 PM.

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