6

I am trying to trace a high level function call that blocks a certain process. An example of such is scanf, which blocks the terminal until it receives a '\n' . Now I traced scanf down to the getc (scanf uses getc to acquire characters from stdin). My question is, what is the process that takes to interpret the data that comes from the keyboard, all the way through the kernel and to the return of getc? Also how does scanf stops the terminal (is the computer idling, or working on another task)? Thank You

7
  • 3
    Are you asking us to point you to strace, or are you asking how blocking system calls in general work? Commented Feb 3, 2012 at 11:30
  • 1
    If you were interested on how it's done on assembler level: programmersheaven.com/mb/x86_asm/295346/295346/keyboard-input :) Commented Feb 3, 2012 at 11:31
  • 2
    Try running it under a debugger such as gdb. Hit control-C, then type "bt". Scanf calls getc, which calls read, which is a systemcall that does not return until there actually is somthing to read. You are blocked in read. Commented Feb 3, 2012 at 11:32
  • 1
    @Eregrith, you are talking about the shell, not the terminal. The terminal is a physical device (or a piece of software emulating a physical device). Commented Feb 3, 2012 at 12:25
  • 1
    @pretobomba scanf is blocking on a read from a file descriptor, not the terminal. Very often, there is no terminal involved. Commented Feb 3, 2012 at 12:27

2 Answers 2

5

Whenever a process issues a system call (such as a blocking read(2)), the process starts to execute in kernel mode, that is, the kernel code that handles the particular system call is invoked.

After that, depending on the underlying device and the driver, the process can be suspended and put in a wait-queue. When a key is pressed, the kernel code that handles interrupts is invoked and from there it is deducted which key is pressed.

The kernel then resumes the process that is waiting for this input, and delivers the data by copying it from the kernel address space to the particular process' address space.

Sign up to request clarification or add additional context in comments.

Comments

1

The system call enables the user program to get executed in previliged mode. When the user program makes the system call, it generates the interrupt 0x80. When the kernel receives the interrupt, it will do a lookup on Interrupt Descriptor Table (IDT) for 0x80 and executes the corresponding handler (syscall). After the execution of this handler, the control will be transferred to user program after copying the information from kernel memory to user memory.

In this case, scanf is mapped to the library function, "read". The "read" system call invokes "sys_read" which then reads from the input stream STDIN using getc. Hope this helps!

1 Comment

It's not necessarily int 80h. x86 provides SYSCALL/SYSENTER as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.