Já tomu nějak nerozumím. Jednoduše by to šlo třeba takhle (bez kontrol chyb):
#define ASZ(x) (sizeof (x) / sizeof (x)[0])
void proc(char id, sem_t *waitsem[], sem_t *postsem[])
{
int i;
for (i = 0; waitsem[i]; i++) sem_wait(waitsem[i]);
for (i = 0; i < 5; i++) {
printf("%c", id); fflush(stdout);
sleep(1);
}
for (i = 0; postsem[i]; i++) sem_post(postsem[i]);
}
pid_t start(char id, char *wait, char *post)
{
pid_t pid;
if ((pid = fork()) == 0) {
int i;
char *tok, *saveptr;
sem_t *wsem[5] = {0}, *psem[5] = {0};
wait = strdup(wait); post = strdup(post);
for (i = 0, tok = strtok_r(wait, ",", &saveptr); tok; tok = strt
wsem[i] = sem_open(tok, 0);
for (i = 0, tok = strtok_r(post, ",", &saveptr); tok; tok = strt
psem[i] = sem_open(tok, 0);
proc(id, wsem, psem);
for (i = 0; wsem[i]; i++) sem_close(wsem[i]);
for (i = 0; psem[i]; i++) sem_close(psem[i]);
exit(0);
}
return pid;
}
int main(int argc, char *argv[])
{
struct { char *name; sem_t *sem; } semlist[] = {
{ "/xx01", NULL },
{ "/xx12", NULL },
{ "/xx23", NULL },
{ "/xx35", NULL },
{ "/xx14", NULL },
{ "/xx45", NULL }
};
struct { pid_t pid; char *wait, *post; } proclist[] = {
/*P1*/ { -1, "/xx01", "/xx12,/xx14" },
/*P2*/ { -1, "/xx12", "/xx23" },
/*P3*/ { -1, "/xx23", "/xx35" },
/*P4*/ { -1, "/xx14", "/xx45" },
/*P5*/ { -1, "/xx35,/xx45", ""}
};
int i;
for (i = 0; i < ASZ(semlist); i++)
semlist[i].sem = sem_open(semlist[i].name, O_CREAT|O_EXCL, 0777, 0);
for (i = 0; i < ASZ(proclist); i++)
proclist[i].pid = start('1'+i, proclist[i].wait, proclist[i].post);
sem_post(semlist[0].sem);
for (i = 0; i < ASZ(proclist); i++)
waitpid(proclist[i].pid, NULL, 0);
for (i = 0; i < ASZ(semlist); i++) {
sem_close(semlist[i].sem);
sem_unlink(semlist[i].name);
}
return 0;
}