Semaphores Help
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Semaphores Help




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Semaphores Help  
Xarky


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-04 04:35 PM

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





[ Post a follow-up to this message ]



    Re: Semaphores Help  
Juha Laiho


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-04 09: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)





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 10:06 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register