Hello,
I am playing with the linx FIFO and mknod. As simple example as opening two programs "sender" and "receiver", and a FIFO between them, it seems that they can exchange buffers. However the interesting thing is, can they exchange function calls. Let`s say we have that struct:
Code:
struct msg {
char message[64];
int count;
void (*pfoo)(void);
};
and we assume that sender and receiver know about that struct, is it possible to make a something like that:
sender.c
Code:
static void _p(void) { printf("AAAAAAAAAAAAAAAA"); }
int main(int argc, char *argv[])
{
(void) argc;
(void) argv;
struct msg buff;
strcpy(msg.message, "sender message");
buff.count = 1;
msg.pfoo = _p;
int num = 0, fd = 0;
mknod(FIFO_NAME, S_IFIFO|0666, 0);
fd = open(FIFO_NAME, O_WRONLY);
while (1) {
sleep(2);
if ((num = write(fd,
(struct test_call*)&msg,
sizeof(struct msg))) == -1) {
perror("write");
} else {
printf("sending %d data\n", num);
}
}
return 0;
}
and the receiver.c
Code:
int main(int argc, char *argv[])
{
int num, fd;
mknod(FIFO_NAME, S_IFIFO|0666, 0);
printf("waiting for writers\n");
fd = open(FIFO_NAME, O_RDONLY);
printf("Got a writer!\n");
char buff[sizeof(struct msg)] = {0};
do {
if ((num = read(fd, buff, sizeof(struct msg)))==-1) {
perror("read");
} else {
struct msg *m = (struct msg*)&ubuff.tc;
printf("[%s][%d]\n", m->msg, m->count);
if (tc->pfoo) {
tc->pfoo();
}
}
} while (num > 0);
return 0;
}
Is that possible, or I have to compile in a separate file msg and give it`s own translation unit with the function ptr to a specific callback?