简体   繁体   English

进程和线程之间的区别

[英]Difference between process and thread

I was asked a question during interview today. 我今天在采访中被问了一个问题。 First they asked how to provide Synchronization between thread. 首先,他们询问如何在线程之间提供同步。 Then they asked how to provide Synchronization between process, because I told them, the variable inside each process can not be shared with other process, so they asked me to explain how two process can communicate with each other and how to provide Synchronization between them, and where to declare the shared variable? 然后他们询问如何在进程之间提供同步,因为我告诉他们,每个进程内的变量都不能与其他进程共享,所以他们让我解释两个进程如何相互通信以及如何在它们之间提供同步,以及在哪里声明共享变量? Now the interview finished, but I want to know the answer, can anyone explain me?Thank you. 现在面试结束了,但我想知道答案,有人能解释一下吗?谢谢。

I think the interviewer(s) may not be using the proper terminology. 我认为面试官可能没有使用正确的术语。 A process runs in its own space, and has been mentioned in separate answers, you have to use OS-specific mechanisms to communicate between process. 进程在自己的空间中运行,并且在单独的答案中提到过,您必须使用特定于操作系统的机制来在进程之间进行通信。 This is called IPC for Inter-Process Communication. 这称为进程间通信的IPC。

Using sockets is a common practice, but can be grossly inefficient, depending on your application. 使用套接字是一种常见的做法,但根据您的应用程序而言可能非常低效。 But if working with pure Java, this may be the only option since sockets are universally supported. 但是如果使用纯Java,这可能是唯一的选择,因为套接字是普遍支持的。

Shared memory is another technique, but that is OS-specific and requires OS-specific calls. 共享内存是另一种技术,但这是特定于操作系统的,需要特定于操作系统的调用。 You would have to use something like JNI for a Java application to access shared memory services. 您必须使用类似JNI的东西来访问Java应用程序以访问共享内存服务。 Shared memory access is not synchronized, so you will likely have to use semaphors to synchronize access among multiple processes. 共享内存访问不同步,因此您可能必须使用信号量来同步多个进程之间的访问。

Unix-like systems provide multiple IPC mechansims, and which one to use depends on the nature of your application. 类Unix系统提供多个IPC机制,使用哪个机制取决于应用程序的性质。 Shared memory can be a limited resource, so it may not be the best method. 共享内存可能是有限的资源,因此它可能不是最好的方法。 Googling on this topics provides numerous hits providing useful information on the technical details. 谷歌搜索这个主题提供了大量的点击,提供有关技术细节的有用信息。

A process is a collection of virtual memory space, code, data, and system resources. 进程是虚拟内存空间,代码,数据和系统资源的集合。 A thread is code that is to be serially executed within a process. 线程是要在进程内串行执行的代码。 A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. 处理器执行线程而不是进程,因此每个应用程序至少有一个进程,并且进程始终至少有一个执行线程,称为主线程。 A process can have multiple threads in addition to the primary thread. 除主线程外,进程可以有多个线程。 Prior to the introduction of multiple threads of execution, applications were all designed to run on a single thread of execution. 在引入多个执行线程之前,应用程序都设计为在单个执行线程上运行。

When a thread begins to execute, it continues until it is killed or until it is interrupted by a thread with higher priority (by a user action or the kernel's thread scheduler). 当一个线程开始执行时,它会继续执行,直到它被终止或被一个具有更高优先级的线程(通过用户操作或内核的线程调度程序)中断。 Each thread can run separate sections of code, or multiple threads can execute the same section of code. 每个线程可以运行单独的代码段,或者多个线程可以执行相同的代码段。 Threads executing the same block of code maintain separate stacks. 执行相同代码块的线程维护单独的堆栈。 Each thread in a process shares that process's global variables and resources. 进程中的每个线程共享该进程的全局变量和资源。

To communicate between two processes I suppose you can use a ServerSocket and Socket to manage process synchronization. 为了在两个进程之间进行通信,我认为您可以使用ServerSocket和Socket来管理进程同步。 You would bind to a specific port (acquire lock) and if a process already is bound you can connect to the socket (block) and wait until the server socket is closed. 您将绑定到特定端口(获取锁定),如果已绑定进程,则可以连接到套接字(块)并等待服务器套接字关闭。

private static int KNOWN_PORT = 11000;//arbitrary valid port
private ServerSocket socket;
public void acquireProcessLock(){
   socket = new ServetSocket(KNOWN_PORT);
   INetAddress localhostInetAddres = ...
   try{
      socket.bind(localhostInetAddres );
   }catch(IOException failed){
      try{
       Socket socket = new Socket(localhostInetAddres ,KNOWN_PORT);
       socket.getInputStream().read();//block
      }catch(IOException ex){ acquireProcessLock(); } //other process invoked releaseProcessLock()
   }
}
public void releaseProcessLock(){
  socket.close();
}

Not sure if this is the actual best means of doing it but I think its worth considering. 不确定这是否是实际的最佳方法,但我认为值得考虑。

Synchronization is for threads only it wont work for processes in Java. 同步仅适用于线程,它不适用于Java中的进程。 There is no utility in them working across processes, since the processes do not share any state that would need to be synchronized. 它们之间没有用于跨进程的实用程序,因为进程不共享任何需要同步的状态。 A variable in one process will not have the same data as a variable in the other process 一个进程中的变量与另一个进程中的变量不具有相同的数据

From a system point of view, a thread is defined by his " state " and the " instruction pointer ". 从系统的角度来看,线程由他的“ 状态 ”和“ 指令指针 ”定义。

The instruction pointer ( eip ) contains the address of the next instruction to be executed. 指令指针( eip )包含要执行的下一条指令的地址。

A thread " state " can be : the registers (eax, ebx,etc), the signals , the open files , the code , the stack , the data managed by this thread (variables, arrays, etc) and also the heap . 线程“ 状态 ”可以是: 寄存器 (eax,ebx等), 信号打开文件代码堆栈 ,由此线程管理的数据 (变量,数组等)以及

A process is a group of threads that share a part of their " state ": it might be the code , the data , the heap . 进程是一组共享其“ 状态 ”的一部分的线程:它可能是代码数据 Hope i answer your question ;) 希望我回答你的问题;)

