现在我正在做我的项目,我有一个关于信号量初始化的问题。实际上我在 Mac OS X 上编程,但我试图在 Linux 上编译我的项目,但它没有编译。在 OS X 上,它可以编译,但每次都在初始化时崩溃。
sem_t *mutex_1, *mutex_2, *mutex_3, *reader, *writer;
int initialization_semaphores (void)
{
int ERROR = EOK;
if ((mutex_1 = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
ERROR = ESEM;
if ((mutex_2 = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
ERROR = ESEM;
if ((mutex_3 = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
ERROR = ESEM;
if ((reader = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
ERROR = ESEM;
if ((writer = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
ERROR = ESEM;
if (ERROR == EOK) {
if (sem_init(mutex_1, 1, 1) == -1)
ERROR = ESEM;
if (sem_init(mutex_2, 1, 1) == -1)
ERROR = ESEM;
if (sem_init(mutex_3, 1, 1) == -1)
ERROR = ESEM;
if (sem_init(reader, 1, 1) == -1)
ERROR = ESEM;
if (sem_init(writer, 1, 1) == -1)
ERROR = ESEM;
}
}
当我尝试在 linux 上编译它时,我看到了这个:
/tmp/ccmkN9G7.o: In function `initialization_semaphores':
readerWriter.c:(.text+0x1a2): undefined reference to `sem_init'
readerWriter.c:(.text+0x1cb): undefined reference to `sem_init'
readerWriter.c:(.text+0x1f4): undefined reference to `sem_init'
readerWriter.c:(.text+0x21d): undefined reference to `sem_init'
readerWriter.c:(.text+0x246): undefined reference to `sem_init'
readerWriter.c:(.text+0x275): undefined reference to `shm_open'
这样对吗?:
int ERROR = EOK;
mutex_1 = sem_open("mutex1", O_CREAT, S_IRUSR | S_IWUSR, 1);
mutex_2 = sem_open("mutex2", O_CREAT, S_IRUSR | S_IWUSR, 1);
mutex_3 = sem_open("mutex3", O_CREAT, S_IRUSR | S_IWUSR, 1);
reader = sem_open("reader", O_CREAT, S_IRUSR | S_IWUSR, 1);
writer = sem_open("writer", O_CREAT, S_IRUSR | S_IWUSR, 1);