#include #include #include #include #include #include #define N_CHILD 4 #define N_ROUNDS 8 /* * implementing a rendez-vous. * a rendez-vous synchronizes threads proceeding at their own pace * at a barriers. once all threads arrive at the barrier (the rendez-vous) * they can continue into their next round of activity. * * because signals do not accumulate, the protocol has two stages. the * first stage the parent engages the child threads one by one in a * cleared/acknowledged pair of HUP signals. Then the parent broadcasts * an INT signal. * * the first stage: when the child is ready, it waits on a HUP from the * parent before responding to the parent with a HUP signal. this signal * declares to the parent that this child is ready. the child then * approaches the barrier. * * the second stage: the parent has collected from all children the HUP * signals that they are ready, the parent sends the INT signal releasing * the children to cross the barrier. * * author: bjr * date: sep 2018 * update: * */ int g_count_hups = 0 ; int g_continues = 0 ; int g_cleared = 0 ; int g_acknowledged = 0 ; void parent_sighandler(int sig) { g_acknowledged = 1 ; return ; } void child_sighandler(int sig) { g_cleared = 1 ; return ; } void child_int_sighandler(int sig) { g_continues = 1 ; return ; } void do_child(int child_id) { pid_t ppid = getppid() ; int rounds = 0 ; signal(SIGHUP,child_sighandler) ; signal(SIGINT,child_int_sighandler) ; while (rounds