Problem with Pipes => Only works first pipe


 
Thread Tools Search this Thread
Top Forums Programming Problem with Pipes => Only works first pipe
# 1  
Old 06-30-2011
Problem with Pipes => Only works first pipe

Hi!

I'm having problems with pipes... I need comunnications with childs processes and parents, but only one child can comunnicate with parent (first child), others childs can't.

A brief of code:

Code:
if(pipe(client1r)<0){
                perror("pipe");
        }
        if(pipe(client2r)<0){
                perror("pipe");
        }
        if(pipe(client3r)){
                perror("pipe");
        }
        listen(sock,5);
        while(1){
                FD_ZERO(&fds);
                FD_SET(client1r[0],&fds);
                FD_SET(client2r[0],&fds);
                FD_SET(client3r[0],&fds);
                FD_SET(sock,&fds);
                a=select(256,&fds,NULL,NULL,NULL);
                if(a>0){
                        c=0;
                        if(FD_ISSET(client1r[0],&fds)){
                                write(1,"test",12);
                        }
                        if(FD_ISSET(client3r[0],&fds)){
                                 write(1,"test",12);
                        }
                        if(FD_ISSET(client2r[0],&fds)){
                                write(1,"You Win",7);
                        }
                        if(FD_ISSET(sock,&fds)){
                                fd_client = accept(sock,0,0);
                                if(fd_client == -1){
                                        perror("accept");
                                }else{
                                        (*numlogin)=(*numlogin)+1;
                                        fill=fork();
                                        switch(fill){
                                            case 0:
                                                        if((*numlogin)==2){
                                                                close(client2r[0]);  
                                                                Client(fd_client,logins,shmid1,shmid2,shmid3,client2r[1]);
                                                        }
                                                        if((*numlogin)==1){
                                                                close(client1r[0]);
                                                               Client(fd_client,logins,shmid1,shmid2,shmid3,client1r[1]);
                                                        }
                                                        if((*numlogin)==3){
                                                                close(client3r[0]); 
                                                               Client(fd_client,logins,shmid1,shmid2,shmid3,client3r[1]);
                                                        }
                                                        break;
                                                case -1:
                                                        write(1,"Error al fork\n",14);
                                                        break;
                                                default:
                                                        close(client1r[1]);
                                                        close(client2r[1]);
                                                        close(client3r[1]);
                                                        close(fd_client); //No ho necessita
                                                        break; //PARE

Results are:

First pipe (client1r) works correctly, but others pipe (client2r and client3r) doesn't works. I don't understant why doesn't works, because others pipes are exactly the same, and then I call another function (function client) passin the value.

Any idea?

Thanks
# 2  
Old 07-01-2011
The usual thing is to pipe, fork, the child closes the parent end and writes or reads his end, the parent closes the child end and writes or reads his end. If you want it be be stdin or stdout, you close std* and dup pipe and close pipe. If you are gutless, you use dup2. You do know pipe() makes unidirectional pipes? Once you have the fds all set up, then you select() or poll(). Pipes will say they are writable until they fill, so you may not want to include them except when you have something to write. They will say they are readable until they are empty. Why not add some logging for debug?

Last edited by DGPickett; 07-01-2011 at 12:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

please help a problem in client-server ipc message 2 pipes communication simple example

I want to have a message send & receive through 2 half-duplex pipes Flow of data top half pipe stdin--->parent(client) fd1--->pipe1-->child(server) fd1 bottom half pipe child(server) fd2---->pipe2--->parent(client) fd2--->stdout I need to have boundary structed message... (1 Reply)
Discussion started by: ouou
1 Replies

2. Programming

Problem with pipes

problem solved. (1 Reply)
Discussion started by: superfons
1 Replies

3. Shell Programming and Scripting

The problem of pipe

Hi,guys: I want to use c to implement a pipe. For example: ps auxwww | grep fred | more I forked three child processes. Each is responsible for each command, and pipe to next one. for(i=0;i<2;i++) pipe(fd) if(child==1) // child 1 { close(1) dup2(fd,1) close(fd) }... (3 Replies)
Discussion started by: tomlee
3 Replies

4. UNIX for Advanced & Expert Users

sendmails works, but opens 43 file handles per email -> problem

I'm using Sendmail 8.13.8 on a CentOS 5.5 vServer (Virtuozzo). I'm using a loop in PHP to send a lot of HTML-mails via sendmail. Each mail is a mail with individual statistics for our users, so its not mass mailing, bcc is not an option. It all works fine, but when I take a closer look there... (2 Replies)
Discussion started by: ZX81
2 Replies

5. Shell Programming and Scripting

Problem with pipes on infinite streams

Here is an example code that shows the issue I have: #!/bin/bash counter() { seq 1000 | while read NUM; do echo $NUM echo "debug: $NUM" >&2 sleep 0.1 # slow it down so we know when this loop really ends done } counter | grep --line-buffered "" | head -n1 ... (10 Replies)
Discussion started by: tokland
10 Replies

6. Homework & Coursework Questions

How join works and the specific parameters to my problem?

1. The problem statement, all variables and given/known data: I have two files created from extracting data off of two CSV files, one containing class enrollment on a specific quarter and the other containing grades for that specific quarter. The Enrollment file generated contains course name,... (11 Replies)
Discussion started by: Lechnology
11 Replies

7. Programming

Pipe problem

Could anyone tell me whats wrong whit this piping? the commands that they execute are correct. the command I am trying is ls|wc. Both processes go to the right if statement. for(i=0;i<argc;i++){ if(i==0&&argc>1){//first command if(pipe(pipa1)==-1) ... (2 Replies)
Discussion started by: isato
2 Replies

8. UNIX for Dummies Questions & Answers

problem with pipes

I have written the following program. The function of this prog is to read data from a file(source.c) and write into another file(dest.c) using pipes. I have just written a line in the source file.Im able to compile and run the program without errors. But the data is not written onto the other... (2 Replies)
Discussion started by: afser
2 Replies

9. UNIX for Dummies Questions & Answers

Plink problem....only works if passwd is in the script

I have a one line bat script run off a XP machine that tar's and compresses some files from a Sol 8 box. It goes something like this (a bit simplified).... plink -pw <passwd> user@host "tar -cvf - -C / tmp/a_file | compress " > a_file.tar.Z So this works....and it's worked many times. But now... (3 Replies)
Discussion started by: Yinzer955i
3 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question