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