Find the answer to your Linux question:
Results 1 to 3 of 3
Hi everyone, I'm ertan and Im in new cpp programming in linux. I have one problem in my task. Now,I created one parent and two children. I must create some ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3

    [SOLVED] I can't run second child in C (Fork Problem)


    Hi everyone, I'm ertan and Im in new cpp programming in linux. I have one problem in my task.
    Now,I created one parent and two children. I must create some value as random in child1 and child1 has to send these values to child2.Child2 must read them..
    now,I cant create some random values and i can send them.But child2 doesnt work. I have two functions. One them is writing, another one is reading. Child2 uses reading function,but it doesnt work (child2 cant call it, because writing function in endless loop)
    What i must do? I used wait,usleep... No way..
    I attached my file and also there is code..
    I'm waiting for your help..
    Thank you..

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <signal.h>
    #include <stdarg.h>
    #include <sys/types.h>
    #include <string.h>
    #include <iostream>
    #include <sstream>
    #include <string>
    using std::ostringstream;

    using namespace std;


    void producerint handle )
    {
      
    int count 0total 0;
      
    char buf128 ];
    int myVal=0;
      while ( 
    )
      {

        
    myVal=rand()%100;

          
    sprintfbuf"%d\n",myVal );

          
    int ret writehandlebufstrlenbuf ) );
    printf("handle %d  buf:  %s \n",handle,buf);
    //getchar();
              
    if ( ret )
              {
                   
    printf"Writing Error in Pipe!" );
              exit( 
    );
              }



          
    printf"Sending Number: %s\n"buf );
      }
    }


    void consumerint handle )
    {
    //  int total = 0;
      
    char buf128 ];
      
    int tmp=0;
      while ( 
    )
      {
          
    int ret readhandlebufsizeofbuf ) );
          if ( 
    ret )
          {
                    
    printf"Reading Error in pipe!" );
              exit( 
    );
          }
          
    bufret ] = 0;


      }
       
    string str=(char*)buf;

       
    size_t found=0;


      
    int tmp1=0;
      
    int total=0;
      
      
    string str2;


        do{
          
         
    found=str.find_first_of("\n",found+1);

              
    str2 str.substr(tmp1,found-tmp1);
        
    total += atoi(str2.c_str());
          
    cout<<"Recieved Number: " <<str2<< "\n"<<"Sum of Numbers: "<<total<<"\n";
          
    tmp1=found+1;
              

        }  while (
    found!=string::npos);

    //semaphor

      
    }
    main()

        
     
    int mypipe];

    if ( 
    pipemypipe ) < )
    {
       
    printf("Creating pipe Error!" );
         exit( 
    );
      }
      
      
    int myChild;
      
    int myChild2;
      
      
    myChild fork();
      

      if (
    myChild <0
      {
         
    printf("can't fork"); 
         exit(
    1);
      } 
      
      else if (
    myChild == 0)
       {  
    // Child 1
       
    printf("child1\n");
         
    closemypipe] );
          
    producermypipe1] );
          
        }
        
        else  
        
        {
        
        
    printf("Parent: my pid = %d, parent pid = %d \n "getpid(), getppid());

        
    myChildfork();
            if (
    myChild == 0)
           { 
    // Child2
           
    printf("child2\n");
         
    closemypipe] );
          
    consumermypipe0] );

                }           
        }
      return 
    0;




    Attached Files Attached Files

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    In your consumer() function, you aren't breaking out of the read loop, so you never get to the data processing part.
    Code:
    void consumer( int handle ) 
    { 
    //  int total = 0; 
      char buf[ 128 ]; 
      int tmp=0; 
      while ( 1 ) 
      { 
          int ret = read( handle, buf, sizeof( buf ) ); 
          if ( ret < 0 ) 
          { 
                    printf( "Reading Error in pipe!" ); 
              exit( 1 ); 
          } 
          buf[ ret ] = 0; 
      }
    // You never get here
    .
    .
    .
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3
    Hi,Thank you for your reply. I already have solve this problem. I made mistake and i realized it later. I have just edited "while loop" and it works now..
    Thank you for your attention..
    Have a nice days..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...