Solved

Semaphores, code question

Posted on 2004-10-13
4
317 Views
Last Modified: 2008-01-09
Can anyone show me where I would add semaphores to this to ensure it always will print ready, then set, then go?

#include <iostream.h>
#include <pthread.h>
using namespace std;

void *ready(void *u)
{
cout << "ready" << endl;
}

void *set(void *u)
{
cout << "set" << endl;
}
      
void *go(void *u)
{
cout << "go" << endl;
}    

main()
{
pthread_t tr, ts, tg;

pthread_create(&tr,NULL,ready,NULL);
pthread_create(&ts,NULL,set,NULL);
pthread_create(&tg,NULL,go,NULL);

pthread_join(tr,NULL);
pthread_join(ts,NULL);
pthread_join(tg,NULL);
}
0
Comment
Question by:killer455
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
meidanze earned 75 total points
Comment Utility
Here are my comments,
Sorry i did not write you the correct syntax of the command but i do'nt have infront of me
a unix machine to check it.

#include <iostream.h>
#include <pthread.h>
using namespace std;

void *ready(void *u)
{
//wait to semaphore to become 3 , run first (using semop)
cout << "ready" << endl;
//decrease semaphore in 1 (using semop)
}

void *set(void *u)
{
//wait to semaphore to become 2,, run second (using semop)
cout << "set" << endl;
//decrease semaphore in 1 (using semop)
}
     
void *go(void *u)
{
//wait to semaphore to become 1, run first (using semop)
cout << "go" << endl;
}    

main()
{


pthread_t tr, ts, tg;

// Create the semaphore  (using semget)
//Init the semaphore to 3(using semctrl)
pthread_create(&tr,NULL,ready,NULL);
pthread_create(&ts,NULL,set,NULL);
pthread_create(&tg,NULL,go,NULL);

pthread_join(tr,NULL);
pthread_join(ts,NULL);
pthread_join(tg,NULL);

//Delete the semaphore (using semctrl)
}
0
 
LVL 1

Expert Comment

by:jaguarul
Comment Utility
I don't know the exact type signature of your functions, but are you sure they should return a void pointer? (you have void* in front of each function). Then add a "return NULL;" in each of your functions, otherwise you are in for some stack trouble ;)
0
 
LVL 1

Expert Comment

by:meidanze
Comment Utility
The signature of the function MUST be void * because the signature of pthread_create
include the void *function(void  *0) argument.
no need to return NULL.
0
 
LVL 1

Expert Comment

by:jaguarul
Comment Utility
You may be right, but then the function signature is strange: void * f() means that f returns a void pointer, right? So in principle, you should respect what you declare. In practice, it may work on some architectures and compilers, because in this case the return value might be stored in a register instead of the stack, so no problem arises if you don't set that return value. At least in one example I saw on the internet, the thread function returned something (actually NULL). I'm not saying it will not work in some cases...

In addition, void *function(void  *0)  means a pointer to a function that returns void (not void *) and takes a void pointer as parameter.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now