processes vs threads (user vs kernel)
I understand the difference between a process and a thread. And I know the differen开发者_运维技巧ce 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?
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 call clone()
can be used to create a new kernel thread (by passing the CLONE_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.
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.
精彩评论