C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-15-2003, 07:57 AM   #1
Registered User
 
Join Date: Sep 2003
Posts: 224
Trouble sharing files between processes.

I'm having trouble sharing files between processes. Here's what I have:

parent.c

Code:
#include <unistd.h>
#include <fcntl.h>

#define OUT STDOUT_FILENO
#define ERR STDERR_FILENO

int main()
{
	int handle, pid;
	char ch;

	if((handle = open("temp", O_CREAT|O_RDWR|O_TRUNC, S_IREAD|S_IWRITE)) < 0)
	{
		write(ERR, "open\n", 5);
		exit(0);
	}

	if((pid = fork()) < 0)
	{
		write(ERR, "fork\n", 5);
		exit(0);
	}
	else if(pid)
	{
		for(ch = 'a'; ch <= 'z'; ch++)
			write(handle, &ch, 1);
	}
	else
	{
		char *file[3];

		sprintf(file, "%d", handle);
		execlp("./child", file, (char *) NULL);
	}

	close(handle);

	exit(0);
}
child.c:

Code:
#include <unistd.h>
#include <fcntl.h>

#define OUT STDOUT_FILENO
#define ERR STDERR_FILENO

int main(int argc, char **argv)
{
	int count = 0;
	int input = atoi(argv[0]);
	char buf;
	int temp;

	while(count < 26)
	{
		if((temp = read(input, &buf, 1)) < 0)
		{
			write(ERR, "write\n", 6);
			exit(0);
		}

		if(temp)
		{
			write(OUT, &buf, 1);
			count++;
		}
	}

	exit(0);
}
The child process just goes into an imfinite loop, which indicates that read is always return 0. When I used pipes, everything worked fine. Please help me out. Thanks.
Yasir_Malik is offline   Reply With Quote
Old 09-15-2003, 09:07 AM   #2
Registered User
 
Join Date: Aug 2003
Posts: 51
i haven't tried ur code, but i have a feeling it's because you've used the wrong arguement in the child.

argv[0] is the executable file of the program.

argv[1] is the first arguement given to the program. So try to change

Code:
int input = atoi(argv[0]);
to

Code:
int input = atoi(argv[1]);
Kyro is offline   Reply With Quote
Old 09-15-2003, 11:19 AM   #3
Registered User
 
Join Date: Sep 2003
Posts: 224
Thanks

I don't think that's it. When I print out argv[0], I get the value of the file descriptor, 3, not value "child." The file descriptor value is the same in the parent as well. I think when you call one of the exec functions, the argument numbering is different.
Yasir_Malik is offline   Reply With Quote
Old 09-15-2003, 02:09 PM   #4
Registered User
 
Join Date: Sep 2003
Posts: 224
Bump.
Yasir_Malik is offline   Reply With Quote
Old 09-15-2003, 03:50 PM   #5
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
No bumping allowed here, don't do it again.

Didn't your compiler warn you about this:

Code:
char *file[3];
execlp("./child", file, (char *) NULL);
It should be more like this
Code:
char file[20];  /* Bigger than 3! */
execlp("./child", "child", file, (char *) NULL);
Then you use argv[1].

Use perror() to print error messages which will help you debug this.

How can you guarantee that the parent has finished writing before the child starts reading?

When you call read() in the child, where do you think the internal file pointer is pointing to? (ie, what makes you think it's pointing to the beginning of the file?)
__________________
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Hammer is offline   Reply With Quote
Old 09-16-2003, 08:57 AM   #6
Registered User
 
Join Date: Sep 2003
Posts: 224
Thanks

Sorry for bumping; I didn't read the FAQ carefully enough.
The purpose of the assignment was for the child to read the file while the parent is writing to the file. I solved the problem in a desparation attempt by just opening the file, temp, again, as read only; it's not nice because I was supposed to just pass the file descriptor.
When I used pipes, I passed the read of the file descriptor to the child, and everything worked fine then: the parent wrote the write end, and the child read from the read end. Why did it work then?
And my compiler did not complain when I used execlp; it did complain when I used execl.
Yasir_Malik is offline   Reply With Quote
Old 09-16-2003, 09:26 AM   #7
Registered User
 
Join Date: Aug 2003
Posts: 51
i have a feeling that it worked with pipes because when you create a pipe, you get 2 separate file descriptors to the same 'file', one for writing and one for reading. I think since it's two separate fd's writing does not modify the position of the reading fd.
Kyro is offline   Reply With Quote
Old 09-16-2003, 09:48 AM   #8
Registered User
 
Join Date: Sep 2003
Posts: 224
If I duplicate a file descriptor using dup(), does modifying the file pointer in one modify the file pointer in another? And does writing in a duplicated file descriptor effect the other file descriptor (i.e. the same file is changed by just using any of file descriptors)?
Yasir_Malik is offline   Reply With Quote
Old 09-16-2003, 02:08 PM   #9
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
With regards to dup(), read the man pages, they answer your questions.

If you want to use the same file descriptor, I'd suggest you get the child to wait for the parent to finish writing, then get it to lseek() to the beginning and then read it.
__________________
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Hammer is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error opening files in a different dir Ozzie C++ Programming 3 10-09-2008 06:55 AM
Working with muliple source files Swarvy C++ Programming 1 10-02-2008 08:36 AM
Trouble with linking files magda3227 C Programming 11 06-18-2008 01:00 AM
Program Deployment and DLL/OCX Files? dfghjk C++ Programming 5 06-16-2008 02:47 AM
Sharing Classes using .DLL files? Laos C++ Programming 2 08-30-2001 04:58 AM


All times are GMT -6. The time now is 11:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22