0

i'm facing a problem with reading from two input files with two children. The two children should read alternately a word from each file, sending them to the parent that will output them like this:

input1: "I really"

input2: "am stupid"

output: "I am really stupid"

How can i do that using the functions open (for opening the input file for reading) and read? The "son" function for the children (There will be two similar function for the children, one that reads the even words and one that reads the odd words) should be something like this:

void son(char i_file, int p[2], int bro_pid){
    int c,fd=open("i_file", O_RDONLY, S_IWUSR);
    char buf;
    if(fd > 0){
        close(p[0]);
        while(c=read(i_file, buf, 1) == 1){   //Here should be a conditions to continue reading till EOF
            ....                 //I need your help here
            ....                
            write(p[1], buf, c); //Here i write in the pipe the word for the parent
            while(1){
                pause();         //The child will wait a signal from the parent before reading the next word
                break;          
            }
        }
    }else
        perror("Error opening file secondo figlio");
    close(fd);
    close(p[1]);
}

Can you help me with this problem? Thanks in advance for the help.

EDIT: I misread the exercise, there're two different input files.

Aster
  • 143
  • 2
  • 11
  • first child: read current word and ignore next; second child: ignore current word and read next – pmg Feb 27 '15 at 12:34
  • Assuming you're developing in a multi-threaded or multi-tasking environment, where control can be handed between threads or tasks, then you should look into using mutexes or semaphores. Which will depend on how many tasks need to share the resource. I.e. open the file once with a mutex or semaphore, giving each task equal access to the file handle, then use the mutex or semaphore to determine which task is actually using the shared resource at any time. – Evil Dog Pie Feb 27 '15 at 12:56
  • @MikeofSST I'm managing the concurrent handling with signals using the "pause()" command in the child, waiting for the parent that will send "kill(pid_child, SIGUSR1)" in order to let the child continue. The problem that i'm facing is the reading part, i can't figure out a way to read a word at a time alternately from the two children. – Aster Feb 27 '15 at 13:42
  • **If** you are using the standard libraries, I'd recommend looking at `fscanf()` with the "%s" format parameter, which will read the stream up to the first whitespace character. Also, I'm not convinced that you really want the `while(1){pause();break;}` loop. What purpose does it serve rather than simply using `pause();`? There's a reasonable chance it will be optimised out by the compiler anyway. – Evil Dog Pie Feb 27 '15 at 17:24
  • @MikeofSST You're right, the while loop should be pointless, i'll try using only the 'pause()' command. The 'fscanf()' would do the job, thanks for the help! – Aster Feb 28 '15 at 09:52

0 Answers0