Summary:process & Tread

Source: Internet
Author: User

Refer to http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process/

A process is an executing instance of A application. What's 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 this process.

It's important to note the 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 of each one are used to accomplish. threads is used for small tasks, whereas processes is used for more ' Heavyweigh T ' 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 co Mmunication between threads. Communication between processes–also known as IPC, or inter-process communication–is quite difficult and Reso Urce-intensive.

Multithreading

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



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

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 it's very easy-to-share data amongst threads, but it's also easy for the threads-step on each other, WH Ich can leads to bad things.

multithreaded programs must is carefully programmed to prevent those bad things from happening. Sections of code, modify data structures shared by multiple threads is called critical Sections. When a critical sections is running in one thread it's extremely important that no other thread being allowed into that critic Al section. This is called synchronization, which we wont get to any further over here. But, the 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 are very low relative to processes.

Here's a summary of the differences between threads and processes:

1. Threads is easier to create than processes since they don ' t require a separate address space.2. Multithreading requires careful programming since threads share data strucures that should only being modified by one Threada T a time.  Unlike threads, processes don ' t share the same address space.3.  Threads is considered lightweight because they use far less resources than processes.4.  Processes is independent of each of the other.  Threads, since they share the same address space is interdependent, so caution must is taken so this different Threads do N ' t step on each of the other.  This is really another the stating #2 above.5.  A process can consist of multiple threads.

My summary:

1. Threads is used for small ' light weight ' tasks, whereas processes is used for more ' heavyweight ' tasks.

2. Threads within the same process share the same address space, allow reading writing from the same data structure, where As processes does not. They use IPC, and is very resource intensive.

Thread how to securely access shared memory->mutex,semaphone/mutex differences

Mutex:

is a key to a toilet. One person can has the key-occupy the Toilet-at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: "mutexes is typically used to serialise access to a sections of re-entrant code thatcannot is executed concurrently by more than one thread. A Mutex object is only allows one thread to A controlled section, forcing other threads which attempt to gain access to that sections to wait until the first thread have exited from that Section.

(A mutex is really a semaphore with value 1.)

Semaphore:

is the number of free identical toilet keys. Example, say we have a four-toilets with identical locks and keys. The semaphore count-the count of Keys-is set to 4 at beginning (all four toilets was free) and the count value is de Cremented as people is coming in. If all toilets is full, ie. There is no free keys left, the semaphore count is 0. Now, when Eq. One person leaves the toilet, semaphore are increased to 1 (one free key), and given to the next person in th E queue.

Officially: "a semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they has finished using the Resource (incrementing the semaphore). "

Summary:process & Tread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.