share file descriptor between childs


 
Thread Tools Search this Thread
Top Forums Programming share file descriptor between childs
# 1  
Old 10-10-2008
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  
Old 10-10-2008
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  
Old 10-11-2008
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  
Old 10-11-2008
I'd use flock(). Obtain lock, read n bytes, release lock, next child, etc...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with File Descriptor in a While loop

Hi, I am trying to read a file line-by-line in a while loop, and perform some tasks which involves non-interactive SSH to a remote server. The code looks something like this -- #!/usr/bin/ksh export myFile=/path/to/my/file.load while read line do do something ## Adding the SSH... (2 Replies)
Discussion started by: Subu1987
2 Replies

2. Shell Programming and Scripting

file descriptor count

I am trying to write a script which will only show me the file descriptor count for a process/pid. My script will return me the count only not the whole output. For example, I would like my script to return the output 23 this case, not the whole output. Can anybody please help me how do I get... (11 Replies)
Discussion started by: mohullah
11 Replies

3. Programming

How Can I share a socket between childs?

Hello guys! I had seen some posts at this forum talking about my problem, but maybe my scenario is a little different, and I want other solutions. I saw users of this forums saying that the way to shared sockets is using UNIX Sockets, but this is the only way in my scenario? My Scenario:... (4 Replies)
Discussion started by: serpens11
4 Replies

4. Shell Programming and Scripting

file descriptor KSH

Hello, How can i use file descriptor in a script to read 2 files at the same time and extract line 200 from file 1 and line 500 from file 2. Thanks. (6 Replies)
Discussion started by: LiorAmitai
6 Replies

5. UNIX for Dummies Questions & Answers

File Descriptor

Hi What the below path contains? /proc/<pid>/fd (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

6. Shell Programming and Scripting

File Descriptor

Hello All, Im opening a file desciptor in perl and sending data using print CMD "$xyz". is there a limit to the length of the string that I can give to this CMD at a time. (3 Replies)
Discussion started by: rimser9
3 Replies

7. UNIX for Advanced & Expert Users

File Descriptor Table

Im working on writing a small operating system. I am currently working on implementing dup, dup2, pipe, and close and I need to implement some type of file descriptor table in my PCB. I was wondering if there is anyone who is familiar with linux/unix implementation of these tables who could... (6 Replies)
Discussion started by: Ashaman0
6 Replies

8. UNIX for Dummies Questions & Answers

File Descriptor Help

What is a file descriptor in Unix?? How to find a file descriptor of a file in Unix?? Does it have anything to do with the Inode numbers?? (3 Replies)
Discussion started by: rahulrathod
3 Replies

9. UNIX for Dummies Questions & Answers

file activity (open/closed) file descriptor info using KORN shell scripting

I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies

10. UNIX for Dummies Questions & Answers

bad file descriptor?

Ok, I'm sure this is a total newbie question, but I think I'm in the right place, no? I'm trying to call a perl module from a cgi script - Mail::Sendmail - and my web host installed the module in a directory that doesn't seem to be accessible, at least not the way I'm trying. But I thought you... (1 Reply)
Discussion started by: ftb
1 Replies
Login or Register to Ask a Question