# Is the way an OS schedule threads related to parallel computing?

From Wikipedia:

Operating systems schedule threads in one of two ways:

Preemptive multitasking is generally considered the superior approach, as it allows the operating system to determine when a context switch should occur. The disadvantage of preemptive multithreading is that the system may make a context switch at an inappropriate time, causing lock convoy, priority inversion or other negative effects, which may be avoided by cooperative multithreading.

Cooperative multithreading, on the other hand, relies on the threads themselves to relinquish control once they are at a stopping point. This can create problems if a thread is waiting for a resource to become available.

I wonder if in parallel computing (writing and running parallelized programs in OpenMP, OpenMPI, pThread), which of Cooperative multithreading and Preemptive multitasking is/are used, or does the way OS scheduling threads have nothing to do with the multi-process or multi-thread within a parallelized program?

• What reading and research have you done to answer your question on your own? Have you read a textbook chapter on scheduling in an OS textbook? – D.W. Apr 26 '14 at 22:00
• I don't have an OS textbook. I read Wikipedia. – Tim Apr 26 '14 at 22:01
• We expect you to do more research than just reading Wikipedia, before asking here. You might want to try doing some searching and reading on sources that are available to you -- or visit a library. There's not a lot of value to the world in us re-typing out an explanation of something that's already explained in many standard textbooks. – D.W. Apr 26 '14 at 22:03
• I have done the research that I can. believe it or not. Unlike cstheory.SE, Isn't CS.SE supposed to be friendly to newbies? – Tim Apr 26 '14 at 22:05
• There's a lot of additional research you could do! If you stopped after reading Wikipedia, you aren't being resourceful enough. There are many resources on threads, operating systems, and schedulers on the Internet -- and Google can help you find them. It's not a question of being friendly or unfriendly; it's a matter of following site rules, and my expectations on this site are that you will do substantial research before asking. See How much research effort is expected of Stack Overflow users?. – D.W. Apr 26 '14 at 22:09

There is a relationship between what Wikipedia is calling "preemptive multitasking" and running multiple threads in parallel, but it's somewhat indirect.

Suppose you have two threads of computation that are independent (the memory locations they touch are non-overlapping). Then we say that those two threads can run concurrently. "Preemptive multitasking" is a way of giving the illusion that the threads are running concurrently, even though we only have a single physical processor. The single processor gets interrupted (usually by some timer hardware) every once in a while (e.g., 100 times a second), and chooses which thread to run. Since the processor is switching back and forth so quickly it is hard to tell that you aren't running simultaneously on two (slow) processors.

If you actually run the two threads on two different processors then you are really running in parallel, and there is no switching back and forth to share a single resource.

The similarity comes up because in both true parallelism and preemptive multitasking any ordering of the instructions between the two threads is possible. Thus if there is any sharing of resources, memory, or locks you need to reason about the correctness of that sharing by proving that nothing bad can happen in any possible interleaving.

With cooperative multithreading you break the illusion of having multiple processors. If a thread gets scheduled it runs as long as it wants to (and knows that the other thread is not running) and only gives up the processor by calling some system call (often called something like yield().) On the one hand, you've lost the illusion of two processors, which is problematic if one of the threads forgets to yield(). The advantage is that you know when the the threads are switching back and forth, so the number of possible interleavings you need to reason about is much smaller.