qfedu-linux-advanced-level/day7/named_sem_3.c

35 lines
581 B
C
Raw Normal View History

#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
// 无血缘关系的进程的互斥
void printer(char *msg)
{
while (*msg)
{
printf("%c", *msg++);
fflush(stdout);
sleep(1);
}
}
int main(int argc, char const *argv[])
{
// 打开信号量文件
sem_t *sem = sem_open("mysem", O_CREAT | O_RDWR, 0666, 1);
sem_wait(sem);
#ifdef DISEN
printer("disen666\n");
#else
printer("jack888\n");
#endif
sem_post(sem);
sem_close(sem);
return 0;
}