/* ------------------------ main.c ------------------------ */ # include # include # include # include # include # include "sign.h" # include "count.h" int main() { int pid, ppid, status, child_nr; if ((install_lm_sig(SIGUSR1) == SIG_ERR) || (install_lm_sig(SIGUSR2) == SIG_ERR)) perror("install_sig"), exit(1); for(child_nr = 1; child_nr <= 2; child_nr++) { switch(pid = fork()) { case -1: perror("fork"); exit(1); case 0 : { long mypid; int i, sleeptime = 1; if (install_ct_sig(SIGUSR2) == SIG_ERR) { perror("install_ct_sig"); exit(1); } ppid = getppid(); mypid = getpid(); for(i = 1; i <= 5; i++) { int tries = 10; while((sleep(sleeptime) <= 0 ) && (tries > 0)) { fprintf(stderr, "P() : %ld\n", mypid); if (kill(ppid,SIGUSR1) < 0) { perror("kill"); exit(1); } tries--; } if (tries <= 0) { fprintf(stderr, "%ld: couldn't get the lock!\n", mypid); exit(2); } /* have the lock: */ count(mypid, 1); fprintf(stderr, "V() : %ld\n", mypid); if (kill(ppid,SIGUSR2) < 0 ) { perror("kill"); exit(1); } } /*for*/ exit(0); } default:; } } wait(&status); wait(&status); sleep(1); printf("Parent: going to exit!\n"); exit(0); }