views:

655

answers:

5
+2  Q: 

Thread and Process

What is the best definition of a thread and what is a process? If I call a function, how do I know that a thread is calling it or a process (or am I not understanding it??!). This is in a multi-core system (quadcore).

+5  A: 

From http://wiki.answers.com/Q/What_is_the_difference_between_a_computer_process_and_thread:

A single process can have multiple threads that share global data and address space with other threads running in the same process, and therefore can operate on the same data set easily. Processes do not share address space and a different mechanism must be used if they are to share data.

If we consider running a word processing program to be a process, then the auto-save and spell check features that occur in the background are different threads of that process which are all operating on the same data set (your document).

Marc Novakowski
+2  A: 

In every modern OS I know of, everything runs in a thread, which runs in a process. The OS can keep track of multiple processes, and each process can host an arbitrary number of threads. So all code is executed within a thread and within a process (since the thread runs in a process).

The main distinction between the two is that each process has its own virtual address space. Separate processes do not have access to each others' data, file handles or anything else, and are essentially not aware that other processes exist.

On the other hand, every thread in a process share the same address space, and all threads can therefore inspect or modify each others' data, call the same functions and everything else.

It is often (but not always) the cases that one program consists of one process and a number of threads.

jalf
+1  A: 

A process is composed of one or more threads (one by default for most environments). A process can create additional threads though.

Like the previous answer says, each Process has its own memory space (each can have a pointer to 0x12345, with that memory location having different values for each process), while all the Threads of a process would actually point to the exact same memory location, since they're all in the same memory space.

When calling a function, it's almost always called on the same thread that the caller is running on. In Objective-C, there are exceptions (performSelectorOnMainThread), and there might be for other languages as well, but that sort of functionality is necessary only in special cases.

xtophyr
In all languages I am familiar with (and that most developers use) all functions are executed on the same thread that is calling the function. If the function in question happens to spin off a new thread or send a method call onto a different thread, that's different that what you are saying.
siz
+1  A: 

From a user's point of view, the main distinction is that threads share memory with each other, while processes do not. That means you can easily share data between threads, while processes require some kind of OS call to do so.

Some call this a benifit of threads, but sharing data between multiple threads of control is fraught with danger, so it can be argued that processes lead to more reliable code.

There's a lot more to it, particularly if you are an OS person.

T.E.D.
+2  A: 

One thing to add is how does a multi-core processor handle this. Think of a thread as the sequential execution of your code.

A core in a CPU can only execute one thread at a time. So if this thread is blocked because the program is waiting for an I/O operation to finish, the process is blocked (very simplified example: Word not responding). Multi-threading allows us to execute multiple code paths at the same time. "Same time" is a bit of a lie, since only one thread can actually execute at a time in a core, but the CPU gives some small chunk of time to each thread, so it appears as if all these threads are executing at the same time. A good example here is the spell checker in Word.

If you have multiple cores, the only difference is that in an N-Core CPU you can have N threads executing at the same time. To simplify a lot, it doesn't matter what process the threads belong to. To simply even further, you'd expect a N times performance increase. :-D

siz