Page 1 of 1

Async I/O

Posted: Thu Jan 03, 2008 5:31 am
by Jeko
I'm writing the device manager of my OS. I want this internally asyncronous.

Each device in my device manager has a linked list of requests that the apps did. When an app make a request I take the physical address of the buffer in the request and I lock it (because the address space can change between the request and the finish of the request).
But how can I do when an app did a request but it die before the finish of the request?

I don't know if I made myself clear, but I hope you could help me.
Thanks! And sorry for my bad english!

Re: Async I/O

Posted: Thu Jan 03, 2008 6:10 am
by Brendan
Hi,

If a task is terminated for any reason, then any outstanding asynchronous requests it made need to be cancelled.

Cancelling I/O requests is a multi-level thing. For example, if an application asks to read data from a file then terminates; then the VFS may need to cancel the application's requests and tell the file system code to cancel the VFS's request; and the file system code may need to cancel the VFS's request and tell the disk driver to cancel the file system code's request; and the file system may need to cancel the file system code's request.

At any level it may not be possible to cancel the request, so the next level up may need to ignore an unwanted response for a previously cancelled request. For example, if the disk driver just finished reading sectors for a request then it's easier for the disk driver to complete the file system code's request than to cancel the (already completed) operation.

Note1: Being able to ignore unwanted responses for previously cancelled requests also fixes problems with race conditions.

Note2: If you don't cancel the request at all levels, then lower levels will complete the request which will waste resources (CPU time, disk I/O, etc).

Note3: Only the highest level needs to find out that the task that made the request was terminated.

In addition, this may also need to work in the opposite direction. For example, if an application requests to read data from a file that's on a CD and the user removes the CD, then the CD-ROM driver will need to cancel any pending requests by telling the file system code the request was cancelled (and the file system code would tell the VFS, which would tell the application). This applies to all removable media (flash memory, CD-ROM, hot-plug hard drives, floppies) and anything else that may become unplugged (e.g. all types of networking). Depending on your OS (if it has any fault tolerance) it may also apply to all devices. For e.g. if the floppy driver crashes then the file system code might detect that and cancel it's request, and tell the VFS the request was cancelled.

Once you've got "internally asynchronous" working correctly, why not extend it to "internally and externally asynchronous" (even if it is just POSIX asynchronous I/O compatability)?


Cheers,

Brendan

Re: Async I/O

Posted: Thu Jan 03, 2008 6:24 am
by Jeko
Brendan wrote:Hi,

If a task is terminated for any reason, then any outstanding asynchronous requests it made need to be cancelled.

Cancelling I/O requests is a multi-level thing. For example, if an application asks to read data from a file then terminates; then the VFS may need to cancel the application's requests and tell the file system code to cancel the VFS's request; and the file system code may need to cancel the VFS's request and tell the disk driver to cancel the file system code's request; and the file system may need to cancel the file system code's request.

At any level it may not be possible to cancel the request, so the next level up may need to ignore an unwanted response for a previously cancelled request. For example, if the disk driver just finished reading sectors for a request then it's easier for the disk driver to complete the file system code's request than to cancel the (already completed) operation.

Note1: Being able to ignore unwanted responses for previously cancelled requests also fixes problems with race conditions.

Note2: If you don't cancel the request at all levels, then lower levels will complete the request which will waste resources (CPU time, disk I/O, etc).

Note3: Only the highest level needs to find out that the task that made the request was terminated.

In addition, this may also need to work in the opposite direction. For example, if an application requests to read data from a file that's on a CD and the user removes the CD, then the CD-ROM driver will need to cancel any pending requests by telling the file system code the request was cancelled (and the file system code would tell the VFS, which would tell the application). This applies to all removable media (flash memory, CD-ROM, hot-plug hard drives, floppies) and anything else that may become unplugged (e.g. all types of networking). Depending on your OS (if it has any fault tolerance) it may also apply to all devices. For e.g. if the floppy driver crashes then the file system code might detect that and cancel it's request, and tell the VFS the request was cancelled.

Once you've got "internally asynchronous" working correctly, why not extend it to "internally and externally asynchronous" (even if it is just POSIX asynchronous I/O compatability)?


Cheers,

Brendan
Thank you!

In practice, when a task dies, I must delete each his requests.
But isn't this a waste of time? I must search in each linked list of each device if there is a request of the task being deleted.
Maybe is very faster if I implement only POSIX asynchronous method with fork...
I don't know. I need advices.

Re: Async I/O

Posted: Thu Jan 03, 2008 6:50 am
by Brendan
Hi,
MarkOS wrote:In practice, when a task dies, I must delete each his requests.
But isn't this a waste of time?
It's a small waste of time that prevents a large waste of time... ;)

Imagine an application asks the VFS to load 1 GB of data from a file and then the application terminates. Do you want all other file I/O requests to wait for the file system and disk driver to load 1 GB of data from disk for no reason, or do you want to cancel the request as soon as possible?
MarkOS wrote:Maybe is very faster if I implement only POSIX asynchronous method with fork...
Then (for the 1 GB example) you'll still be waiting for 1 GB of data to be read from disk for no reason (unless you implement POSIX asynchronous I/O functions using "fork()" and also implement some sort of "cancel", even if everything isn't internally asynchronous).
MarkOS wrote:I don't know. I need advices.
Ok! :)

If your OS is a hobby/toy thing that no-one will ever actually use, then don't bother with POSIX asynchronous I/O functions (or any other asynchronous I/O) at all.

If you want your OS to be a good (high performance) OS, then make it internally and externally asynchronous (and also implement I/O priorities).

If you're only doing the OS for educational purposes (to learn more) then decide how much you want to learn and implement it according to that - if you don't want to learn how to implement high performance I/O then don't attempt it.... ;)


Cheers,

Brendan

Re: Async I/O

Posted: Thu Jan 03, 2008 7:00 am
by Jeko
Brendan wrote: Then (for the 1 GB example) you'll still be waiting for 1 GB of data to be read from disk for no reason (unless you implement POSIX asynchronous I/O functions using "fork()" and also implement some sort of "cancel", even if everything isn't internally asynchronous).
That's right! I didn't think of this...

Now I go to implement async I/O internally and externally. It's the best way to manage devices!