Hi I have been playing with a bit of code to teach myself how to use semaphores on my MAC OSX, however it does not appear to be working, here is a screen shot of what I am getting:
1. The value of the semaphors is 0
1. The value of the semaphors is 0
Bad file descriptor mutex returned -1
Here is the code that I have been playing with:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define NITER 1000000
int count = 0;
sem_t *mutex;
void * ThreadAdd(void * a)
{
int value;
sem_getvalue(mu tex, &value);
printf("1. The value of the semaphors is %d\n", value);
int r =0;
if((r = sem_trywait(mut ex)) != 0)
{
fprintf(stderr, "%s mutex returned %d\n",strerror( errno),r);
exit(EXIT_FAILU RE);
}
int i, tmp;
for(i = 0; i < NITER; i++)
{
tmp = count; /* copy the global count locally */
tmp = tmp+1; /* increment the local copy */
count = tmp; /* store the local value into the global count */
}
sem_post (mutex);
sem_getvalue(mu tex, &value);
printf("2. The value of the semaphors is %d\n", value);
}
int main(int argc, char * argv[])
{
pthread_t tid1, tid2;
sem_init(mutex, 0, 1);
//mutex = sem_open("mutex ",O_CREAT, S_IRUSR | S_IWUSR, 10);
if(pthread_crea te(&tid1, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 1");
exit(EXIT_FAILU RE);
}
if(pthread_crea te(&tid2, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 2");
exit(EXIT_FAILU RE);
}
if(pthread_join (tid1, NULL)) /* wait for the thread 1 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILU RE);
}
if(pthread_join (tid2, NULL)) /* wait for the thread 2 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILU RE);
}
if (count < 2 * NITER)
printf("\n BOOM! count is [%d], should be %d\n", count, 2*NITER);
else
printf("\n OK! count is [%d]\n", count);
sem_close(mutex );
//sem_unlink("mut ex");
pthread_exit(NU LL);
}
and here is how I have been compiling the code
gcc -o filename filename.c -lpthread
I feel like I am missing something really obvious here but don't know what. Has anybody got any idea's?
1. The value of the semaphors is 0
1. The value of the semaphors is 0
Bad file descriptor mutex returned -1
Here is the code that I have been playing with:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define NITER 1000000
int count = 0;
sem_t *mutex;
void * ThreadAdd(void * a)
{
int value;
sem_getvalue(mu tex, &value);
printf("1. The value of the semaphors is %d\n", value);
int r =0;
if((r = sem_trywait(mut ex)) != 0)
{
fprintf(stderr, "%s mutex returned %d\n",strerror( errno),r);
exit(EXIT_FAILU RE);
}
int i, tmp;
for(i = 0; i < NITER; i++)
{
tmp = count; /* copy the global count locally */
tmp = tmp+1; /* increment the local copy */
count = tmp; /* store the local value into the global count */
}
sem_post (mutex);
sem_getvalue(mu tex, &value);
printf("2. The value of the semaphors is %d\n", value);
}
int main(int argc, char * argv[])
{
pthread_t tid1, tid2;
sem_init(mutex, 0, 1);
//mutex = sem_open("mutex ",O_CREAT, S_IRUSR | S_IWUSR, 10);
if(pthread_crea te(&tid1, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 1");
exit(EXIT_FAILU RE);
}
if(pthread_crea te(&tid2, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 2");
exit(EXIT_FAILU RE);
}
if(pthread_join (tid1, NULL)) /* wait for the thread 1 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILU RE);
}
if(pthread_join (tid2, NULL)) /* wait for the thread 2 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILU RE);
}
if (count < 2 * NITER)
printf("\n BOOM! count is [%d], should be %d\n", count, 2*NITER);
else
printf("\n OK! count is [%d]\n", count);
sem_close(mutex );
//sem_unlink("mut ex");
pthread_exit(NU LL);
}
and here is how I have been compiling the code
gcc -o filename filename.c -lpthread
I feel like I am missing something really obvious here but don't know what. Has anybody got any idea's?
Comment