linux 共享内存实例,Linux下共享内存编程实例

#include #define MY_SHM_ID 34325

#define MY_SEM_ID 23234

#define MAX_STRING 200

typedef struct

{

int semID;

int counter;

char string[ MAX_STRING+1 ];

}MY_BLOCK_T;

int main(int argc,char** argv)

{

int shmid,ret,i;

MY_BLOCK_T* block;

struct sembuf sb;

char user;

//make sure there is a command

if( argc>=2 )

{

//create the shared memory segment and init it

//with the semaphore

if( !strncmp(argv[ 1 ],"create",6) )

{

//create the shared memory segment and semaphore

printf( "Creating the shared memory/n" );

shmid=shmget( MY_SHM_ID,sizeof( MY_BLOCK_T ),( IPC_CREAT|0666 ) );

block=( MY_BLOCK_T* )shmat( shmid,( const void* )0,0 );

block->counter=0;

//create the semaphore and init

block->semID=semget(MY_SEM_ID,1,( IPC_CREAT|0666 ));

sb.sem_num=0;

sb.sem_op=1;

sb.sem_flg=0;

semop( block->semID,&sb,1 );

//now detach the segment

shmdt( ( void* )block );

printf( "Create the shared memory and semaphore successuflly/n" );

}

else if( !strncmp(argv[ 1 ],"use",3) )

{

/*use the segment*/

//must specify  also a letter to write to the buffer

if( argc<3 ) exit( -1 );

user=( char )argv[ 2 ][ 0 ];

//grab the segment

shmid=shmget( MY_SHM_ID,0,0 );

block=( MY_BLOCK_T* )shmat( shmid,( const void* )0,0 );

/*##########重点就是使用旗语对共享区的访问###########*/

for( i=0;i<100;++i )

{

sleep( 1 ); //设置成1s就会看到 a/b交替出现,为0则a和b连续出现

//grab the semaphore

sb.sem_num=0;

sb.sem_op=-1;

sb.sem_flg=0;

if( semop( block->semID,&sb,1 )!=-1 )

{

//write the letter to the segment buffer

//this is our CRITICAL SECTION

block->string[ block->counter++ ]=user;

sb.sem_num=0;

sb.sem_op=1;

sb.sem_flg=0;

if( semop( block->semID,&sb,1 )==-1 )

printf( "Failed to release the semaphore/n" );

}

else

printf( "Failed to acquire the semaphore/n" );

}

//do some clear work

ret=shmdt(( void*)block);

}

else if( !strncmp(argv[ 1 ],"read",4) )

{

//here we will read the buffer in the shared segment

shmid=shmget( MY_SHM_ID,0,0 );

if( shmid!=-1 )

{

block=( MY_BLOCK_T* )shmat( shmid,( const void* )0,0 );

block->string[ block->counter+1 ]=0;

printf( "%s/n",block->string );

printf( "Length=%d/n",block->counter );

ret=shmdt( ( void*)block );

}

else

printf( "Unable to read segment/n" );

}

else if( !strncmp(argv[ 1 ],"remove",6) )

{

shmid=shmget( MY_SHM_ID,0,0 );

if( shmid>=0 )

{

block=( MY_BLOCK_T* )shmat( shmid,( const void* )0,0 );

//remove the semaphore

ret=semctl( block->semID,0,IPC_RMID );

if( ret==0 )

printf( "Successfully remove the semaphore /n" );

//remove the shared segment

ret=shmctl( shmid,IPC_RMID,0 );

if( ret==0 )

printf( "Successfully remove the segment /n" );

}

}

else

printf( "Unkonw command/n" );

}

return 0;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值