Thread: User Input with Forks

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

    Unhappy User Input with Forks

    Hey just thought some of the user might be able to shed some light on my situation. So im working with forks and creating 2 fork processes. One child and parent. What im trying to get the child to ask me for a value. Then the parent would start to figure out the value and keep trying. Once it figures it out its tells them you got it and simply stops the program. I cant seem to get the program to keep guessing and stop when the number is found which sends a sigint. Here's what i have so far.

    Code:
    #include <stdio.h>  
    #include <unistd.h> 
    #include <signal.h> 
    #include <stdbool.h>  
    
     int main() {     
     int j, i,pid;     
    bool p= true;    
     int input;     
     printf("Fork\n");        
       
      // clone process    
     j = fork();     
    
     //child     
    if (pid == 0) {   
         while(true){      
     if(!p){        
     printf("Enter a value");       
      scanf("%d", &input);     }}}   
    
       //parent          
    else {            
    while (input == 1) {          
    printf("%s\n", "you got it"); 
    }}}

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    Fork creates two copies of the same process, with copies of all the variables. You can't write to a variable in one fork and read it in the other.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    maybe use threads instead. they can share variables

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User Input
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2003, 01:03 AM
  2. user input
    By unregisterd in forum C Programming
    Replies: 5
    Last Post: 12-30-2002, 05:22 AM
  3. user input
    By hen in forum C Programming
    Replies: 1
    Last Post: 08-02-2002, 11:37 AM
  4. user input
    By hen in forum C Programming
    Replies: 4
    Last Post: 06-29-2002, 04:10 PM
  5. user input
    By hen in forum C Programming
    Replies: 16
    Last Post: 06-27-2002, 11:09 PM

Tags for this Thread