개발

Process & Thread (프로세스와 스레드), 쓰레드?

Monsh 2016. 9. 8. 13:16
반응형




사전지식 확인을 위해서 물어볼 수 있는 흔한 문제,
Q. 프로세스(Process)와 스레드(Thread)의 차이점을 서술하시오.

A. ?




A process is an executing instance of an application.
What does that mean?

Well, for example,
when you double-click the Microsoft Word icon,
you start a process that runs Word.
A thread is a path of execution within a process.
Also, a process can contain multiple threads.

When you start Word,
the operating system creates a process
and begins executing the primary thread of that process.
It’s important to note that a thread can do anything a process can do.
But since a process can consist of multiple threads,
a thread could be considered a ‘lightweight’ process.

Thus, the essential difference between a thread
and a process is the work that each one is used to accomplish.
Threads are used for small tasks,
whereas processes are used for more ‘heavyweight’ tasks
– basically the execution of applications.


Another difference between a thread
and a process is that threads
within the same process share the same address space,
whereas different processes do not.
This allows threads to read from
and write to the same data structures and variables,
and also facilitates communication between threads.

Communication between processes
– also known as IPC, or inter-process communication –
is quite difficult and resource-intensive.




MultiThreading


Threads, of course, allow for multi-threading.
A common example of the advantage of multithreading is
the fact that you can have a word processor
that prints a document using a background thread,
but at the same time another thread is running that accepts user input,
so that you can type up a new document.


If we were dealing with an application that uses only one thread,
then the application would only be able to do one thing at a time
– so printing and responding to user input at the same time
would not be possible in a single threaded application.


Each process has it’s own address space,
but the threads within the same process share that address space.
Threads also share any other resources within that process.
This means that it’s very easy to share data amongst threads,
but it’s also easy for the threads to step on each other,
which can lead to bad things.

Multithreaded programs must be carefully programmed
to prevent those bad things from happening.
Sections of code that modify data structures shared
by multiple threads are called critical sections.
When a critical section is running in one thread
it’s extremely important
that no other thread be allowed into that critical section.
This is called synchronization,
which we wont get into any further over here.
But, the point is that multithreading requires careful programming.


Also, context switching between threads is
generally less expensive than in processes.
And finally, the overhead (the cost of communication)
between threads is very low relative to processes.


영문 출처: http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process/




반응형