简体   繁体   English

应该在上下文切换之前完成系统调用

[英]should system calls be completed before context switch

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 ? 还是取决于系统调用类型或操作系统实现?

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. 大多数C编译器工具链都包含单线程和多线程库。 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. 应用程序编写者不应该永远不必担心操作系统如何管理共享资源的访问内部,这样你就可以调用openreadwrite您的应用程序,而无需担心,只要你不分享你的进程中的线程之间的任何手柄。

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). 应用程序编写者负责保护自己的共享资源,并记录所有流程并发问题(进程间资源共享)。

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. 现在,如果您的单核CPU要执行多任务,则您希望它在等待时运行其他线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM