Linux旗语编程实例


  1. /*旗语是一个受保护的变量。对两个或多个进程共享的资源,它可以提供限制访问的方法。  
  2.   Linux的旗语就是操作系统原理中的信号量,有PV操作。可以获得和释放旗语;  
  3.   释放旗语会自动唤醒下一个等待获取旗语的进程。  
  4.   旗语有两种类型:  
  5.   二进制旗语:代表单个资源  
  6.   计数旗语:用来代表数量大于一的共享资源  
  7.   GNU/Linux中的旗语实际上是一个旗语数组。说是一个旗语,实际上代表了一个包含64个旗语的数组。  
  8.   GNU/Linux的这个特点允许同时对很多个旗语进行元操作。  
  9.   要包含头文件:  
  10.   <sys/types.h>,<sys/ipc.h>,<sys/sem.h>  
  11.   message queue  
  12.   semaphore array  
  13.   */  
  14. //展示了旗语的创建,获得,释放,显示/修改旗语计数值,移除旗语操作   
  15. #include <sys/types.h>   
  16. #include <sys/ipc.h>   
  17. #include <sys/sem.h>   
  18. #include <stdio.h>   
  19. #define MY_SEM_ID 12321   
  20. int main(  )   
  21.     {   
  22.         //创建一个旗语   
  23.         int semid,ret;   
  24.         semid=semget( MY_SEM_ID,1,0666|IPC_CREAT );   
  25.         //注意创建一个新的旗语,其计数值是不确定的,有的默认为0,要想建立可移植程序   
  26.         //应该显式的初始化旗语的计数值   
  27.         if( semid>=0 )   
  28.             printf( "Created a semphore %d\n",semid );   
  29.         //获取当前旗语计数   
  30.         int count;   
  31.         count=semctl( semid,0,GETVAL );   
  32.         if( count!=-1 )   
  33.             printf( "Current semaphore count is %d\n",count );   
  34.         //初始化旗语的计数值   
  35.         ret=semctl( semid,0,SETVAL,1 );//把1换成>1的数就会变成计数旗语   
  36.         if( ret!=-1 )   
  37.             printf( "Initialize semaphore conut is 1\n");   
  38.            
  39.         //获得一个旗语   
  40.         int semid_acq;   
  41.         struct sembuf sb; //旗语操作结构体   
  42.            
  43.         semid_acq=semget( MY_SEM_ID,1,0 );   
  44.         if( semid_acq>=0 )   
  45.             {   
  46.                 sb.sem_num=0; //旗语号,因为是二进制旗语所以用0   
  47.                 sb.sem_op=-1; //把旗语数减1,但仅在旗语数的值大于0时进行,   
  48.                               //如果已经为0,则该操作及其进程会阻断直到旗语数值增加;相当于P操作   
  49.                 sb.sem_flg=0;   
  50.                 printf( "Attempting to acquire semaphore %d\n",semid_acq );   
  51.                 if( semop( semid_acq,&sb,1 )==-1 )   
  52.                 {   
  53.                     printf( "Acquired the semaphore failed %d\n",semid_acq );   
  54.                     exit( -1 );   
  55.                 }   
  56.                 printf( "Acquired the semaphore successfully %d\n",semid_acq );   
  57.             }   
  58.         //释放一个旗语   
  59.         printf( "Releasing semaphore %d\n",semid_acq );   
  60.         sb.sem_num=0;   
  61.         sb.sem_op=1;   //旗语数加1,进行释放,相当于V操作,注意与上面设置的对称性   
  62.         sb.sem_flg=0;   
  63.         if( semop( semid_acq,&sb,1 )==-1)   
  64.            {   
  65.             printf( "Released semaphore failed %d\n",semid_acq );   
  66.             exit( -1 );   
  67.            }   
  68.         printf( "Released semaphore sucessfully %d\n",semid_acq );   
  69.         //移除一个旗语   
  70.         ret=semctl( semid_acq,0,IPC_RMID );   
  71.         if( ret!=-1 )   
  72.             printf( "Semaphore %d has removed\n",semid_acq );   
  73.            
  74.         return 0;   
  75.            
  76.     }  

相关内容