Reputation: 638
Is it guaranteed that system calls will be completed before context switch or preemption ? Should we expect that system call instructions are uninterruptible. Or is it depend on system call type or operating system implementations ?
Upvotes: 1
Views: 425
Reputation: 6669
Is it guaranteed that system calls will be completed before context switch or preemption
No, many system calls will actually cause context switches to happen, even in a single threaded application. Let's not forget that there are other processes running on most modern systems today, plus hardware interrupts and a host of asynchronous events.
Or is it depend on system call type or operating system implementations ?
Yes, very much so.
if those system calls thread safe what guarantees or not?
Read your system documentation.
Most C compiler tool chains include single and multi-threaded libraries. Even the single threaded libraries are context-switch safe, but not necessarily re-entrant from the same process. A context-switch is just that, the entire context of the current thread is switched out for a new one. As long as those threads aren't touching any shared resources (context overlap), there's nothing else to be concerned with. When you write multi-threaded applications however, you have to take responsibility for the resources that are shared between the threads that you own. For the most part, good design avoids shared resources, but there can be critical paths where it is unavoidable.
Most modern operating systems have many internally shared resources, and they use various techniques to synchronize access to those. Locks, semaphores, critical sections, atomic operations (lock-free algorithms) are all common practice in multi-threaded environments. Application writers shouldn't ever need to worry about how the OS manages access to shared resources internally, so you can call open
, read
and write
from your application, without concern, provided you are not sharing any handles between threads within your process.
When you write multi-threaded applications, you should use the thread-safe libraries supplied by your compiler tool chain. Thread-safe libraries use all of the same techniques that operating systems do, to protect internally shared resources, but they cannot protect you from yourself! If you share a resource between threads, such as a global variable, handle, buffers, even accesses to some kinds hardware registers, you must arrange to synchronize those accesses.
OS writers are responsible for protecting their own internally shared resources and documenting all thread-safety concerns.
Library writers are responsible for protecting their own internally shared resources and documenting all thread-safety concerns.
Application writers are responsible for protecting their own shared resources and documenting all process concurrency concerns (inter-process resource sharing).
Upvotes: 4
Reputation: 28932
No. Lets say you are waiting for a socket read to complete. This can take seconds. Now if your single core cpu is to multi-task, you expect it to run other threads while waiting.
Upvotes: 1