fork(maybe)

This is a discussion on fork(maybe) within the C Programming forums, part of the General Programming Boards category; I want to have the user be able to input something for 5 seconds; after that, I want it to ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    fork(maybe)

    I want to have the user be able to input something for 5 seconds; after that, I want it to see if there was and answer and if not count it wrong(obviously if he/she did check it), but I dont' know how to get the program to ask for and input for only 5 seconds. I was first about to use sleep, but soon realized where it failed. Do I need to fork my program. If so how would I go about doing that I have never done it. And if you are wondering, I of course, run gentoo Linux.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    try using select()

    Check the man page of select() for a perfect example of this.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
           #include <stdio.h>
           #include <sys/time.h>
           #include <sys/types.h>
           #include <unistd.h>
    
           int
           main(void)
           {
               fd_set rfds;
               struct timeval tv;
               int retval;
    
               /* Watch stdin (fd 0) to see when it has input. */
               FD_ZERO(&rfds);
               FD_SET(0, &rfds);
               /* Wait up to five seconds. */
               tv.tv_sec = 5;
               tv.tv_usec = 0;
    
               retval = select(1, &rfds, NULL, NULL, &tv);
               /* Don't rely on the value of tv now! */
    
               if (retval)
                   printf("Data is available now.\n");
                   /* FD_ISSET(0, &rfds) will be true. */
               else
                   printf("No data within five seconds.\n");
    
               exit(0);
           }
    I don't see how to actually get the input though. I understand it waits and sees if it is changed. I must be missing something.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well once you know there IS input then you can use any number of different methods to get it.

    Lets look at:
    Code:
    if (retval)
                   printf("Data is available now.\n");
                   /* FD_ISSET(0, &rfds) will be true. */
    You can change that to:
    Code:
    if (retval)
    {
      fgets(buffer, sizeof buffer, stdin);
    }

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Ah I see. Does this buffer the input from stdin so you don't miss what happened in the 5 seconds or what. This is pretty nifty. Oh and thanx Thantos

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yeah, since input is buffered by default select won't return until the user hits enter (or time elapses) and any input that the user put in will remain in the buffer till emptied. (Though if you wanted to do unbuffered input take a looky at this thread)

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Okay I got that part working, but I get a minor problem. I just decided to keep it on this same thread though. Say I was in the middle of typing the answer when the time ran out. When it goes strait to the next question what I typed is still there
    Code:
     while(x<input){
                    bn=rand();
                    bn%=99;
                    bn+=1;
                    if(bn<=9)
                            bn+=10; /*bignum will always be 2 digits now*/
                    ln=rand();
                    ln%=10;
                    if(ln<2)
                            ln+=2;
                    printf("\t%5i\n\t*%4i\n\t-----\n\n",bn,ln);
                    FD_ZERO(&rfds);
                    FD_SET(0, &rfds);
                    tv.tv_sec = 6;
                    tv.tv_usec = 0;
                    retval=select(1, &rfds, NULL, NULL, &tv);
                    canswer=ln*bn;
                    if(retval){
                            fgets(buffer,sizeof buffer,stdin);
                            sscanf(buffer,"%d",&answer);
                            if((compare(answer,canswer))){
                                    score++;
                                    printf("Correct\n");
                                    }
                            else{
                                    printf("Wrong\n");
                                    cscore++;
                            }
                    }
                    else{
                            printf("Wrong\n");
                            cscore++;
                    }
                    x++;
            }
    this is the loop I have. Any suggestions. Oh and compare is defined like so
    Code:
    int compare(int num1,int num2){
            if(num1==num2)
                    return 1;
            else
                    return 0;
    }
    Any ideas to fix this?

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by linuxdude
    Okay I got that part working, but I get a minor problem. I just decided to keep it on this same thread though.
    Good choice.

    Quote Originally Posted by linuxdude
    Say I was in the middle of typing the answer when the time ran out.
    Typing anything should reset or cancel the timer.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    figured it. I had another stupid prob. Thanx

Popular pages Recent additions subscribe to a feed

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