Like Tree1Likes
  • 1 Post By Malcolm McLean

User Input with Forks

This is a discussion on User Input with Forks within the C Programming forums, part of the General Programming Boards category; Hey just thought some of the user might be able to shed some light on my situation. So im working ...

  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
    333
    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.
    Salem likes this.
    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.
    http://www.malcolmmclean.site11.com/www

  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, 12:37 PM
  4. user input
    By hen in forum C Programming
    Replies: 4
    Last Post: 06-29-2002, 05:10 PM
  5. user input
    By hen in forum C Programming
    Replies: 16
    Last Post: 06-28-2002, 12:09 AM

Tags for this Thread


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