进程与线程(用户与内核)
我了解进程和线程之间的区别。我知道用户线程和内核线程之间的区别。
问题
你如何用 C 语言编写它们中的任何一个?我在 C 中只知道如何创建 POSIX 线程,但是这是用户线程还是内核线程?
任何人都可以为进程、用户线程和内核线程提供一些 C 代码示例吗?
是否有我未包含的任何类型的线程?
I understand the difference between a process and a thread. And I know the difference between a user thread and a kernel thread.
Question
How do you code any of them in C? All I know in C is how to create POSIX threads, but is this user threads or kernel threads?
Can anyone put some C code samples for a process, user thread and a kernel thread.
Are there any type of threads that I did not include?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题的答案主要取决于您的操作系统。 POSIX 线程可以实现为用户线程或内核线程 - 它只是一个 API 规范。在任何现代 Linux 系统上,它们都是通过内核线程实现的。
就较低级别的 API 而言,UNIX 系统调用
fork()
创建一个新进程。在 Linux 上,系统调用clone()
可用于创建新的内核线程(通过传递CLONE_VM
标志) - 其他操作系统将有其他调用来执行此操作。用户线程的创建完全取决于您使用的用户线程库。The answers to this mostly depends on your operating system. POSIX threads can be implemented as either user threads or kernel threads - it is just an API specification. On any modern Linux system, they are implemented with kernel threads.
In terms of lower-level APIs, the UNIX system call
fork()
creates a new process. On Linux, the system callclone()
can be used to create a new kernel thread (by passing theCLONE_VM
flag) - other operating systems will have other calls to do this. Creation of a user thread will depend entirely on what user threading library you are using.有一个 教程 应该对线程有所帮助。您可以使用 pthread_create 的不同 attr 参数来选择用户还是内核。
对于进程,请尝试fork 教程。
There's a tutorial that should help with threads. You can use a different attr parameter to pthread_create to choose user vs kernel.
For processes, try the fork tutorial.