linux中信号量捕捉脚本,Linux信号量编程实例

本例示范Linux信号量的基本用法。该范例使用了两个线程分别对一个公用队列进行入队和出队操作,并用信号量进行控制,当队列空时出队操作可以被阻塞,当队列满时入队操作可以被阻塞。

主要用到的信号量函数有:

sem_init:初始化信号量sem_t,初始化的时候可以指定信号量的初始值,以及是否可以在多进程间共享。

sem_wait:一直阻塞等待直到信号量>0。

sem_timedwait:阻塞等待若干时间直到信号量>0。

sem_post:使信号量加1。

sem_destroy:释放信号量。和sem_init对应。

关于各函数的具体参数请用man查看。如man sem_init可查看该函数的帮助。

下面看具体的代码:

None.gif//--------------------------msgdequeue.h开始-------------------------------------

None.gif//实现可控队列None.gif#ifndef MSGDEQUEUE_H

None.gif#defineMSGDEQUEUE_HNone.gif#include"tmutex.h"None.gif#includeNone.gif#includeNone.gif#includeNone.gif#includeNone.gif#includeNone.gifusingnamespacestd;

None.gif

None.giftemplateNone.gifclassCMessageDequeue

ExpandedBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.pngpublic:

6a9c071a08f1dae2d3e1c512000eef41.png        CMessageDequeue(size_t MaxSize) : m_MaxSize( MaxSize )

ExpandedSubBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.png                sem_init(&m_enques,0, m_MaxSize );//入队信号量初始化为MaxSize,最多可容纳MaxSize各元素6a9c071a08f1dae2d3e1c512000eef41.pngsem_init(&m_deques,0,0);//队列刚开始为空,出队信号量初始为0ExpandedSubBlockEnd.gif}6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png~CMessageDequeue()

ExpandedSubBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.png                sem_destroy(&m_enques);

6a9c071a08f1dae2d3e1c512000eef41.png                sem_destroy(&m_deques);

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngintsem_wait_i( sem_t*psem,intmswait )

ExpandedSubBlockStart.gif{//等待信号量变成>0,mswait为等待时间,若mswait<0则无穷等待,否则等待若干mswait毫秒。6a9c071a08f1dae2d3e1c512000eef41.pngif( mswait<0)

ExpandedSubBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.pngintrv=0;                          

6a9c071a08f1dae2d3e1c512000eef41.pngwhile( ((rv=sem_wait(psem) )!=0)&&(errno==EINTR

6a9c071a08f1dae2d3e1c512000eef41.png) );//等待信号量,errno==EINTR屏蔽其他信号事件引起的等待中断6a9c071a08f1dae2d3e1c512000eef41.pngreturnrv;    

ExpandedSubBlockEnd.gif                }6a9c071a08f1dae2d3e1c512000eef41.pngelseExpandedSubBlockStart.gif{                                            

6a9c071a08f1dae2d3e1c512000eef41.png                        timespec ts;                         

6a9c071a08f1dae2d3e1c512000eef41.png                        clock_gettime(CLOCK_REALTIME,&ts );//获取当前时间6a9c071a08f1dae2d3e1c512000eef41.pngts.tv_sec+=(mswait/1000);//加上等待时间的秒数6a9c071a08f1dae2d3e1c512000eef41.pngts.tv_nsec+=( mswait%1000)*1000;//加上等待时间纳秒数6a9c071a08f1dae2d3e1c512000eef41.pngintrv=0;                          

6a9c071a08f1dae2d3e1c512000eef41.pngwhile( ((rv=sem_timedwait( psem,&ts ))!=0)&&(errno==6a9c071a08f1dae2d3e1c512000eef41.pngEINTR) );//等待信号量,errno==EINTR屏蔽其他信号事件引起的等待中断6a9c071a08f1dae2d3e1c512000eef41.pngreturnrv;   

ExpandedSubBlockEnd.gif                }6a9c071a08f1dae2d3e1c512000eef41.png                                                             

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.pngboolpush_back(constT&item,intmswait=-1)     

