- 论坛徽章:
- 0
|
Understanding the Utility of Semaphores
The advantage of semaphores over other synchronization mechanisms is
that they can be used to synchronize two related or unrelated processes
trying to access the same resource.
Related Process
The processes are said to be related if the new process is created
from within an existing process, which ends up in duplicating the
resources of the creating process. Such processes are called related
processes. The following example shows how the related processes are
synchronized.
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int fd, i,count=0,nloop=10,zero=0,*ptr;
sem_t mutex;
//open a file and map it into memory
fd = open("log.txt",O_RDWR|O_CREAT,S_IRWXU);
write(fd,&zero,sizeof(int));
ptr = mmap(NULL,sizeof(int),PROT_READ |PROT_WRITE,MAP_SHARED,fd,0);
close(fd);
/* create, initialize semaphore */
if( sem_init(&mutex,1,1)
In this example, the related process access a common piece of memory, which is synchronized.
Unrelated Process
Processes are said to be unrelated if the two processes are unknown
to each other and no relationship exists between them. For example,
instances of two different programs are unrelated process. If such
programs try to access a shared resource, a semaphore could be used to
synchronize their access. The following source code demonstrates this:
File1: server.c
#include
#include
#include
#include
#include
#include
#include
#include
#define SHMSZ 27
char SEM_NAME[]= "vik";
int main()
{
char ch;
int shmid;
key_t key;
char *shm,*s;
sem_t *mutex;
//name the shared memory segment
key = 1000;
//create & initialize semaphore
mutex = sem_open(SEM_NAME,O_CREAT,0644,1);
if(mutex == SEM_FAILED)
{
perror("unable to create semaphore");
sem_unlink(SEM_NAME);
exit(-1);
}
//create the shared memory segment with this key
shmid = shmget(key,SHMSZ,IPC_CREAT|0666);
if(shmid
File 2: client.c
#include
#include
#include
#include
#include
#include
#include
#include
#define SHMSZ 27
char SEM_NAME[]= "vik";
int main()
{
char ch;
int shmid;
key_t key;
char *shm,*s;
sem_t *mutex;
//name the shared memory segment
key = 1000;
//create & initialize existing semaphore
mutex = sem_open(SEM_NAME,0,0644,0);
if(mutex == SEM_FAILED)
{
perror("reader:unable to execute semaphore");
sem_close(mutex);
exit(-1);
}
//create the shared memory segment with this key
shmid = shmget(key,SHMSZ,0666);
if(shmid
The above executables (client and server) demonstrate how semaphore could be used between completely different processes.
In addition to the applications shown above, semaphores can be used
cooperatively to access a resource. Please note that a semaphore is not
a Mutex. A Mutex allows serial access to a resource, whereas
semaphores, in addition to allowing serial access, could also be used
to access resources in parallel. For example, consider resource R being
accessed by n number of users. When using a Mutex, we would
need a Mutex "m" to lock and unlock the resource, thus allowing only
one user at a time to use the resource R. In contrast, semaphores can
allow n number of users to synchronously access the resource R. The best common example could be the
Toilet Example
.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/16030/showart_1715729.html |
|