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.

I've been reading a bit about threads and interrupts. And there is a sections which says that parallel programing using threads is simpler because we don't have to worry about interrupts.

However, what is the mechanism in which signals the release of the blocking system call if not an interrupt?

Example

I read i file in my thread which use a blocking system call to read the file from the disk.

During that time, other threads are running.

At some point the file is ready to be read from the hard disk.

Does it notify the processor of this via a hardware interrupt, so that it can do a context switch ti the thread which asked for the file?

share|improve this question
    
It's the same for threads as for completely independent processes. When a thread or a process makes a blocking call of that sort, it doesn't block execution of the entire system, it just blocks the execution of that thread or process, which, as vonbrand describes, is put to sleep until the call is completed. –  TAFKA 'goldilocks' Mar 27 '13 at 15:49
add comment

1 Answer

up vote 4 down vote accepted

Interrupts are handled by the operating system, threads (or processes, for that matter) aren't even aware of them.

In the scenario you paint:

  • Your thread issues a read() system call; the kernel gets the request, realizes that the thread won't do anything until data arrives (blocking call), so the thread is blocked.
  • Kernel allocates space for buffers (if needed), and initiates the "find the block to be read, request for that block to be read into the buffer" dance.
  • The scheduler selects another thread to use the just freed CPU
  • All goes their merry way, until...
  • ... an interrupt arrives from the disk. The kernel takes over, sees that this marks the completion of the read issued before, and marks the thread ready. Control returns to userspace.
  • All goes their merry way, until...
  • ... somebody yields the CPU by one of a thousand reasons, and it just so happens the just freed CPU gets assigned to the thread which was waiting for data.

Something like that, anyway. No, the CPU isn't asigned to the waiting thread when an interrupt happens to signal completion of the transfer. It might interrupt another thread, and execution probably resumes that thread (or perhaps another one might be selected).

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.