-1

-Interview Question

I was asked the disadvantages of thread. And what are the scenario where we shouldn't use thread instead use process?

I couldn't think much except invalid memory access in some case.

roottraveller
  • 7,942
  • 7
  • 60
  • 65
  • Possible duplicate of [What is the difference between a process and a thread?](https://stackoverflow.com/q/200469/62576) – Ken White Sep 14 '17 at 12:51
  • @KenWhite this post contain only positive points of thread. – roottraveller Sep 14 '17 at 12:54
  • 1
    Threads do have a lot of advantages. It is just that a lot of programmers don't know how to write threaded code correctly. Deadlock and race bugs are very hard to diagnose and fix. Much less of a problem with a single-threaded process, it isolates memory so a thread in another process can't corrupt anything. That isolation also makes it hard to get processes to co-operate however, doing something about that tends to cause bugs that are very hard to diagnose and fix. – Hans Passant Sep 14 '17 at 13:23
  • @HansPassant __Process__ too suffer deadlock and race condition. – roottraveller Sep 14 '17 at 13:31
  • That's something you can say if you don't want to get hired. Just beef-up on this stuff, it is important. – Hans Passant Sep 14 '17 at 13:44
  • that is a clever move, i guess then – roottraveller Sep 14 '17 at 14:42

1 Answers1

1

Threads, spawned by the same process, all share the same memory. Processes all run in their own memory context.

In Linux (I don't know what the behavior under Windows is like) a newly spawned child process will usually received a copy of certain part the parent process' memory context an therefore is more expensive memory-wise at runtime and CPU-time/MMU wise at creation. Also context switching - (off)loading the process from or to the CPU (this happens, when a process or thread has nothing to do and is pushed to a queue in favor of processes or threads with actual work) - might be more expensive with a process.

On the other hand processes might be much more secure since their memory is isolated from the memory of their sibling processes.

EXIT_FAILURE
  • 278
  • 1
  • 13