多进程和多线程中的阶级和并发

发布于 2025-02-06 00:27:03 字数 310 浏览 2 评论 0原文

您好,我正在学习操作Sysyem。我认识到Parrallelism和并发之间的区别,但我仍然在某个时候想知道,所以我想获得一些帮助,谢谢!

  1. 我所知道的是,线程在多线程中是平行的,但是线程之间有上下文切换。怎么可能?当他们接近共享资源时,这会发生吗?

  2. 在4杆8个线程的情况下。流程运行是同时运行的还是同时进行的?如果它们同时运行,则处理过程相互切换,但只有2个线程在CPU中随时一次运行?

  3. 我听说Coroutine是并发的。这意味着它没有共享任何资源,但是种族状况如何仍在那里?

Hello I'm studying Operating Sysyem. I recognize the difference between parrallelism and concurrency but I still wonder at some point so I wanna get some help thank you!

  1. What I know is that threads are parallel in multiThreading but there are contexts switching among threads. How does it possible? Does that happen when they approach to shared resources?

  2. In case of 4cores 8threads. Are processes running parrallel or concurrently? If they run concurrently, processes switch each other but only 2 threads are running at once at any time in cpu right?

  3. I heard coroutine is concurrent. Which means it doesnt share any resources but how can race conditions still happen there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橪书 2025-02-13 00:27:03
  1. 需要上下文切换,因为通常我们执行比CPU内核更多的线程。是的,这通常会发生在当前运行的线程必须等待某些东西时(我想这是您所说的“共享资源”的意思),然后线程进入睡眠状态,另一个被唤醒。但是上下文切换几乎可以在任何时间发生 - OS调度程序负责以一个或几个线程不消耗所有资源的方式在线程之间共享CPU内核。

  2. “过程是否正在运行parrallal或并发?” - 过程还是线程?线程同时且并行运行,但是并行性限制为4。

  3. 我不知道您在这里是什么意思。使用与线程类似的方式并发,使用它们时的资源共享构成了类似的挑战。

  1. Context switching is required, because usually we execute more threads than we have CPU cores. And yes, this often happens when the currently running thread has to wait for something (I guess this is what you meant by "shared resource"), then the thread goes to sleep and another is awaken. But context switching could also happen at almost any time - OS scheduler is responsible for sharing CPU cores between threads in the way that one or few threads don't consume all resources.

  2. "Do processes are running parrallel or concurrent?" - processes or rather threads? Threads run both concurrently and in parallel, but the parallelism is limited to 4. And no, there is only one thread running in a specific core at a time (I ignore hyper-threading here).

  3. I have no idea what do you mean here. Coroutines are concurrent in a similar way to threads and the resource sharing pose a similar challenge when using them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文