Rectangle 27 0

java Difference between process and thread?


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();
}

@Piotr thank you for that. I put it together a bit quickly but I am sure there would be OS specific issues to go over. @ratzip I am assuming you mean two separate JVMs running on two separate processes. If thats the case, they would share the same memory (obviously virtual) but neither has pointers to one anothers memory. So you cannot synchronize on objects under separate processes. Were they looking for something specific when they said you cannot use Sockets and IO?

Beware, however, that OS-es sometimes forbid second bind to the same port from different process or user.

I also answered them using Socket to communicate with each other. But they do not satisfied.And they repeat the question again and again, so any other thought?

Not sure if this is the actual best means of doing it but I think its worth considering.

They asked how two processes can communicate with each other and be synchronized. This is the question. I through maybe two processes can be Synchronized the way thread does. So I told them one process wait for another put value to a common variable, and then it can fetch it.so they asked where to declare the common variable since the variable can not be shared by processes?

They may have been looking for something really specific, but generally speaking. Two process cannot synchronize on a shared piece of memory you would see in java code like synchronize(myObject){ ... }. Two processes don't know about eachothers heap and in turn reference to virtual memory.

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.

Note
Rectangle 27 0

java Difference between process and thread?


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

yes, I understand your answer, the processes do not share any state,but I wonder if two processes will access the same shared memory? in this case, shall we consider Synchronization?

Note
Rectangle 27 0

java Difference between process and thread?


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.

Note
Rectangle 27 0

java Difference between process and thread?


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.

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.

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.

Note