When to use a Thread or Process?

Asked

Viewed 77 times

-1

I am preparing for a proof of Operating Systems and I would like to know what are the best situations to use a thread or create another process.

  • 2
  • Processes are isolated memory areas (they do not share data in memory easily and therefore it is more difficult to propagate problems to other processes). The context exchange between them is more expensive than between the threads of the same process. Processes are more advantageous when you don’t need to create many and want more isolation between them. Inter-process data exchange requires IPC (inter-process communication) mechanisms provided by the operating system.

  • Threads within the same process share memory (which makes it easier to access common data, although it requires synchronization to avoid disputing conditions) and context exchange is less costly. They are more advantageous when you want to create a higher number of tasks and share data between them, but with the disadvantage of a failure in one of them being able to propagate to the others. The free OS book linked in the details of the tag [tag:operating system] explains better.

1 answer

0


Look, first of all the Linux process has at least one thread.

Process is a running program that has at its disposal a virtual memory space. It is administered by direct calls to the kernel. They are robust and relatively easy to work with. For the one process you can generate a Fork, which would be a duplication of that process from your call generating children, in an attempt to parallelize actions of the main process. They perform in different contexts with segmented memory and as a form of communication is through IPC (inter process comunication), usually by PIPE, FIFO or Socket.

The thread, on the other hand, has some different characteristics. Understand it as a way to segment operations of a process, they call it a lightweight process (although I don’t see it that way). Threads are usually lighter to generate than a process and usually have access in the same memory context which requires more experience to program. This brings some advantage as the parallelism, but if it is not administered this shared access can generate problems and to try to solve it you use mechanism like traffic lights or mutex in cases of competing accesses.
It also has the ability to use threads without competition, such as lightweight threads, generating its own thread management. A very good material for you to study are the protothread, an open header lib you have. Take a look and debug it. is usually faster, lighter than a thread generated by a call in the process and only has an extra job to generate.

Now, these are complicated matters, it is not enough just to know what to answer, see the links Low, a thorough. Operating system is not a trivial matter and not even simple to deal with, and even answering you still run the risk of having said nonsense. So you can’t neglect, even though it’s very common to see people not taking this seriously.
Links:
http://www.dunkels.com/adam/pt/
https://www.man7.org/linux/man-pages/man2/fork.2.html
https://man7.org/linux/man-pages/man2/mmap.2.html
https://www.geeksforgeeks.org/thread-in-operating-system/

Good studies

  • I talked a lot and I didn’t say everything when I wore it? Ai depends, thread is very specific and used more when parallel effort is needed with access to the same memory region. Since the process has no way not to use, it will always be with you as at least a thread. Just be careful that thread amount is no solution to paralysis, the number of threads in the process supported by your processor threads has a limit too, passing from that you have a bottleneck and will lose performance with extra work.

  • For child processes, when you want to duplicate your execution for the other work performing a similar operation, but with your own memory, like tabs of a browser. This limits the access to the data that one tab has with another, which is difficult to do if you generate a thread per tab, being able to risk a tab accessing memory of the other and breaking the execution.

  • Where thread can be useful? Clocks, event handling (even if working threads with events is a difficult thing to do, since one is in a blocking and other non-blocking context and has to take care because an epoll on a thead without the proper flags causes them to conflict and break. Process some information or calculate parallel and asynchronous to the main process. In short, if you really need something external to your process working some specific function that feeds the process and that can be maintained in parallel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.