How can I get the a process to listen for user input without terminating? . So, for example, i want the bash to wait for X minutes, and if i say "stop" it quits, or else just keeps waiting... How can I achieve that? So, upon execution my process would wait , and then I want to be able to stop, pause or continue through stdin, typing "stop", "continue" or "pause. Thank you.
Messing with signals, pipes and forks in C
101 views Asked by João Vilaça At
2
There are 2 answers
1
On
C piece of code, responsible of reading the user actions (TESTED):
int main()
{
/* start function */
char choice;
while(1)
{
printf("Enter s (stop), c (continue) or p (pause): ");
scanf("%c",&choice);
/* protect against \n and strings */
while(choice != '\n' && getchar() != '\n');
switch(choice) {
case 's' :
printf("Stop!\n" );
/* stop function */
return 0;
break;
case 'c' :
printf("Go on!\n" );
/* resume function */
break;
case 'p' :
printf("Pause!\n" );
/* pause function */
break;
default :
printf("What?\n" );
break;
}
}
return 0;
}
A simple (but wasteful) option is that the initial program forks and then wait for input. The child process can update the counter.
When the parent program receive "pause" it sends the signal SIGTSTP to the child.
When the parent program receive "continue" it sends the signal SIGCONT to the child.
When the parent program receive "stop" it sends the signal SIGQUIT to the child.
If you want, you can also set a SIGINT handler in the parent using sigaction that kills the child when you type Ctrl+C.