#include #include #define PARENT_FLAG1 "PFLAG1" #define PARENT_FLAG2 "PFLAG2" #define CHILD_FLAG1 "CFLAG1" #define CHILD_FLAG2 "CFLAG2" void wait_flag(char *pname) { FILE *pfile = 0x0; while(!pfile) pfile = fopen(pname,"r"); fclose(pfile); } void send_flag(char *pname) { FILE *pfile = fopen(pname,"w"); if(pfile) fclose(pfile); } void hide_flag(char *pname) { remove(pname); /* actually deletes the flag file! */ } int main(int argc, char *argv[]) { pid_t child; if((child=fork())==0) { /** child stuff - wait for parent */ printf("[Child-%d] Waiting for parent... ",getpid()); wait_flag(PARENT_FLAG1); /* wait for parent signal */ printf("YES!\n"); /** child stuff - send ack to parent */ printf("[Child-%d] Sending acknowledgement to parent.\n",getpid()); send_flag(CHILD_FLAG1); /** child stuff - wait for parent */ printf("[Child-%d] Waiting for parent... ",getpid()); wait_flag(PARENT_FLAG2); /* wait for parent signal */ printf("YES!\n"); /** child stuff - send ack to parent */ printf("[Child-%d] Sending acknowledgement to parent.\n",getpid()); send_flag(CHILD_FLAG2); } else { /** parent stuff - send 'command' to child */ printf("[Parent-%d] Sending 'command' to child.\n",getpid()); send_flag(PARENT_FLAG1); /** parent stuff - wait for child */ printf("[Parent-%d] Waiting for child response... ",getpid()); wait_flag(CHILD_FLAG1); /* wait for child signal */ printf("YES!\n"); /** parent stuff - cleanup! */ hide_flag(PARENT_FLAG1); hide_flag(CHILD_FLAG1); /** should be done by child? */ /** parent stuff - send 'command' to child */ printf("[Parent-%d] Sending 'command' to child.\n",getpid()); send_flag(PARENT_FLAG2); /** parent stuff - wait for child */ printf("[Parent-%d] Waiting for child response... ",getpid()); wait_flag(CHILD_FLAG2); /* wait for child signal */ printf("YES!\n"); /** parent stuff - cleanup! */ hide_flag(PARENT_FLAG2); hide_flag(CHILD_FLAG2); /** should be done by child? */ } printf("[%d] Done\n",getpid()); return 0; }