The difference between process and thread

I was asked during an interview. First they asked how to ensure synchronization between threads. Then they asked how to ensure synchronization between the process, because I told them that the variable inside each process cannot be transferred to another process, so they asked me to explain how the two processes can interact with each other and how to ensure synchronization between them and where declare a shared variable? Now the interview is over, but I want to know the answer, can someone explain to me? Thanks.

+6
source share
10 answers

I think the interviewer (s) cannot use the correct terminology. The process works in its own space and is mentioned in separate answers, you should use OS-specific mechanisms for communication between the process. This is called IPC for interprocess communication.

Using sockets is common practice, but can be extremely inefficient depending on your application. But if you work with pure Java, this may be the only possibility, since sockets are supported everywhere.

Shared memory is another method, but it depends on the OS and requires OS-specific calls. You will need to use something like JNI for a Java application to access shared memory services. Access to shared memory is not synchronized, so you may have to use semaphores to synchronize access to multiple processes.

Unix-like systems provide several IP channels, and one of them depends on the nature of your application. Shared memory may be a limited resource, so this may not be the best method. Googling on these topics provides numerous hits containing useful information on technical details.

+5
source

To communicate between the two processes, I suppose you can use ServerSocket and Socket to control the synchronization of the process. You bind to a specific port (acquire a lock), and if the process is already connected, 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 really the best way to do this, but I think it's worth considering.

+2
source

A process is a collection of virtual memory, code, data, and system resources. A thread is code that must be executed sequentially as part of a process. A processor runs threads, not processes, so every application has at least one process, and a process always has at least one thread of execution, known as the main thread. A process can have multiple threads in addition to the primary thread. Prior to deploying multiple threads, applications were created to run in a single thread of execution.

When a thread starts to execute, it continues until it is killed or until it is interrupted by a thread with a higher priority (through the action of a user or kernel thread scheduler). Each thread can run separate sections of code, or multiple threads can execute the same section of code. Themes that execute the same block of code support separate stacks. Each thread in the process shares a process that processes global variables and resources.

+2
source

Synchronization is only for threads; it does not work for Java processes. They do not have any utility working in different processes, since the processes do not share any state that needs to be synchronized. A variable in one process will not have the same data as a variable in another process.

+1
source

From a system point of view, a flow is determined by its “ state ” and “instruction pointer” "

The instruction pointer ( eip ) contains the address of the next command to be executed.

The state thread can be: registers (eax, ebx, etc.), signals , open files , code , stack , data controlled by this stream (variables, arrays, etc.), as well as a bunch .

Process is a group of threads that share part of their state ": it could be a code , > data strong>, a bunch . I hope I will answer your question;)

EDIT: Processes can communicate via IPC (Inter process communication). There are 3 mechanisms: shared memory , message queue . Synchronization between processes can be done using Semaphores.

Thread synchronization can be done using mutexes ( pthread_mutex_lock, pthread_mutex_unlock, etc. )

+1
source

Check the Terracotta Cluster or Terracotta DSO Clustering documentation for how this problem can be solved (bytecode manipulation, support for semantics of the Java language specification on the put field / getfield level, etc.)

0
source

The simplest answer is that a process means an executable program, and a program is nothing more than a collection of functions. where a thread is part of a process because all threads are functions. otherwise we can say that a process can have several threads. always the OS allocates memory for the process and this memory is allocated among the threads of this process. OS does not allocate memory for threads.

0
source

In one sentence, processes are created more independently than threads.
Their main differences can be described at the memory level. The various processes are not shared: from register, stock to heap memory, which makes them safe on their own roads. However, threads are usually designed to share shared heap memory, which provides a more closely related task for a multiprocessor computing task. Creating a more efficient way to use computing resources.

eg. If I am calculating three processes, I have to let them finish the job and wait for the results at the system level, on average, the registers and the memory stack are always accepted. However, if I do this with 3 threads, then, fortunately, thread 2 finishes its work earlier, because the result that it calculated has already been saved to the heap’s shared memory pool, we can just kill it without waiting for others to receive their results, and these freed resources of registers and storage memory can be used for other purposes.

0
source

process:

  • A process is nothing but an executable program.
  • Each process has its own memory address space.
  • The process is used for heavy tasks, that is, applications are mostly executed.
  • The cost of communication between the process is high.
  • The transition from one process to another takes some time to save and load registers, memory cards, etc.
  • A process is an operating system approach.

Topics:

  • A thread is a lightweight subprocess.
  • A topic shares the same address space.
  • The cost of communication between the stream is low.

Note. Each thread requires at least one process.

-1
source

I believe that processes can interact through a third party: a file or a database ...

-2
source

Source: https://habr.com/ru/post/886419/


All Articles