C Lock System Call Tracking

I am trying to trace a call to a high-level function that blocks a specific process. An example of this is scanf, which blocks the terminal until it receives '\ n'. Now I traced scanf to getc (scanf uses getc to get characters from stdin). My question is: what process is required to interpret the data coming from the keyboard, right down to the kernel and getc return? Also, how does scanf stop the terminal (the computer is idling or is working on another task)? Thank you.

+6
source share
2 answers

Whenever a process issues a system call (for example, blocking read(2) ), the process starts in kernel mode, i.e. kernel code that processes a specific system call is called.

After that, depending on the base device and driver, the process can be paused and placed in a waiting queue. When the key is pressed, the kernel code that processes the interrupts is called, and the key pressed is subtracted from there.

The kernel then resumes the process waiting for this input and transfers the data, copying it from the address space of the kernel to the address space of a specific process.

+5
source

A system call allows the user program to start in the prevailing mode. When a user program makes a system call, it generates a 0x80 interrupt. When the kernel receives an interrupt, it will search the interrupt descriptor table (IDT) for 0x80 and execute the corresponding syscall handler. After executing this handler, control will be transferred to the user program after copying information from the kernel memory to user memory.

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

+1
source

Source: https://habr.com/ru/post/907559/


All Articles