免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 925 | 回复: 0
打印 上一主题 下一主题

Semaphores in Linux page 5 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-12 13:43 |只看该作者 |倒序浏览
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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP