c - Motivation for spawning a new process v thread -


i understand if program has large segments can executed in parallel beneficial spawn new threads when instances not bound single resource. example of web server issuing page requests.

threads beneficial aspect inter-thread communication less costly , context switching faster.

processes give more security aspect 1 process cannot "mess" processes' contents, whereas if 1 thread crashes threads crash within said process.

my question is, examples when want use process (for example fork() in c)?

i can think of if have program wants launch program make sense encapsulate in new process, feel missing larger reason starting new process.

specifically, when make sense have 1 program spawn new process vs thread?

main reason using processes process can crash or go crazy, , os limit effect has on other processes. example firefox has started running plugins in separate processes, iirc chrome runs different pages in different processes, , web servers long time have handled individual requests in separate processes.

there few different ways in oses apply limits:

  • crashes - note, if thread crashes takes down whole process. motivates browser process boundaries: browsers , browser plugins complex bits of code subject constant attack, makes sense take unusual precautions.
  • resource limits. if thread in process opens lot of files, allocates lot of memory, etc, affects you. process needn't, because can limited separately. each request in web server might more limited in resource usage server whole, because want server serve multiple requests simultaneously without 1 remote user hogging resources.
  • capabilities. varies os, example can run process in chroot jail ensure doesn't modify or read files shouldn't, no matter how vulnerable code exploits. example, symbianos has explicit list of permissions various things system ("read user phonebook", "write user phonebook", "decrypt drm files" , on). there's no way surrender permissions process has, if want highly sensitive, , fall low-sensitivity mode, need process boundary somewhere. 1 reason want security - unknown code or code might contain security flaws can sandboxed, , smaller quantity of code isn't limited can subjected increased scrutiny. reason have os enforce aspects of design.
  • drivers. in general, device driver controls shared access unique system resource. capabilities, restricting access single driver process means can forbid other processes. example iirc truecrypt on windows installs driver has enhanced permissions allow register encrypted container drive letter , act other windows filesystem. gui part of app runs in regular user mode. i'm not sure whether filesystem drivers on windows need associated process, device drivers in general might do, if isn't example gives idea.

another potential reason using processes makes easier reason code. in multi-threaded code rely on invariants of classes deduce access particular object serialized: if code isn't multi-threaded know is[*]. it's possible multi-threaded code well, of course, make sure know thread "owns" each object, , never access object thread isn't owner. process boundaries enforce rather designing it. again, not motivation, example world community grid client can use multiple cores. in mode runs multiple processes different task in each, has performance benefits of additional cores, without individual task needing parallelizable, or code task needing thread-safe.

[*] well, long wasn't created in shared memory. need avoid unexpected recursive calls , like, that's simpler problem synchronizing multi-threaded code.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -