![]() |
| | #1 |
| Registered User Join Date: Sep 2003
Posts: 224
| Trouble sharing files between processes. 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);
}
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);
}
|
| Yasir_Malik is offline | |
| | #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]); Code: int input = atoi(argv[1]); |
| Kyro is offline | |
| | #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 | |
| | #4 |
| Registered User Join Date: Sep 2003
Posts: 224
| Bump. |
| Yasir_Malik is offline | |
| | #5 |
| End Of Line 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);
Code: char file[20]; /* Bigger than 3! */
execlp("./child", "child", file, (char *) NULL);
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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #9 |
| End Of Line 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |