Are function callback and interprocess communication are same?
Asked Answered
W

3

7

Few days back, in an interview one question was asked to me as,

Are  function callback in C and interprocess communication are same?

I was bit neutral in that question.Because I was in a dilemma. In IPC we communicate between process to process at the end if we shall see process is nothing but a function. Here one function calls other function. In function callback we use function pointer again that is one function calls other function with address. So just wanted to know though they are not completely same, so what the difference?

Wiltshire answered 15/5, 2012 at 7:56 Comment(0)
T
13

No, not really.

Function callback is passing the address (or some other identifier) of a function to another function so that it can call back for some reason.

The classic example is the C standard library qsort function. You pass in a pointer to the array (and its sizes), along with a comparison function. The qsort function then uses that comparison function (the callback) to decide which of any two elements in the list is greater so that it can arrange them in order.

IPC, on the other hand, is a means for processes to communicate with each other, such as shared memory, pipes, semaphores and so on.

Some IPC mechanisms may use callbacks but it's by no means necessary. For example, sockets don't use callbacks, they simply allow a user to call read and write.

Similarly with shared memory, you attach to the memory blocks and simply access them much the same as "regular" memory, it's just that the effects are felt across all processes attached to that memory.

One that does sort of use callbacks is ONC RPC (or Sun RPC). It runs a server which awaits client calls over the network and passes them to a pre-configured client function. More detail can be found here.

But, even then, I'd be hesitant to call it a callback since it's really configured at compile-time. "Proper" callbacks tend to be done at run-time.

Trioecious answered 15/5, 2012 at 7:58 Comment(3)
+1 couldn't beat this (version of this) answer in simplicity and comprehensivenessRaddie
@paxdiablo: Function callback:-Some reason means getting some data...so the same we are getting from IPC by using socket or pipes and allWiltshire
"Some IPC mechanisms may use callbacks but it's by no means necessary." - It would be good if this can be elaborated.Ironsmith
M
8

A function callback means caller and callee are in the same thread. IPC is in different processes. a process is comprised of one or more threads. Ergo, callbacks are not IPC.

Merrillmerrily answered 15/5, 2012 at 8:1 Comment(0)
N
0

Function callbacks and inter-process communication (IPC) are related concepts but serve different purposes and are implemented in different contexts.

Function Callback: A function callback refers to a programming technique where a function is passed as an argument to another function to be called later as a callback. The main function can then call the callback function to perform a specific action or task at a certain point in its execution. Function callbacks are often used for event handling, asynchronous programming, and customizing behavior.

Inter-Process Communication (IPC): Inter-Process Communication refers to mechanisms that allow different processes in an operating system to communicate with each other and share data. IPC enables processes to exchange information, synchronize their activities, and cooperate to achieve tasks. IPC can take various forms, such as shared memory, message passing, sockets, pipes, and signals, among others.

Nikko answered 13/6 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.