scatman
scatman

Reputation: 14565

processes vs threads (user vs kernel)

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?

Upvotes: 0

Views: 1297

Answers (2)

caf
caf

Reputation: 239341

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.

Upvotes: 1

John
John

Reputation: 6805

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.

Upvotes: 1

Related Questions