ExpandedSubBlockStart.gif{//等待mswait毫秒直到将item插入队列,mswait为-1则一直等待6a9c071a08f1dae2d3e1c512000eef41.pngif(-1==sem_wait_i(&m_enques, mswait ))   

ExpandedSubBlockStart.gif{                                            

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfalse;                        

ExpandedSubBlockEnd.gif                }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png//AUTO_GUARD:定界加锁,见Linux多线程及临界区编程例解的tmutex.h文件定义。6a9c071a08f1dae2d3e1c512000eef41.pngAUTO_GUARD( g, MUTEX_TYPE, m_lock );

6a9c071a08f1dae2d3e1c512000eef41.pngtryExpandedSubBlockStart.gif{                                            

6a9c071a08f1dae2d3e1c512000eef41.png                        m_data.push_back( item );            

6a9c071a08f1dae2d3e1c512000eef41.png                        cout<

6a9c071a08f1dae2d3e1c512000eef41.png                        sem_post(&m_deques );               

6a9c071a08f1dae2d3e1c512000eef41.pngreturntrue;                         

ExpandedSubBlockEnd.gif                }6a9c071a08f1dae2d3e1c512000eef41.pngcatch(...)                                   

ExpandedSubBlockStart.gif{                                            

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfalse;                        

ExpandedSubBlockEnd.gif                }ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngboolpop_front( T&item,boolbpop=true,intmswait=-1)      

ExpandedSubBlockStart.gif{//等待mswait毫秒直到从队列取出元素,mswait为-1则一直等待6a9c071a08f1dae2d3e1c512000eef41.pngif(-1==sem_wait_i(&m_deques, mswait ) )  

ExpandedSubBlockStart.gif{                                            

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfalse;                        

ExpandedSubBlockEnd.gif                }6a9c071a08f1dae2d3e1c512000eef41.png//AUTO_GUARD:定界加锁,见Linux多线程及临界区编程例解的tmutex.h文件定义。6a9c071a08f1dae2d3e1c512000eef41.pngAUTO_GUARD( g, MUTEX_TYPE, m_lock );         

6a9c071a08f1dae2d3e1c512000eef41.pngtryExpandedSubBlockStart.gif{                                            

6a9c071a08f1dae2d3e1c512000eef41.png                        item=m_data.front();               

6a9c071a08f1dae2d3e1c512000eef41.pngif( bpop )                           

ExpandedSubBlockStart.gif{                                    

6a9c071a08f1dae2d3e1c512000eef41.png                                m_data.pop_front();          

6a9c071a08f1dae2d3e1c512000eef41.png                                cout<

ExpandedSubBlockEnd.gif                        }6a9c071a08f1dae2d3e1c512000eef41.png                                                             

6a9c071a08f1dae2d3e1c512000eef41.png                        sem_post(&m_enques );               

6a9c071a08f1dae2d3e1c512000eef41.pngreturntrue;                         

ExpandedSubBlockEnd.gif                }6a9c071a08f1dae2d3e1c512000eef41.pngcatch(...)                                   

ExpandedSubBlockStart.gif{                                            

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfalse;                        

ExpandedSubBlockEnd.gif                }ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.png        inline size_t size()                                 

ExpandedSubBlockStart.gif{                                                    

6a9c071a08f1dae2d3e1c512000eef41.pngreturnm_data.size();                        

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivate:                                                     

6a9c071a08f1dae2d3e1c512000eef41.png        MUTEX_TYPE m_lock;                                   

6a9c071a08f1dae2d3e1c512000eef41.png        dequem_data;                                     

6a9c071a08f1dae2d3e1c512000eef41.png        size_t m_MaxSize;                                    

6a9c071a08f1dae2d3e1c512000eef41.png        sem_t m_enques;                                      

6a9c071a08f1dae2d3e1c512000eef41.png        sem_t m_deques;                                      

ExpandedBlockEnd.gif};                                                           

None.gif                                                             

None.gif#endifNone.gif

None.gif//--------------------------msgdequeue.h结束-------------------------------------

None.gif

None.gif//--------------------------test.cpp开始-------------------------------------

None.gif//主程序文件None.gifNone.gif#include"msgdequeue.h"None.gif#includeNone.gif#includeNone.gifusingnamespacestd;

None.gif

None.gifCMessageDequeueqq(5);

None.gif

None.gifvoid*get_thread(void*parg);

None.gifvoid*put_thread(void*parg);

None.gif

None.gifvoid*get_thread(void*parg)

ExpandedBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.pngwhile(true)

ExpandedSubBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.pnginta=-1;

6a9c071a08f1dae2d3e1c512000eef41.pngif(!qq.pop_front( a,true,1000) )

ExpandedSubBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.png                        cout<

ExpandedSubBlockEnd.gif                }ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.pngreturnNULL;

ExpandedBlockEnd.gif}None.gif

None.gifvoid*put_thread(void*parg)

ExpandedBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.pngfor(inti=1; i<=30; i++)

ExpandedSubBlockStart.gif{

6a9c071a08f1dae2d3e1c512000eef41.png                qq.push_back( i,-1);

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngreturnNULL;                                         

ExpandedBlockEnd.gif}None.gif                                                             

None.gifintmain()                                                   

ExpandedBlockStart.gif{                                                          

6a9c071a08f1dae2d3e1c512000eef41.png        pthread_t pget,pput;                                 

6a9c071a08f1dae2d3e1c512000eef41.png        pthread_create(&pget,NULL,get_thread,NULL);         

6a9c071a08f1dae2d3e1c512000eef41.png        pthread_create(&pput, NULL, put_thread,NULL);       

6a9c071a08f1dae2d3e1c512000eef41.png                                                             

6a9c071a08f1dae2d3e1c512000eef41.png        pthread_join( pget,NULL );                           

6a9c071a08f1dae2d3e1c512000eef41.png        pthread_join( pput,NULL );                           

6a9c071a08f1dae2d3e1c512000eef41.png                                                             

6a9c071a08f1dae2d3e1c512000eef41.pngreturn0;                                            

ExpandedBlockEnd.gif}None.gif

None.gif//--------------------------test.cpp结束-------------------------------------

编译程序:g++ msgdequeue.h test.cpp -lpthread -lrt -o test

-lpthread链接pthread库。-ltr链接clock_gettime函数相关库。

编译后生成可执行文件test。输入./test执行程序。

线程get_thread每隔1000毫秒从队列取元素,线程put_thread将30个元素依次入队。两个线程模拟两条入队和出队的流水线。因我们在 CMessageDequeueqq(5)处定义了队列最多可容纳5个元素,入队线程每入队到队列元素满5个后需阻塞等待出队线程将队列元素出队才能继续。测试时可调整队列可容纳最大元素个数来观察运行效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值