Thread: C Simple child and parent back and forth

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    C Simple child and parent back and forth

    Im creating a small guess game between a child and a parent: The child takes in the input from user and makes that the number to guess, the parent simply goes into a loop trying to figure out which is the right number.
    I can't get it to work fully. Also you can see I'm keeping track of how many times it takes, once its found, make a new one and then exits after 4 rounds.

    Code:
    #include <stdio.h> 
    #include <unistd.h> 
    #include <time.h> 
    #include <stdlib.h>  
    char readbuffer[100]; 
    
     int main() {  
        int pid, nbytes, i, input;   
        int fd[2] = {} ;   
        int guess = 0;   
        pid = fork();  
        int x;   
        
         srand((unsigned) time(NULL));  
         x = (rand() % 10 + 1);  
    
           if (pid == 0) 
        {     
           close(fd[0]);    
           printf("enter a number ");     
           scanf("%d", &input); 
          
           write(fd[1], &input, (sizeof(input)));
          
            if (x == input) 
            {    
                exit(0);     
            }  
    
          close(fd[1]);  
       }
     
      else 
      {   
         //PARENT     
        close(fd[1]);    
        nbytes = read(fd[0], &input, sizeof(input));  
       
        while (x != input)
        {    
          if (x != input) 
          {        
             guess++;       
          }     
        }   
          char answer; 
       
        if (x == input) 
       {    
          printf(" correct");       
          printf("Number of Guesses: %d\n", guess);
           exit(0);    
        }    
    
     //to reset random   
     
        x = (rand() % 10 + 1);  
       close(fd[0]);   } }
    Last edited by LivingLegend; 04-10-2013 at 12:49 PM. Reason: indent

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    SourceForge.net: Indentation - cpwiki
    Nobody is going to try and pick apart code which looks like it was indented by a dog chasing a stick.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    srry my code was well indented but for some reason the code tag messed it one sec let me fix it didnt realize it

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int fd[2] = {} ;   
    ...
    close(fd[0]);    
    ...
    write(fd[1], &input, (sizeof(input)));
    Well, it looks like you want to use a pipe but you never create one. Just declaring an array fd isn't enough.

    Code:
      
    nbytes = read(fd[0], &input, sizeof(input));
    You capture the return value of read() which is good, but you never use it. I can tell you that its value is -1 which means there was an error. The reason for the error is explained above.

    Code:
        while (x != input)
        {    
          if (x != input) 
          {        
             guess++;       
          }     
        }
    Typical infinite loop because you neither change "x" nor "input" within the loop.

    Code:
     //to reset random   
     
        x = (rand() % 10 + 1);
    ???

    Generally, crossposting is considered bad form.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IPC communication between parent and child
    By Durango2011 in forum Linux Programming
    Replies: 1
    Last Post: 10-23-2012, 04:36 AM
  2. parent child process
    By simo_mon in forum C Programming
    Replies: 2
    Last Post: 08-03-2008, 03:44 AM
  3. calling parent from child
    By Aemon07 in forum Windows Programming
    Replies: 5
    Last Post: 07-25-2007, 09:27 PM
  4. C# parent child
    By BraneMxm in forum C# Programming
    Replies: 2
    Last Post: 06-02-2007, 10:24 AM
  5. Creating Child with parent of Child
    By SilkySmooth in forum Windows Programming
    Replies: 3
    Last Post: 06-28-2003, 12:15 PM

Tags for this Thread