使用信号灯程序运行正常在Linux ...在Mac OSX上意外的结果 [英] Program using Semaphores runs fine on Linux...unexpected results on Mac osX
问题描述
我写了一个简单的解决方案使用旗语读者,作家的问题。它完美地运行在Linux操作系统,但是当我在我的Mac OSX上运行它,我得到意想不到的结果,我想不通为什么。
我的计划:
的#include< semaphore.h>
#包括LT&; SYS / types.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&pthreads.h中GT;
#包括LT&;&unistd.h中GT;void *的功能1(无效* VAL);
void *的函数2(无效* VAL);// 共同的价值观
挥发性INT X;
挥发性INTÿ;//声明信号灯
sem_t S1;
sem_t S2;主要()
{
void *的状态;的pthread_t线程1;
的pthread_t线程2;
函数srand(时间(NULL));//初始化信号量为零
sem_init(安培; S1,0,0);
sem_init(安培; S2,0,0);在pthread_create(安培;线程1,NULL,功能1,NULL);
在pthread_create(安培;线程2,NULL,函数2,NULL);在pthread_join(线程1,和放大器;状态);
在pthread_join(线程2,和放大器;状态);sem_destroy(安培; S1);
sem_destroy(安培; S2);}void *的功能1(无效* VAL)
{
而(1)
{
X =兰特()%1000; //写
的printf(经过线程ID一个写入X,X =%d个\\ N,X);
sem_post(安培; S1); //信号
sem_wait(安培; S2); //等待
的printf(,Y后,线程ID一个从Y,Y =%d个\\ n读); //读取
睡眠(3);
}
}void *的函数2(无效* VAL)
{
而(1)
{
sem_wait(安培; S1); //等待
的printf(,X后,线程ID b从X,X =%d个\\ n读); //读取
Y =兰特()%1000; //写
的printf(,Y线程编号B写Y,Y =%d个\\ n后);
sem_post(安培; S2); //信号
睡眠(3);
}
}
我收到在Linux(什么它应该看起来像)输出:
之后线程ID一个写入X,X = 100
经过线程编号B根据X读,X = 100
螺纹编号B写Y,Y = 234后
经过线程ID一个从Y,Y = 234读取
...
在Mac OSX上的输出(意外):
之后线程ID一个写入X,X = 253
经过线程ID一个从Y,Y = 0读取
经过线程编号B根据X读,X = 253
螺纹编号B写Y,Y = 728后
...
检查的sem_init调用返回的错误;我敢打赌,你会发现OS X版本返回一个函数未实现错误。
这是因为不愿透露姓名的POSIX信号不能在OS X 实施。您需要使用命名信号,或Pthread互斥/条件变量。
I wrote a simple program solving the Readers-Writers problem using semaphores. It runs perfectly on Linux os, but when I run it on my Mac osX I get unexpected results and I can't figure out why.
My Program:
#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* function1(void* val);
void* function2(void* val);
// shared values
volatile int X;
volatile int Y;
// declare semaphores
sem_t s1;
sem_t s2;
main()
{
void* status;
pthread_t thread1;
pthread_t thread2;
srand(time(NULL));
// initialize semaphores to zero
sem_init(&s1, 0, 0);
sem_init(&s2, 0, 0);
pthread_create(&thread1, NULL, function1, NULL);
pthread_create(&thread2, NULL, function2, NULL);
pthread_join(thread1, &status);
pthread_join(thread2, &status);
sem_destroy(&s1);
sem_destroy(&s2);
}
void* function1(void* val)
{
while(1)
{
X = rand()%1000; // write
printf("After thread ID A writes to X, X = %d\n", X);
sem_post(&s1); // signal
sem_wait(&s2); // wait
printf("After thread ID A reads from Y, Y = %d\n", Y); // read
sleep(3);
}
}
void* function2(void* val)
{
while(1)
{
sem_wait(&s1); // wait
printf("After thread ID B reads from X, X = %d\n", X); // read
Y = rand()%1000; // write
printf("After thread ID B write to Y, Y = %d\n", Y);
sem_post(&s2); // signal
sleep(3);
}
}
The output I receive on Linux (what it's supposed to look like):
After thread ID A writes to X, X = 100
After thread ID B reads from X, X = 100
After thread ID B write to Y, Y = 234
After thread ID A reads from Y, Y = 234
...
The output on Mac osX (unexpected):
After thread ID A writes to X, X = 253
After thread ID A reads from Y, Y = 0
After thread ID B reads from X, X = 253
After thread ID B write to Y, Y = 728
...
Check the error return on the sem_init calls; I bet you'll find the OS X version returning a "Function not implemented" error.
This is because unnamed POSIX semaphores are not implemented on OS X. You need to use named semaphores, or pthread mutex/condition variables.
这篇关于使用信号灯程序运行正常在Linux ...在Mac OSX上意外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!