跳至主要内容

Who request reading?

Origin

一个问题:一个进程要读取文件,在底层用的是DMA的方式,DMA完成文件读取以后要通过中断让CPU去处理,但是CPU和中断处理程序根本不知道进程的,他怎么去和它进行关联,如何去唤醒那个等待的进程?
A problem: A process want to read a file. This computer use DMA to handle io request. DMA will finish the reading/writing then interrupt CPU to make CPU handle the next step. But CPU and interrupt handler of io have no idea which process request for the io, how could they wake up that waiting process?

What I have done in my os lab

github repository of my os lab

Process:

  • system call write(int fildes, const void *buf, size_t nbyte) is invoked
  • context switch happens: move to kernel state, because a system call is invoked
  • user process send a message(IPC in my os lab) to FM(file manager – controll the service related with file) and suspend itself
  • FM combine the information in file table(file descriptor lead us to file ) to find file info
  • FM send message to corresponding driver(ide – disk driver, tty – terminal driver, memory) – in our case, is hard disk drive – ide
  • FM suspend itself
  • ide receive the request and write to hardware port to read information
  • ide waiting for the hardware interrupt about the finishing of the data.
  • when finished, ide send message back to wake up FM, FM do the same thing to wake up user process.

Sequential access

As shown in my os lab, there exist two very obvious feature:
  • all read/write are sequenced, served as FIFO
  • read/write are blocked
The following is some core code about this:
void
disk_do_read(void *buf, uint32_t sector) {
    int i;
    Msg m;

    ide_prepare(sector);
    issue_read();

    // block process when the reading is not finished
    do {
        receive(MSG_HARD_INTR, &m);
    } while (m.type != IDE_READY);

    // then copy code
    for (i = 0; i < 512 / sizeof(uint32_t)
                                ; i ++) {
        *(((uint32_t*)buf) + i) = 
              in_long(IDE_PORT_BASE);
    }
}

Real os implementation

Behavior

But it seems contradictory with our intuition: we can copy multiple files simultaneously in your operating system, right? It seems that they are not sequential read/write, really? Does that mean real OS using more advanced approach to handle this?

Intuition

If we want to mimic real OS’s behavior, we may use this way: split a very large IO task into a small one and a large one, solve a small one and put other part IO task still in the working queue. In this way, next IO task can be responded in time.
Of course, in order to get fine responsiveness, we will pay some cost: performance lost. It will cost more time to finish the large IO task compared with old way.

Real system

Cited from Modern Operating Systems: 5.3.2 Device Drivers
Next the driver may check if the device is currently in use. If it is, the request will be queued for later processing. If the device is idle, the hardware status will be examined to see if the request can be handled now.
After the commands have been issued, one of two situations will apply. In many cases the device driver must wait until the controller does some work for it, so it blocks itself until the interrupt comes in to unblock it. In other cases, however, the operation finishes without delay, so the driver need not block.
We can get from upper words: no new request for a single driver can start if old task is not finished and new request will be queued. This means only one process is waiting for data from this single driver, other process request for data has not read at all(request is queued, and user process is also waiting for the response).
And In the real operating system, a large file is actually many small blocks, reading from them is already split into many small read request, which is the same with our intuition in essence.

reference

Written with StackEdit.

评论

此博客中的热门博文

Spring Boot: Customize Environment

Spring Boot: Customize Environment Environment variable is a very commonly used feature in daily programming: used in init script used in startup configuration used by logging etc In Spring Boot, all environment variables are a part of properties in Spring context and managed by Environment abstraction. Because Spring Boot can handle the parse of configuration files, when we want to implement a project which uses yml file as a separate config file, we choose the Spring Boot. The following is the problems we met when we implementing the parse of yml file and it is recorded for future reader. Bind to Class Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties. As the document says, there exists three ways to access properties in *.properties or *.yml : @Value : access single value Environment : can access multi

Elasticsearch: Join and SubQuery

Elasticsearch: Join and SubQuery Tony was bothered by the recent change of search engine requirement: they want the functionality of SQL-like join in Elasticsearch! “They are crazy! How can they think like that. Didn’t they understand that Elasticsearch is kind-of NoSQL 1 in which every index should be independent and self-contained? In this way, every index can work independently and scale as they like without considering other indexes, so the performance can boost. Following this design principle, Elasticsearch has little related supports.” Tony thought, after listening their requirements. Leader notice tony’s unwillingness and said, “Maybe it is hard to do, but the requirement is reasonable. We need to search person by his friends, didn’t we? What’s more, the harder to implement, the more you can learn from it, right?” Tony thought leader’s word does make sense so he set out to do the related implementations Application-Side Join “The first implementation

Implement isdigit

It is seems very easy to implement c library function isdigit , but for a library code, performance is very important. So we will try to implement it and make it faster. Function So, first we make it right. int isdigit ( char c) { return c >= '0' && c <= '9' ; } Improvements One – Macro When it comes to performance for c code, macro can always be tried. #define isdigit (c) c >= '0' && c <= '9' Two – Table Upper version use two comparison and one logical operation, but we can do better with more space: # define isdigit(c) table[c] This works and faster, but somewhat wasteful. We need only one bit to represent true or false, but we use a int. So what to do? There are many similar functions like isalpha(), isupper ... in c header file, so we can combine them into one int and get result by table[c]&SOME_BIT , which is what source do. Source code of ctype.h : # define _ISbit(bit) (1 << (