EDIT: The processes can communicate via the IPCs (Inter process communications). 编辑:进程可以通过IPC进行通信(进程间通信)。 There are 3 mecanisms : shared memory , message queue . 共有3种机制: 共享内存消息队列 Synchronization between processes can me made with the Semaphors 我可以使用Semaphors进行进程之间的同步

Threads synchronization can me made with mutexes ( pthread_mutex_lock, pthread_mutex_unlock, etc ) 可以使用互斥锁进行线程同步( pthread_mutex_lock,pthread_mutex_unlock等

the most simplest answer is process means a program under execution and a program is nothing but collection of functions. 最简单的答案是进程意味着正在执行的程序,程序只是函数集合。 where thread is the part of the proccess because all the threads are functions. 其中thread是proccess的一部分,因为所有的线程都是函数。 in other way we can say that a process may have multiple threads. 换句话说,我们可以说一个进程可能有多个线程。 always OS allocates the memory for a process and that memory is disributed among the threads of that process.OS does not allocates memory for threads. OS总是为进程分配内存,并且该进程的线程之间会分配内存.OS不为线程分配内存。

In one sentence processes are designed more independently than threads are. 在一句话中,流程的设计比线程更独立。
Their major differences can be described at memory level. 他们的主要差异可以在记忆层面描述。 Different processes share nothing among each other, from register, stock memory to heap memory, which make them safe on their own tracks. 不同的进程彼此之间没有任何共享,从寄存器,库存到堆内存,这使它们在自己的轨道上安全。 However, normally threads are designed to share a common heap memory, which provides a more closely connected way for multiple processes computing task. 但是,通常线程被设计为共享公共堆内存,这为多个进程计算任务提供了更紧密连接的方式。 Creating a more efficient way to take up computation resources. 创建一种更有效的方式来占用计算资源。

Eg If I compute with 3 processes, I have to let them each finish their jobs and wait for their results in a system level, at the mean time, registers and stack memory are always taken up. 例如,如果我使用3个进程进行计算,我必须让它们各自完成它们的工作并在系统级别等待它们的结果,同时,总是占用寄存器和堆栈存储器。 However, if I do it with 3 threads, then if thread 2 luckily finish its job earlier, because the result it computed had already been stored to the common heap memory pool, we can simply kill it without waiting for others to deliver their results, and this released resources of registers and stock memory can be used on other purposes. 但是,如果我使用3个线程,那么如果线程2幸运地完成了它的工作,因为它计算的结果已经存储到公共堆内存池,我们可以直接杀死它而无需等待其他人传递结果,这个已发布的寄存器和库存内存资源可用于其他目的。

检查Terracotta Cluster或Terracotta的DSO Clustering文档,了解如何解决此问题(字节码操作,维护putfield / getfield级别的Java语言规范的语义等)

Process: 处理:

  • A process is nothing but a program under execution. 一个过程只不过是一个正在执行的程序。
  • Each process have its own memory address space. 每个进程都有自己的内存地址空间。
  • Process are used for heavyweight tasks ie is basically execution of applications. 进程用于重量级任务,即基本上是应用程序的执行。
  • Cost of communication between process is high. 流程之间的沟通成本很高。
  • Switching from one process to another require some time for saving and loading registers, memory maps etc. 从一个进程切换到另一个进程需要一些时间来保存和加载寄存器,内存映射等。
  • Process is operating system approach. 流程是操作系统方法。

Threads: 主题:

  • A thread is light weight sub-process. 线程是轻量级子过程。
  • Thread share the same address space. 线程共享相同的地址空间。
  • Cost of communication between the thread is low. 线程之间的通信成本很低。

Note: At least one process is required for each thread. 注意:每个线程至少需要一个进程。

我想这些进程可以通过第三方进行通信:文件或数据库......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM