Skip to main content
added 2 characters in body
Source Link

When it comes to Linux processes and threads are kind of the same thing. Which is to say they are created with the same system call: clone.

If you think about it, the difference between threads and processes is in which kernel objects will be shared by the child and parent. For processes, it's not a lot: open file descriptors, memory segments which haven't been written to, probably a few others which I can't think of off the top of my head. For threads, a lot more objects are shared, but not all.

What makes threads and objects closer in Linux is the revokeunshare system call. Kernel objects which start out as being shared can be unshared after thread creation. So you can, for example, have two threads of the same process which have different file descriptor space (by revoking sharing of file descriptors after the threads are created). You can test it yourself by creating a thread, calling revokeunshare in both threads and then closing all files and opening new files, pipes or objects in both threads. Then look in /proc/your_proc_fd/task/*/fd and you'll see that each task (which you created as a thread) will have different fd's.

In fact, both creation of new threads and of new processes are library routines which call clone underneath and specify which of the kernel objects the newly created process-thread-thingamajig (ie, task) will share with the calling process/thread.

When it comes to Linux processes and threads are kind of the same thing. Which is to say they are created with the same system call: clone.

If you think about it, the difference between threads and processes is in which kernel objects will be shared by the child and parent. For processes, it's not a lot: open file descriptors, memory segments which haven't been written to, probably a few others which I can't think of off the top of my head. For threads, a lot more objects are shared, but not all.

What makes threads and objects closer in Linux is the revoke system call. Kernel objects which start out as being shared can be unshared after thread creation. So you can, for example, have two threads of the same process which have different file descriptor space (by revoking sharing of file descriptors after the threads are created). You can test it yourself by creating a thread, calling revoke in both threads and then closing all files and opening new files, pipes or objects in both threads. Then look in /proc/your_proc_fd/task/*/fd and you'll see that each task (which you created as a thread) will have different fd's.

In fact, both creation of new threads and of new processes are library routines which call clone underneath and specify which of the kernel objects the newly created process-thread-thingamajig (ie, task) will share with the calling process/thread.

When it comes to Linux processes and threads are kind of the same thing. Which is to say they are created with the same system call: clone.

If you think about it, the difference between threads and processes is in which kernel objects will be shared by the child and parent. For processes, it's not a lot: open file descriptors, memory segments which haven't been written to, probably a few others which I can't think of off the top of my head. For threads, a lot more objects are shared, but not all.

What makes threads and objects closer in Linux is the unshare system call. Kernel objects which start out as being shared can be unshared after thread creation. So you can, for example, have two threads of the same process which have different file descriptor space (by revoking sharing of file descriptors after the threads are created). You can test it yourself by creating a thread, calling unshare in both threads and then closing all files and opening new files, pipes or objects in both threads. Then look in /proc/your_proc_fd/task/*/fd and you'll see that each task (which you created as a thread) will have different fd's.

In fact, both creation of new threads and of new processes are library routines which call clone underneath and specify which of the kernel objects the newly created process-thread-thingamajig (ie, task) will share with the calling process/thread.

Source Link

When it comes to Linux processes and threads are kind of the same thing. Which is to say they are created with the same system call: clone.

If you think about it, the difference between threads and processes is in which kernel objects will be shared by the child and parent. For processes, it's not a lot: open file descriptors, memory segments which haven't been written to, probably a few others which I can't think of off the top of my head. For threads, a lot more objects are shared, but not all.

What makes threads and objects closer in Linux is the revoke system call. Kernel objects which start out as being shared can be unshared after thread creation. So you can, for example, have two threads of the same process which have different file descriptor space (by revoking sharing of file descriptors after the threads are created). You can test it yourself by creating a thread, calling revoke in both threads and then closing all files and opening new files, pipes or objects in both threads. Then look in /proc/your_proc_fd/task/*/fd and you'll see that each task (which you created as a thread) will have different fd's.

In fact, both creation of new threads and of new processes are library routines which call clone underneath and specify which of the kernel objects the newly created process-thread-thingamajig (ie, task) will share with the calling process/thread.