How Do Programs Communicate with Each Other

How do programs communicate with each other?

In case of most IPC (InterProcess Communication) mechanisms, the general answer to your question is this: process A calls the kernel passing a pointer to a buffer with data to be transferred to process B, process B calls the kernel (or is already blocked on a call to the kernel) passing a pointer to a buffer to be filled with data from process A.

This general description is true for sockets, pipes, System V message queues, ordinary files etc. As you can see the cost of communication is high since it involves at least one context switch.

Signals constitute an asynchronous IPC mechanism in which one process can send a simple notification to another process triggering a handler registered by the second process (alternatively doing nothing, stopping or killing that process if no handler is registered, depending on the signal).

For transferring large amount of data one can use System V shared memory in which case two processes can access the same portion of main memory. Note that even in this case, one needs to employ a synchronization mechanism, like System V semaphores, which result in context switches as well.

This is why when processes need to communicate often, it is better to make them threads in a single process.

C++ How to make two programs communicate?

there are many solutions with many pros and cons. you will have to do some reading on each to work out which is best for you. no solution is best for everyone

here is a good starting place http://msdn.microsoft.com/en-us/library/aa365574(v=vs.85).aspx

What is the best way for two programs on the same machine to communicate with each other

My personal preference for this, given that you're using C++ and C# both, and it's on the same system, would be to use Pipes.

They work very well from native code (C++) as well as from C# via NamedPipeClientStream and NamedPipeServerStream.

However, there are other options for Interprocess Communication, any of which would work.

what is it called when a program talks with other programs or other computers

I expect that searching for server/client examples and socket programming in general will get you headed in the right direction.

For example, a google search for socket client server c will get you to An Introduction to Socket Programming.

A similar search for socket client server java gets you to a Lesson on Socket Communications

How can two programs talk to each other in Java?

What you're describing sounds like Premature Optimisation. If you're writing something other than a toy application, it's important to be confident that your optimisations are actually addressing a real problem. Is your program running slowly? If so, have you run it through a profiler or otherwise identified where the poor performance is happening?

If you have identified that what you want to do will address your performance issue, I suggest you look at running the components concurrently in different threads, not different processes. Then your components can avoid blocking each other, you will be able to take advantage of multi-core processors and you do not take on the complexity and performance overhead of inter-process communication over network sockets and the like.

how do programs communicate - micro kernel monolithic kernel exo kernel

System calls are the main way of communicating with both monolithic and micro kernels. Using system calls interface kernel may provide numerous IPC methods (such as signals, shared memory, message passing). Due to the nature of microkernels IPC is usually the most important part of such kernel and is used to allow programs to communicate with the servers.

In other words, there are system calls that allow programs to make requests to kernel. Kernel provides some IPC methods that allow programs to communicate with each other including servers in microkernel-based operating systems.

Communication between Different programs in Different Computers (C# or Python)

The most complete protocol for what you want is gRPC. There is a learning curve but worth it in my opinion. https://github.com/grpc/grpc

How to have two programs communicate with one another

So one of the few ways I know of, that transfers data from a thread to the main GUI thread of an application, is to place the data in a python Queue. This Queue is read by a QThread (a Qt threading implementation that supports Qt signals and slots). The QThread makes a blocking call to queue.get(). When data is placed in the Queue by your handle() method, the QThread unblocks, reads the data out of the queue, and emits a thread-safe signal to the GUI thread. As a demonstration, I added the data into the QTextEdit.

So basically you need an intermediary between a python thread and the Qt GUI which generally interacts via signals/slots. The QThread performs this task, linking the thread-safe Queue object, with the thread-safe qt signal emission.

This effectively follows a similar answer I gave here, but here you have a socket instead of a custom thread putting the data in the queue.

You might also be interested in this SO post, which explains why some of the lines of code I have used to make the QThread and connect the signals, etc, are written in the order they are!

P.S. I added a line to shutdown the socket server when the app window is closed (the socket server used to keep running in the background)

Server Window code

import SocketServer
import concurrent.futures
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from Queue import Queue

HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)

# create a global queue object that both the handle() method and the QThread (see later in the code) can access
queue = Queue()

class MyRequestHandler(SocketServer.StreamRequestHandler):
def handle(self):
print('...connected from:', self.client_address)
data = self.rfile.readline().strip()
print('Data from client %s' % data)
queue.put(data)

class ServerWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setGeometry(1500, 100, 500, 500)

self._control = QtGui.QWidget()
self.setCentralWidget(self._control)

l = QtGui.QVBoxLayout(self._control)
self.t = QtGui.QTextEdit()
l.addWidget(self.t)

self.executor = futures.ThreadPoolExecutor(max_workers=1)
self.startServerThread()

self.show()

@QtCore.pyqtSlot(str)
def receive_data(self, data):
self.t.moveCursor(QtGui.QTextCursor.End)
self.t.insertPlainText( data )

def startServerThread(self):
self.executor.submit(self.startServer)

# How to get information from the thread while it is still running?

def startServer(self):
print('starting server')
self.tcpServ = SocketServer.TCPServer(ADDR, MyRequestHandler)
print('waiting for connection...')
self.tcpServ.serve_forever()

# How to get information from the client (custom request handler)
# back to the GUI in a thread safe manner?

# This class runs in a QThread and listens on the end of a queue and emits a signal to the GUI
class MyReceiver(QtCore.QObject):
mysignal = QtCore.pyqtSignal(str)

def __init__(self,queue,*args,**kwargs):
QtCore.QObject.__init__(self,*args,**kwargs)
self.queue = queue

@QtCore.pyqtSlot()
def run(self):
while True:
text = self.queue.get()
self.mysignal.emit(text)

def launch():
app = QtGui.QApplication(sys.argv)

ex = ServerWindow()

# Create thread that will listen on the other end of the queue, and send the text to the textedit in our application
thread = QtCore.QThread()
my_receiver = MyReceiver(queue)
my_receiver.mysignal.connect(ex.receive_data)
my_receiver.moveToThread(thread)
thread.started.connect(my_receiver.run)
thread.start()

ret_code = app.exec_()
ex.tcpServ.shutdown()
sys.exit(ret_code)

if __name__ == '__main__':
launch()


Related Topics



Leave a reply



Submit