Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What actually happens to a process waiting for user input?

Tags:

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?

like image 906
techiek7 Avatar asked Oct 05 '16 10:10

techiek7


1 Answers

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 (+).

like image 161
r3mainer Avatar answered Sep 23 '22 16:09

r3mainer