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 ]
|