Thread: Trouble sharing files between processes.

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

  2. #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]);

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

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Bump.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

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

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

  8. #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)?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

Popular pages Recent additions subscribe to a feed

Similar Threads

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