source: http://stackoverflow.com/questions/9557631/how-do-programs-communicate-with-each-other
reference: https://en.wikipedia.org/wiki/Inter-process_communication
question: How do procceses communicate with each other? Using everything I've learnt to fo with programming so far, I'm unable to explain how sockets, file systems and other things to do with sending messages between programs work.
reply: I am not sure that threads always lower the number of context switches (they certainly don't, if you have much less cores than threads). Threads are scheduled like processes are on Linux (the kernel scheduler is scheduling tasks, either threads of multi-threaded processes or single-threaded processes). – Basile Starynkevitch Mar 4 '12 at 19:14
reference: https://en.wikipedia.org/wiki/Inter-process_communication
question: How do procceses communicate with each other? Using everything I've learnt to fo with programming so far, I'm unable to explain how sockets, file systems and other things to do with sending messages between programs work.
Btw I use a Linux based OS if your going to add anything OS specific. Thanks in advance. The question's been bugging me for ages. I'm also guessing the kernel has something to do with it.
answer: 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.
댓글
댓글 쓰기