Saturday, July 21, 2012

Difference between running a thread and a process is that when you are running a thread some data-structures are shared across the thread. For doing the same we will need IPC.

According to a nice post I found online here is the explanation:inux
uses a 1-1 threading model, with (to the kernel) no distinction between
processes and threads -- everything is simply a runnable task. *

On Linux, the system call clone clones a task, with a configurable level of sharing, among which are:

CLONE_FILES: share the same file descriptor table (instead of creating a copy)

CLONE_PARENT: don't set up a parent-child relationship between the new task and the old (otherwise, child's getppid() = parent's getpid())

CLONE_VM: share the same memory space (instead of creating a COW copy)

forking costs a tiny bit more than pthread_createing
because of copying tables and creating COW mappings for memory, but the
Linux kernel developers have tried (and succeeded) at minimizing those
costs.

Switching
between tasks, if they share the same memory space and various tables,
will be a tiny bit cheaper than if they aren't shared, because the data
may already be loaded in cache. However, switching tasks is still very
fast even if nothing is shared -- this is something else that Linux
kernel developers try to ensure (and succeed at ensuring).

Why running processes on a SMP could be better than threading

In fact, if you are on a multi-processor system, not sharing
may actually be beneficial to performance: if each task is running on a
different processor, synchronizing shared memory is expensive.

** Simplified. There exist both SYS_fork and SYS_clonesyscalls, but in the kernel, the sys_forkand sys_clone are both very thin wrappers around the same do_fork function, which itself is a thin wrapper around copy_process. Yes, the terms process, thread, and task are used rather interchangeably in the Linux kernel...