Reputation: 1466
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
Upvotes: 6
Views: 1429
Reputation: 1962
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!
Upvotes: 1
Reputation: 43508
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.
Upvotes: 5