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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

semaphore problem

Status
Not open for further replies.

Geeco

Technical User
Jun 30, 2003
8
FI
Hi,
In client/server application I have a shared memory block which contains two separate data variables which are used by multiple concurrent processes. I have created one semaphore which grants access to both of them. Only it is taking too long time for a process to do its job with this data (when there is many processes running at the same time). How can accessing these shared memory datas be more efficient and faster? Should I create another set of semaphore? What is the difference between many semaphoresets and many semaphores in one set?

Here is example of using my semaphore
//Before creating a child process, shared memory is attached and semaphore initialized.

sema_init();
shm_attach();

//in child process
shm_init();


if ( sem_lock() == -1 )
set error...
else
do_things_with_shared_data();
if ( sem_unlock() == -1 )
set error...

void sem_init()
{
sem_id = semget( IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | 0600)) == -1);
sem_val.val = 1;
semctl(sem_id, 0, SETVAL, sem_val);
}
void shm_attach ()
{

shm_id = shmget(key, 1024, IPC_CREAT | IPC_EXCL | 0600);
shm_addr = shmat(shm_id, NULL, 0);
}
int sem_lock()
{
sem_op.sem_num = 0;
sem_op.sem_op = -1;
sem_op.sem_flg = 0;

return semop( sem_id, &sem_op, 1);
}

int sem_unlock()
{
sem_op.sem_num = 0;
sem_op.sem_op = 1;
sem_op.sem_flg = 0;

return semop( sem_id, &sem_op, 1);
}

Thanks 4 help :)
 
Hi,

I will try to give answer of your second question, "What is the difference between many semaphoresets and many semaphores in one set? "
The difference comes (I am yalking of Semaphores on Unix)when you have more than one semaphore in a set and you want to do operations on both.
From the definition of the semop() function call, second argument of semop() is struct sembuf[] that takes the id of semaphore in the set and the operation to be perfromed on it.
There is not a way by which you can issue commands for semaphores belonging to different sets.
There by the overhead of coming back to user code segment for each semaphore can be reduced.
So in terms of perfromance , I think more than one semaphore in a set shall be better than multiple sets of one semaphore each.

Regarding your first question, Efficiency, I think there is no escape from semaphores. If both the processes can read/write simulataneously, Semaphores are mandatory.

I hope this helps

 
Ideally, do all preperation before hand, then lock, use the resouce, unlock. Say forinstance the data was a flat file memory mapped. Well you want the operation to be as swift and easy as possible so instead of removing lines, just use a bit/char at the start of the line to see if it is deleted...

Without more data on whatyour doing... can't be of much more help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top