top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to exit from scanf after some time?

+3 votes
1,049 views

I want to exit from the scanf after certain amount of time, how to proceed. Any pointer or suggestions?

posted Oct 28, 2014 by Prithvi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

One way is by using timer concept ,
start timer before scanf() function,set some amount of time and give one call back function in this function you need to write further steps.
OR
By using threads mechanism.
ex :

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
jmp_buf env;
void trigger(int sig)
{
    printf("Im in trigger\n");
    longjmp(env,2);
}

int main(void)
{
    char buf[100];
    int i;

    signal(SIGALRM, trigger);
    alarm(3);
    i = setjmp(env);

    if(!i){
    scanf("%s\n",buf);
    }else{
        printf("im in else part\n");
    }

    printf("Finall ...\n");

    return 0;
}
answer Oct 28, 2014 by Bheemappa G
But how to stop the current scanf processing after timer expiry?
I dont think you can terminate scanf programmatically, as scanf terminates only of EOF.  May be you need to break the functionality to two thread one which stop at scanf and another start at the timer expiry and does the processing beyond scanf.

Whenever timer is expired stop the thread1 which is waiting for scanf and start the thread2 which has further processing. If scanf returns successfully then stop the timer.
Try this......

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
jmp_buf env;
void trigger(int sig)
{
    printf("Im in trigger\n");
    longjmp(env,2);
}

int main(void)
{
    char buf[100];
        int i;


    signal(SIGALRM, trigger);
    alarm(3);
    i = setjmp(env);
    if(!i){
    scanf("%s\n",buf);
    }else{
        printf("im in else part\n");
    }

    printf("Finall ...\n");

    return 0;
}
Can you please explain the logic I mean role of setjmp and longjmp  :)
if we want to skip scanf we should jump after scanf or before scanf . jumping after scanf is little bit difficut so here i used setjmp and longjmp to start execution from before the scanf .(when you call longjmp the execution will be started immediately after line setjmp()).
Similar Questions
+2 votes

I know of "select()" which works using file descriptors.

However I want similar functionality to "select()" but for a simple function that returns an int.

So suppose, there's "int calculate_magic()" function and I want to allow it to work for 2 seconds, if it takes longer than that I want to move on.

If "calculate_magic()" wrote its answer to a file descriptor I could use "select()" but how to do it for the case I mentioned?

I need something like this for a simple game implementation where the player is allowed a maximum time to make a decision about their next move.
I
was hoping I can get something without having to deal with threads, etc.

+1 vote

What are the time complexities of some famous algorithms like below:

Binary Search :
Merge Sort:
Insertion Sort:
Quick Sort:
Selective Sort:
Finding max & min for a given set of number:

+1 vote

What will be the behaviour of LTE MAC,RLC & PDCP layer once ue data transmission stops & starts again after some time?

...