Solved

Semaphores, code question

Posted on 2004-10-13
4
319 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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.

813 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

11 Experts available now in Live!

Get 1:1 Help Now