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(mutex, &value);
printf("1. The value of the semaphors is %d\n", value);
int r =0;
if((r = sem_trywait(mutex)) != 0)
{
fprintf(stderr,"%s mutex returned %d\n",strerror(errno),r);
exit(EXIT_FAILURE);
}
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(mutex, &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_create(&tid1, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 1");
exit(EXIT_FAILURE);
}
if(pthread_create(&tid2, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 2");
exit(EXIT_FAILURE);
}
if(pthread_join(tid1, NULL)) /* wait for the thread 1 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILURE);
}
if(pthread_join(tid2, NULL)) /* wait for the thread 2 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILURE);
}
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("mutex");
pthread_exit(NULL);
}
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?
2 3001
pthread_t tid1, tid2;
sem_init(mutex, 0, 1);
//mutex = sem_open("mutex",O_CREAT, S_IRUSR | S_IWUSR, 10);
if(pthread_create(&tid1, NULL, ThreadAdd, NULL))
{
So exactly what are the contents of tid1 and tid2?
I see them defined but never initialized.
HI thanks for your response but I have figured out what is going wrong here. I had a look at tid1 and tid2 and re-read the man page fore pthread_create. From what I understand the command "pthread_create(&tid1, NULL, ThreadAdd, NULL)" will initialize tid1 to default values as the attr argument is NULL. The actual problem in the code was that MAC OS does not support POSIX style unamed semaphores but does support the older Mach style which is a counting semaphore therefore the solution was either used named semaphore or the Mach. So I modified the original code to do this:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
// Mach semaphore
#include <mach/semaphore.h>
#include <mach/task.h>
#define NITER 1000000
int count = 0;
semaphore_t sem = 0;
void * ThreadAdd(void * a)
{
semaphore_wait(sem);
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 */
}
semaphore_signal(sem);
}
int main(int argc, char * argv[])
{
pthread_t tid1, tid2;
int initialValue = 1;
semaphore_create(mach_task_self(), &sem, SYNC_POLICY_FIFO, initialValue);
if(pthread_create(&tid1, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 1");
exit(EXIT_FAILURE);
}
if(pthread_create(&tid2, NULL, ThreadAdd, NULL))
{
printf("\n ERROR creating thread 2");
exit(EXIT_FAILURE);
}
if(pthread_join(tid1, NULL)) /* wait for the thread 1 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILURE);
}
if(pthread_join(tid2, NULL)) /* wait for the thread 2 to finish */
{
printf("\n ERROR joining thread");
exit(EXIT_FAILURE);
}
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);
pthread_exit(NULL);
}
and sha-bam problem solved.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Bastian Hammer |
last post by:
Hi
I´m wondering why there are so few examples with Semaphore.
Is it obsolete?
I´ve got a Class Data.
It offers 2 Threads methods for updating, editing, .. a private
dictionary.
Now I...
|
by: rushik |
last post by:
Hello all,
I m facing an interesting problem related to semaphore in php.
We are using a huge business application running on LAMP. For database
operations we are maintaining centralized DB...
|
by: marvind |
last post by:
I tried using a Semaphore class (have included the full listing
reproduced from article Figure 1 at the end of this email) in .NET 1.1.
It works fine most of the time, however, I see the following...
|
by: Unni |
last post by:
Hello all,
I m facing a memory related problem with semaphores. Our business
application uses semaphores extensively and the limit imposed by the OS
on the number of semaphores that can exist on...
|
by: techi_C |
last post by:
Hi
I'm getting a problem while removing semaphore from system.
Before removing semaphore I'm checking the usage count of a smaphore.
// checking usage count
usage_count =...
| |
by: Dimitri Furman |
last post by:
SQL Server 2000 SP4.
Running the script below prints 'Unexpected':
-----------------------------
DECLARE @String AS varchar(1)
SELECT @String = 'z'
IF @String LIKE ''
|
by: sukasa |
last post by:
Greetings.
After first noticing a malfunction in my application, I ran some tests which have given me the following insights on the code performance:
-The " infinite" loop which waits on...
|
by: Samuel R. Neff |
last post by:
I'm having trouble creating a Semaphore with read-access rights for
everyone. Originally I was trying to use this code:
semaphore = new Semaphore(maxLocks, maxLocks, "RwLock#" + name);
but...
|
by: GHUM |
last post by:
hello,
in my application I am using
hSem = win32event.CreateSemaphore (None, 1,
1,"stringincludinginterfaceandport")
rt=win32event.WaitForSingleObject (hSem, 0)
if rt !=...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |