pipe()

This is a discussion on pipe() within the C Programming forums, part of the General Programming Boards category; Hello! I am a little bit newer in C and I would like to know if anybody can help me ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    4

    pipe()

    Hello!

    I am a little bit newer in C and I would like to know if anybody can help me about pipes. I wold like in my aplication communicate beween two childs . So I create with fork() 4 childs and then I would like to comunicate with them with pipe(). Not parent and chil, but child to child.

    Thnx

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    This is your one-time free homework - next time, try and post some effort of what you tried.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    #define PIPE_RD 0
    #define PIPE_WR 1
    
    int main ( ) {
      int p[2];
      pid_t c1, c2;
      pipe(p);
    
      c1 = fork();
      if ( c1 == 0 ) {
        /* child1 up and running */
        write(p[PIPE_WR],"hello",6);
      } else if ( c1 != -1 ) {
        c2 = fork();
        if ( c2 == 0 ) {
          /* child2 up and running */
          char buff[100];
          ssize_t n = read(p[PIPE_RD],buff,100);
          printf( "c2 got %d bytes\n", n );
          if ( n >= 0 ) {
            printf( "c2 got %s\n", buff );
          }
        } else if ( c2 != -1 ) {
          /* wait() for both children */
          int status;
          waitpid(c1,&status,0);
          waitpid(c2,&status,0);
        }
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    Thnx Salem... sorry because I didn't paste my code... here it is now... I fixed the code and now I had just a little bit problem more and that is that my parent dont wait allways for all sons... sometimes finished early then other child proces...
    I apologies because the comments are in our language...

    Code:
    int main(int argc, char** argv)
    {
    	if(argc<2)
    	{
    		printf("Premalo st argumentov\n");
    		return -1;
    	}
    	
    	int Pipe[2];
    	int Pipe1[2];
    	int Pipe2[2];
    	int pid;	
    	int i=0;
    
    	if(pipe(Pipe) == -1) 			//odpremo prvo pipo ter dobimo dva oprimka
    	{
    		perror("pipe call error");		//ce ne uspe izpisemo napako vrnemo OS-u 1
    		_exit(1);
    	}
    	if(pipe(Pipe1) == -1) 			//odpremo drugo pipo ter dobimo dva oprimka
    	{
    		perror("pipe call error");		//ce ne uspe izpisemo napako vrnemo OS-u 1
    		_exit(1);
    	}
    		if(pipe(Pipe2) == -1) 		//odpremo tretjo pipo ter dobimo dva oprimka
    	{
    		perror("pipe call error");		//ce ne uspe izpisemo napako vrnemo OS-u 1
    		_exit(1);
    	}	
    
    	int k=1;
    	for(; k<=argc; k++)			//koliko je argumentov toliko krat posljemo vrednost s plavajoco vejico
    	{			
    		for(i=0; i<4; i++)
    		{
    			if((pid=fork())==0)		//ustavrjanje sinov
    				break;
    		}
    	
    		if(pid==0)				//ce je sin
    		{
    			switch(i)				//preklapljamo med i 
    			{
    				//proizvajalec
    				case 0 :
    				{
    					close(Pipe[0]);							//zapremo del za branje iz prve pipe
    
    					float *num=(float*)malloc(sizeof(float));				//dinamicno dodeljevanje pomnilnika		
    					*num=atof(argv[k]);								//priredimo vrednost k-tega argumenta spremenljivki num
    
    					write(Pipe[1],(char*)num,sizeof(float));		//posljemo distributerju
    					
    					close(Pipe[1]);							//zapremo prvo pipo za pisanje ker je ne potrebujemo vec
    					free(num);							//sprostimo pomnilnik
    					exit(0);								
    					break;
    				}
    				//distributer
    				case 1 :
    				{
    					
    					close(Pipe[1]);
    	
    					float *num1=(float*) malloc(sizeof(float));		//dinamicno dodeljevanje pomnilnika
    					
    					read(Pipe[0],num1,sizeof(float));				//preberemo iz prve pipe
    					
    					*num1 = *num1 + ((*num1 / 100) * 30);		//povecamo prebrano stevilo za 30%
    	
    					write(Pipe1[1],(char*)num1,sizeof(float));		//posljemo trgovcu
    					free(num1);								//sprostimo dinamicno dodeljen pomnilnik
    					close(Pipe[0]);								//zapremo prvo pipo za branje
    	
    					exit(0);
    					break;
    				}
    				//trgovec
    				case 2 :
    				{
    					
    					close(Pipe1[1]);							//zapremo drugo pipo za pisanje ker je tu ne potrebujemo
    
    					float *num2 = (float*) malloc(sizeof(float));		//dinamicnco alociramo prostor
    					
    					read(Pipe1[0],num2,sizeof(float));				//prebremo iz druge pipe
    	
    					*num2 = *num2 + ((*num2 / 100) * 35);		//povecamo prebrano stevilo za 35%
    	
    					write(Pipe2[1],(char*)num2,sizeof(float));		//posljemo potrosniku
    					free(num2);								//sprostimo dinamicno dodeljen pomnilnik
    					close(Pipe1[0]);							//zapremo drugo pipo za branje
    					exit(0);
    				}
    				//potrosnik
    				case 3 :
    				{	
    					close(Pipe2[1]);							//zapremo tretjo pipo za pisanje
    
    					float *num3 = (float*) malloc(sizeof(float));		//dinamicno rezerviramo prostor
    
    					read(Pipe2[0],num3,sizeof(float));				//preberemo iz tretje pipe
    	
    					printf("%f\n",*num3);						//in prebrano vrednost izpisemo na zaslon
    					free(num3);								//sprostimo dinamicno alociran pomnilnik
    					close(Pipe2[0]);							//zapremo tretjo pipo za branje
    					exit(0);
    				}
    			}
    		}
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  2. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  3. Named pipe problem
    By rahul_c in forum C Programming
    Replies: 3
    Last Post: 10-02-2007, 05:40 PM
  4. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM
  5. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21