Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What's the difference between slow system calls and fast system calls? I have learned that slow system call can block if the process catches some signals, because the caught signals may wake up the blocked system call, but I can't exactly understand this mechanism. Any examples would be appreciated.

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

There are in fact three gradations in system calls.

  1. Some system calls return immediately. “Immediately” means that the only thing they need is a little processor time. There's no hard limit to how long they can take (except in real-time systems), but these calls return as soon as they've been scheduled for long enough.
    These calls are usually called non-blocking. Examples of non-blocking calls are calls that just read a bit of system state, or make a simple change to system state, such as getpid, gettimeofday, getuid or setuid. Some system calls can be blocking or non-blocking depending on the circumstances; for example read never blocks if the file is a pipe or other type that supports non-blocking reads and the O_NONBLOCK flag is set.
  2. A few system calls can take a while to complete, but not forever. A typical example is sleep.
  3. Some system calls will not return until some external event happens. These calls are said to be blocking. For example, read called on a blocking file descriptor is blocking, and so is wait.

The distinction between “fast” and “slow” system calls is close to non-blocking vs. blocking, but this time from the point of view of the kernel implementer. A fast syscall is one that is known to be able to complete without blocking or waiting. When the kernel encounters a fast syscall, it knows it can execute the syscall immediately and keep the same process scheduled. (In some operating systems with non-preemptive multitasking, fast syscalls may be non-preemptive; this is not the case in normal unix systems.) On the other hand, a slow syscall potentially requires waiting for another task to complete, so the kernel must prepare to pause the calling process and run another task.

Some cases are a bit of a gray area. For example a disk read (read from a regular file) is normally considered non-blocking, because it's not waiting for another process; it's only waiting for the disk, which normally takes only a little time to answer, but won't take forever (so that's case 2 above). But from the kernel's perspective, the process has to wait for the disk driver to complete, so it's definitely a slow syscall.

share|improve this answer
    
thanks a great! but if the file is a pipe, is reading file non-blocking? see that www2.hawaii.edu/~esb/2007spring.ics612/apr10.html –  KayKay Jun 4 '11 at 9:51
    
it denotes "slow reads/writes (e.g. on a pipe or a terminal) " –  KayKay Jun 4 '11 at 9:52
    
@KayKay: For a pipe, you can have both, depending on the state of the O_NONBLOCK flag. If the flag is set, the syscall can complete without waiting on anything else, so it's non-blocking, and the kernel can treat it as a fast syscall. –  Gilles Jun 4 '11 at 9:55
    
you mean it depends on O_NONBLOCK flag! –  KayKay Jun 4 '11 at 10:01
add comment

A slow system call is something like a TCP socket read() - if you don't have O_ASYNC (or whatever) set, it can wait for ever.

A fast system call is something like gettimeofday() or getpid(), both of which return information to the process that the kernel has immediately available.

Disk reads fall in the category of slow system calls. If a process does a read() on a true disk file, file descriptor, the kernel may have to read in one or more disk blocks to satisfy the read. Depending on the underlying file system's on-disk structure, this may mean reading the on-disk-inode to get the disk block number of an "indirect block", reading the indirect block to get the data block and then reading the data block itself. Quite time consuming, at least in terms of CPU cycles per disk access, probably worse today than it was in the Good Old Days.

I haven't seen this in ages, but the "bottom half" of old Unix disk drive device driver code would block signals/interrupts so that it was easier to maintain on-disk filesystem integrity. Occasionally, a buggy driver or failing disk would never deliver the disk block a process had asked for, and the process slept forever. Even a kill -9 did nothing to it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.