Processes vs Threads

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.

Subscribe to our newsletter for more free interview questions.

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.

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

1. Threads are 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 be modified by one thread
at a time.  Unlike threads, processes don‘t share the same
address space.

3.  Threads are considered lightweight because they use far
less resources than processes.

4.  Processes are independent of each other.  Threads, since they
share the same address space are interdependent, so caution
must be taken so that different threads don‘t step on each other.
This is really another way of stating #2 above.

5.  A process can consist of multiple threads.
时间: 2024-08-04 14:20:56

Processes vs Threads的相关文章

Processes and Threads

http://www.cnblogs.com/xitang/archive/2011/09/24/2189460.html Processes and Threads 译者署名: 呆呆大虾 译者微博: http://weibo.com/popapa 版本:Android 3.2 r1 原文 http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html 快速查看 ·           默认情况下

【Android文档】Processes and Threads

大致翻译一下,记录笔记. 原文地址:Processes and Threads Processes and Threads 当一个app的组件(这里一般指四大组件Activity,Service等)启动时,如果此时系统没有其他组件正在运行,则android系统会为该app启动一个新的linux进程,而且该进程中只有一个线程.默认情况下,app中的所有组件,都运行在同个进程中的同个线程(称为主线程).如果一个app的组件启动时,该app中已经存在一个进程,则该组件将运行在同一个进程中,并且使用同一

Android Processes and Threads

Processes and Threads When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components

Java Processes and Threads 进程与线程

Processes and Threads In concurrent programming, there are two basic units of execution: processes and threads. In the Java programming language, concurrent programming is mostly concerned with threads. However, processes are also important. A comput

Windbg Processes and Threads(进程和线程)窗口的使用

在 WinDbg 中,进程和线程窗口中显示有关系统. 进程和线程正在调试的信息. 此窗口还可选择新的系统. 进程和线程处于活动状态. 如何打开进程和线程窗口 通过菜单View--->Processes and Threads 快捷键Alt+9 通过工具栏 使用进程和线程窗口 通过上面的方式打开的窗口如下: “进程和线程”窗口显示当前正在调试的所有进程的列表.进程中的线程出现在每个进程下.如果调试器附加到多个系统,则系统显示在树的顶层,进程从属于它们,线程从属于进程.每个系统列表都包含服务器名和协

Processes and Threads (转)

http://www.cnblogs.com/xitang/archive/2011/09/24/2189460.html 原文 http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html 快速查看 ·           默认情况下,每个应用程序运行在各自的进程中,应用程序中的所有组件也都运行在其中. ·           activity中所有运行缓慢的.阻塞的操作都应该运行在新建的线程

[原]Threads vs Processes in Linux 分析

Linux中thread (light-weighted process) 跟process在實作上幾乎一樣. 最大的差異來自於,thread 會分享 virtual memory address space. a. 從kernel角度看兩者沒差別,在user看來process是least shared而thread是most sharing. b. 從實作角度來比較: 創建user process的方法是有三個API: fork, vfork, clone創建user thread的方法主要是

ULK --- Chap3 Processes

The concept of a process is fundamental to any multiprogramming operating system. A process is usually defined as an instance of a program in execution; thus, if 16 users are running vi at once, there are 16 separate processes (although they can shar

2.App Components-Processes and Threads

1. Processes and Threads When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all compone