Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Semaphores, code question

Posted on 2004-10-13
4
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
meidanze earned 300 total points
ID: 12308925
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
ID: 12311728
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
ID: 12317534
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
ID: 12324277
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello I read in a discussion about a person who configured a very simple mirror RAID with two hard drives; the system and data were on the same partition. He asked how to repair the system as it was not booting up anymore. In his case running …
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

609 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