I haved a problem to complete my example when asked this question.
I searched for ways of implementing IPC in Google.
I can't decide which way is best for write my program.
I tried lots of implementations and have lots of complications with them.
I want to have:
1. parent process to manage child process - OK ( template )
2. parent and children must have implemented callback for new message signal
3. one process do not know message size from other process ( char * )
My code:
header.h:
#ifndef MESSAGES_H
#define MESSAGES_H
#include <stdio.h>
#include <stdlib.h>
// need here: some includes and definitions
inline char * read_message( /* need here: some params */ ) {
// need here: read message function
}
inline char * send_message( /* need here: some params */ ) {
// need here: send message function
}
#endif
parent.c:
#include "header.h"
// parent specyfic includes and definitions
void on_message( /* need here: some parameters */ ) {
char *message = read_message( /* need here: some other parameters */ );
// do something with / if message etc.
}
int runChild(key) {
int pid = fork();
if (pid == 0) {
execl("./child", "./child", /* params here */, null);
}else{
return pid;
}
}
int main(int argc, char *argv[]) {
// need here: prepare IPC
// need here: on new message event call function "on_message"
int childPid = runChild(key);
// loop - for example: gtk_main()
// need here: close childs
}
child.c
#include "header.h"
// child specyfic includes and definitions
void on_message( /* need here: some parameters */ ) {
char *message = read_message( /* need here: some other parameters */ );
// do something with / if message etc.
}
int main(int argc, char *argv[]) {
// need here: prepare IPC
// need here: on new message event call function "on_message"
int pid = getpid();
int parentPid = getppid();
printf("Child with pid %d is ready for messages from parent with pid: %d", pid, parentPid);
// event loop - for example: gtk_main()
}
Which IPC way is better in that example program template ( safe and speed ) ? Can you share a really simple example that matches the above template ?