Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ok I've tried so many things..and I

Status
Not open for further replies.

robertkjr3d

Programmer
Dec 19, 2003
36
US
Ok I've tried so many things..and I think the overall problem is that I'm just not as familiar with C code as I would like.

I'm useing #include <semaphore.h>

Ok I am declaring these

sem_t *mysemp;
int oflag = (O_CREAT | O_EXCL);
mode_t mode = 0644;
const char semname[] = &quot;/tmp/mysem1&quot;;
unsigned int value = 1;
int sts;

Ok in an intialization section I write this code
{
mysemp = sem_open(semname, oflag, mode, value);
}

Ok finally in the routine that has a critical section
{
mysemp = sem_open(semname, 0);

sem_wait(mysemp); // Critical section

//do stuff

sem_post(mysemp);// end critical section
}

There are multiple threads, and two different processes, that must be handled here. However this piece of code, by putting in degug code, I have ascertained that it hangs on the first sem_wait call, like it blocked the first call.

Help me correct this code!
 
A quick look at the manual suggests that you don't do this
mysemp = sem_open(semname, 0);

Create = sem_open
Lock = sem_wait
Release = sem_post

So it's just
Code:
{
sem_wait(mysemp); // Critical section

//do stuff

sem_post(mysemp);// end critical section
}

> mysemp = sem_open(semname, oflag, mode, value);
I think you only need do this once per process, since the result should be identical for all threads within a process.

--
 
Not the manual I've read... for one thing ...in that particular process or thread you need to open a connection to the semaphore in order to define what mysemp is.

see
However I believe I have found a real issue....

I found another page that mentions this

The mqueue manager must be running to use named semaphores.

It also mentions that named semaphores are kept in /dev/sem.. and that directory didn't exist on either one of my linux servers.

So I guess my new issue is to get the mqueue engine running.

It mentions to type &quot;mqueue &&quot; as root... however it doesn't seem to find that exists... so d'oh.. well the good part is I'll be an expert on semaphores by the time I'm done with this mess.

-Robert
 
Well that page (for locking) reads
Code:
int  oflag = 0;  /* open an existing semaphore;
                    do not create a new one */

.
.
.
mysemp = sem_open(semname, oflag, mode, value);

You missed two parameters (or rather, it got two parameters from who knows where).
It's also a good idea to put all that perror() error checking in there as well.

--
 
I do know something about this stuff..i've been studying it for awhile... the last two arguments of the sem_open are optional. Or rather only required when creating a semaphore, and are not needed when connecting to an existing semaphore.

See
However as I have already said..it appears my real problem is that I don't have the mqueue running, and doesn't appear to be on the system.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top