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
198 Views Asked by João Vilaça At
2
There are 2 best solutions below
1
Eugeniu Rosca
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;
}
Related Questions in C
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in BASH
- How do I recursively find and replace only in files named index.php on Linux webserver?
- Delete the extra space after special character in all the lines of text file
- Calling a python function with options from shell script
- bc: prevent "divide by zero" runtime error on multiple operations
- Multiple commands with find and xargs, also accounting for special characters
- How to split a directory into parts without compressing or archiving?
- concat a lot of files to stdout
- Honoring quotes while reading shell arguments from a file
- No laravel sync folders in homestead vagrant on windows
- Grouping commands in curly braces and piping does not preserve variable
- SWI Prolog pass a goal with non-zero arity through the command line arguments
- Evaluating condition of if statement in awk using a second file
- How to customise bash completion to pick only a custom set of commands?
- Bash regular expression execution hangs on long expressions
- Bitwise OR in bash arguments with square brackets
Related Questions in SHELL
- passing text with \n as one argument in shell
- Delete the extra space after special character in all the lines of text file
- Calling a python function with options from shell script
- bc: prevent "divide by zero" runtime error on multiple operations
- schedule and automate sqoop import/export tasks
- How can launch an external process from java and still be able to interact with this process?
- Linux find files where mtime and ctime are not equal
- Find all files contained into directory named
- Quick way to remove all folders titled CVS in a directory and it's subdirectories?
- shell process not exiting on `exit` inside `$()`
- How to set environment variables with a forward slash in the key
- System 'bash -ic' stuck when I hit ctrl+c
- bash functions returns "command not found"
- Why does pattern "*.so?(.*)" produce a syntax error in a script but not on command line?
- retrieve plaintext password from file using bash command
Related Questions in SIGNALS
- FFT Filtering of signal
- VHDL, concurrent signal assignment wrong on FPGA but right in Modelsim
- How to config Ctrl+u to send signal SIGUSR1 from console
- Forwarding signals in bash script which is submitted on the cluster
- Modify Control C Command Signal to Allow Input
- Get Exact Frequency From Digital Signal
- Messing with signals, pipes and forks in C
- Conceptual Questions About Processes and Signals
- starting a new process group from bash script
- How to get the NAME OF an INSTANCE in node.js
- Wait for signal to start generating data from another process in python
- pthreads SIGEV_THREAD and async-safe function calls
- GenerateConsoleCtrlEvent crashes when child process is cmd
- What does signal(SIGPIPE, SIG_IGN); do?
- Synchronizing processes with semaphores and signals in C
Related Questions in FORK
- Drawing with ncurses, sockets and fork
- Switch parent and child process
- python forked processes not executing with os.execlp
- Messing with signals, pipes and forks in C
- fork()ing with c++ and creating 4 childs of a parent
- How to control the thread of child process
- Error running this fork code in my eclipse, and also have some concept confusion around this code
- How to tell if child Node.js Process was from fork() or not?
- correct output for this fork concept in C
- Publish fork of GitHub project to new NPM module but keep option to merge with original?
- Program stuck on Pipe (exec ls grep sort)
- How to prevent child from interfering with parent's stdin after fork()
- C++ Fork child, ask child for process list, kill a process in Linux
- How merge 2 github repository to trigger a pullrequest?
- How many processes this Program Creates
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
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.