I want to know what actually happens to a process waiting for user input. Lets say, in my code I gave call to scanf() to read user input from console. It will internally call read() system call. But in this case there is no data to read until user gives any input. So is our process sleeping till then?
Yes, it's sleeping (in OS X, at least).
Try compiling and running the following C program:
#include <stdio.h>
int main() {
int x;
puts("Enter a number:");
if (scanf("%d",&x)) {
printf("You entered %d\n",x);
}
else {
puts("That isn't a number");
}
return 0;
}
Start the program running in the console, then open another console and enter ps -v
at the command line. You should see something like this:
PID STAT TIME SL RE PAGEIN VSZ RSS LIM TSIZ %CPU %MEM COMMAND
19544 S 0:00.01 0 0 0 2463084 1596 - 0 0.0 0.0 -bash
19574 S 0:00.01 0 0 0 2454892 1568 - 0 0.0 0.0 -bash
19582 S+ 0:00.00 0 0 0 2434816 676 - 0 0.0 0.0 ./a
Here, ./a
is the name of the program. The entry for this process in the STAT
column is S+
, which means the process is sleeping (S
) and is in the foreground (+
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With