The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File Descriptor rimser9 Shell Programming and Scripting 3 08-14-2008 07:34 AM
Problems with file descriptor teo High Level Programming 11 05-09-2005 12:47 PM
File Descriptor Help rahulrathod UNIX for Dummies Questions & Answers 3 10-14-2004 06:08 AM
file activity (open/closed) file descriptor info using KORN shell scripting Gary Dunn UNIX for Dummies Questions & Answers 3 06-07-2004 02:54 PM
bad file descriptor? ftb UNIX for Dummies Questions & Answers 1 02-20-2002 07:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-10-2008
dlcpereira dlcpereira is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
share file descriptor between childs

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#define BUFF_SIZE 256


#define CHILDS 4
#define DATAFILE "Client_Files.txt"

void worker(int n);
char str_buf[BUFF_SIZE];
FILE *datafile_fp;
int i;
    
char str_buf[BUFF_SIZE];
pid_t id[CHILDS];

int main() {

    if((datafile_fp = fopen( DATAFILE, "r"))== NULL)
    printf("grimi");



    
    for ( i = 0; i < CHILDS; i++ ){
        id[i] = fork();
        if( id[i] == 0 ){
            worker(i);
            exit(0);
        }
        else if ( id[i] == -1 ){
            printf("fork error.\n");
            exit(0);
        }
        else {    
            //stuff;
        }

    }
    
}

void worker(int n) {
    fgets(str_buf, BUFF_SIZE, datafile_fp);
    printf("%c",str_buf[0]);
    
}
how can i share the file descriptor? each child have to read one line.. and they cant read the same one.. this works for the first line.. but then printf doest work...

thanks in advance..
  #2 (permalink)  
Old 10-10-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
The code you posted has a problem. But even if you solve that problem, there is another one. fork() dup()licates fd's from the parent but if you don't have some kind of synchronous read between the childs, you'll have unexpected results.

Do you really want to use multiple processes to read the same fd?

Try running this several times to see what I'm talking about:
Code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define CHILDS_N 5

void worker(int * fd)
{
        char buf[20];

        memset (buf, 0x0, sizeof (buf));
        read(*fd, buf, sizeof (buf)-1);
        printf ("%s\n", buf);

}


int
main ()
{
        pid_t childs[CHILDS_N];
        int fd;
        int i;
        fd = open ("Client_Files.txt", O_RDONLY);
        if (fd == -1)
                exit(1);


        for (i=0; i < CHILDS_N; i++)
        {
                childs[i] = fork();
                if (childs[i] == 0)
                {
                        worker(&fd);
                }
                else
                {
//                      printf ("parent: %d\n", getpid());
                }
        }
}
  #3 (permalink)  
Old 10-11-2008
dlcpereira dlcpereira is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 2
Hmm, i got that. But there is some way so i can read only a line in each child?

and can i use pthred_mutex for synchronization..?

Last edited by dlcpereira; 10-11-2008 at 11:02 AM..
  #4 (permalink)  
Old 10-11-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
I'd use flock(). Obtain lock, read n bytes, release lock, next child, etc...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:45 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0