Trouble sharing files between processes. [Archive] - C Board

PDA

View Full Version : Trouble sharing files between processes.


Yasir_Malik
09-15-2003, 07:57 AM
I'm having trouble sharing files between processes. Here's what I have:

parent.c


#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:


#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.

Kyro
09-15-2003, 09:07 AM
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


int input = atoi(argv[0]);


to


int input = atoi(argv[1]);

Yasir_Malik
09-15-2003, 11:19 AM
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
09-15-2003, 02:09 PM
Bump.

Hammer
09-15-2003, 03:50 PM
No bumping allowed here, don't do it again.

Didn't your compiler warn you about this:


char *file[3];
execlp("./child", file, (char *) NULL);

It should be more like this

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?)

Yasir_Malik
09-16-2003, 08:57 AM
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.

Kyro
09-16-2003, 09:26 AM
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.

Yasir_Malik
09-16-2003, 09:48 AM
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)?

Hammer
09-16-2003, 02:08 PM
With regards to dup(), read the man pages (http://www.rt.com/man/dup.2.html), 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.