0

I was reading about non preemptive threads and I found a slide from Princeton University and it shows the following diagram: (Source Link: http://www.cs.princeton.edu/courses/archive/fall11/cos318/lectures/L5_ThreadsImplementation.pdf)

enter image description here

From what I understood is that a thread to be executed is first put into a ready queue. When it pop's out of the queue it is in running state. If it wants to invoke another thread, it calls the yield function, which will store the current state of the thread and insert it in the tail of the queue. And the thread which is in the front of the queue will be executed.

What happens if The thread is blocked (i.e. it is waiting for some resource) ? I thought in non-preemptive thread it will wait for the resource and then carry on execution.

But from the below diagram it looks as though it goes into blocked state and then is put into the ready queue ? Why is that?

user1692342
  • 5,007
  • 11
  • 69
  • 128
  • I don't understand it either. If a thread cannot obtain an I/O resource immediately, it either blocks or gets some sort of 'not ready' return from its syscall. If it blocks, the CPU it cannot use must go somewhere... – Martin James Aug 25 '14 at 16:00
  • "Non-preemptive" means that another thread cannot interrupt (preempt) a running thread, not that the running thread won't yield when it has to wait for something. If running threads did not yield when waiting for disk/network/whatever I/O, system throughput would suffer severely... Waiting for the user to "hit any key to continue" could cause the entire system to screech to a halt... – twalberg Aug 25 '14 at 17:15
  • 1
    @twalberg Does this mean that When a thread is waiting for a resource, it goes into a blocking queue, allowing other threads to execute from the ready queue? Also when the resource becomes available, it removes itself from the blocking queue and attaches to the tail of the ready queue? – user1692342 Aug 25 '14 at 17:28
  • 1
    Yes, that's exactly what the diagram shows - when a thread needs to block waiting for disk/network/keyboard/whatever, it goes into some sort of "blocked" queue, and yields the processor to something else (various implementations might use a single blocked queue, or separate per-resource queues, or something else entirely), and then the kernel moves it back to the ready queue once the resource it's waiting on is ready... – twalberg Aug 25 '14 at 17:31

1 Answers1

0

As said in the comments, non-preemptive means that another thread cannot interrupt (preempt) a running thread, not that the running thread won't yield when it has to wait for something.

When a thread is waiting for data from memory (for example), it's said to be in blocked state: its context is saved and another thread takes place in the computing resource (CPU core). When data is available in CPU's cache memory, then the first thread is said ready to resume its execution (and it will, as soon as it is the next to be executed and that the currently executed thread yields the computing resource).

This enables overlapping both data movements and threads execution, thus saving time by optimizing resource usage.

Tim
  • 2,052
  • 21
  • 30