One of the common questions that you come across during exams or interviews is – Is it better to create a process or a thread?
Suppose there is a process with four independent tasks to accomplish. The system on which it is running is a quad-core system, which means every core can handle an independent process or thread. If every individual task takes 5 min, this means the process will take 20 minutes to finish all the tasks. Since four cores are available the process can either create three more child process or three threads and assign one task to each of them and do one task on its own. Hence, the four tasks will take only 5 minutes to complete.
Next, the questions come – Should the process create child processes or threads? The below figure shows the memory layout of a process.
What if we create child process?
If the main process creates three child process this means every process has to be given space in the memory because every process will have its own code section, own data section, own heap and own stack section. So, in total, a lot of memory will be required.
What if we create Threads?
But, we know that threads are part of a process. Thus, they share code, data and files of the process but have their own stacks and registers. This means that they require much less space as compared to a process. The situation in case of creating three threads will be as shown below in the diagram.
Conclusion – Is it better to create a process or a thread?
To conclude from the above discussion we can say that it is better to create threads as compared to creating processes to complete multiple tasks as it will save system resources.