The Wayback Machine - https://web.archive.org/web/20200701001619/https://github.com/Snailclimb/JavaGuide/issues/467
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么说linux系统相对其他系统,上下文切换时间短? #467

Open
yundongzhang opened this issue Sep 6, 2019 · 3 comments
Open
Labels

Comments

@yundongzhang
Copy link

@yundongzhang yundongzhang commented Sep 6, 2019

为什么说linux系统相对其他系统,上下文切换时间短?

@wangpeipei90
Copy link

@wangpeipei90 wangpeipei90 commented Sep 8, 2019

好想知道你在哪里看到的,上下文切换在相同的design和algorithm下难道不是拼硬件么?

@yundongzhang
Copy link
Author

@yundongzhang yundongzhang commented Sep 8, 2019

@wangpeipei90
Copy link

@wangpeipei90 wangpeipei90 commented Sep 8, 2019

找到了以下三段话。问题应该在于多线程的实现不同。Unix-like是kernel-based process, 不共享内存;而Linux使用clone,共享内存空间。这意味着在context switch的时候减少了内存置换。

Context Switching leads to an overhead cost because of TLB flushes, sharing the cache between multiple tasks, running the task scheduler etc. Context switching between two threads of the same process is faster than between two different processes as threads have the same virtual memory maps. Because of this TLB flushing is not required.

While many Unix-Like OS use kernel threads for process context switching, Linux uses kernel threads only for executing some kernel code periodically.

Most of the UNIX-like operating systems LWP (read light weight process) are kernel thread based, while on the other hand Linux handles them a bit differently. In linux, LWP are created by calling the clone() function which leads to creation of separate processes. Same task can also be carried out with fork() function but clone() lets recently created processes share their memory, address space etc. Their working in shared environment gave them a name “threads”. Hence, multi-threading is supported by both of them but they differ in internal handling of it.

@Snailclimb Snailclimb added the discuss label Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.