简体   繁体   中英

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.

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.

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. 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. 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. 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. 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.

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 .

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). There are 3 mecanisms : shared memory , message queue . Synchronization between processes can me made with the Semaphors

Threads synchronization can me made with mutexes ( pthread_mutex_lock, pthread_mutex_unlock, etc )

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. 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.

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. 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.

检查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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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