techiek7
techiek7

Reputation: 99

What actually happens to a process waiting for user input?

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?

Upvotes: 5

Views: 1466

Answers (1)

r3mainer
r3mainer

Reputation: 24587

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

Upvotes: 4

Related Questions