Unix Programming - Semaphores Help

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2004 > Semaphores Help





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Semaphores Help
Xarky

2004-04-27, 11:35 am

Hi,

I am trying to use semaphores. I am trying to use the same
semaphore between two different unrelated processes.

From the client, I am setting a value, which I want to be received
by the server. Unfortunately this is not happening.

Can someone tell me how to do it, or fix my error pls.


Thanks in Advance


Sample of my code being used is given below:

*** Client.c ***

key_t key;
int semid;
union semun data;

key = ftok(".", 'a');
if (key == -1)
error_msg(); // a method giving the error set by errno

semid = semget (key, 1, 0666);
if (semid == -1)
{
printf ("Cannot connect to server.... Please try again....\n");
exit (EXIT);
} // to make sure that server is created before clients start to
connect


data.val = 1;
if (semctl(semid, 0, SETVAL, data) == -1) // setting value in the
semaphore
error_msg();


*** Server.c ***

key_t key;
int semid;

key = ftok (".", 'a');
if (key == -1)
{
error_msg();
exit (EXIT_FAILURE);
} // end if

semid = semget (key, 1, 0666 | IPC_CREAT);
if (semid == -1)
error_msg();

if (semctl (semid, 0, GETVAL, data) == -1)
error_msg();

// the value of data.val is never becoming one
// The semctl line is in loop with a sleep(2), where loop ends when
data.val=1
// This is made to ensure that there is enough time to wait
Juha Laiho

2004-04-27, 4:34 pm

bernardpace@yahoo.com (Xarky) said:
> I am trying to use semaphores. I am trying to use the same
>semaphore between two different unrelated processes.
>
> From the client, I am setting a value, which I want to be received
>by the server. Unfortunately this is not happening.


Hmm.. are you sure semaphores are the correct thing for you to use --
i.e. message queues are better for generic messaging.

> Can someone tell me how to do it, or fix my error pls.


The issue appears to be that you're expecting GETVAL to bring the value
into data.val, which apparently does not happen; SETVAL and GETVAL
don't seem to be symmetric in how they pass the data. I'm not certain
of my reading, but it looks like GETVAL would produce the value you're
looking for as the actual return value of the semctl function. The
alternative place to look for would be at element 0 of the value
array (3rd parameter).
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com