转自:http://blog.csdn.net/pcliuguangtao/article/details/6526119/
共享内存是LUNIX 系统中最底层的通信机制,也是最快速的通信机制。共享内存通过两个或多个进程共享同一块内存区域来实现进程间的通信。通常是由一个进程创建一块共享内存区域,然后多个进程可以对其进行访问,一个进程将要传出的数据存放到共享内存中,另一个或多个进程则直接从共享内存中读取数据。因此这种通信方式是最高效的进程间通信方式。但实际的问题在于,当两个或多个进程使用共享内存进行通信时,同步问题的解决显得尤为重要,否则就会造成因不同进程同时读写一块共享内存中的数据而发生混乱。在通常的情况下,通过使用信号量来实现进程的同步。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include
- #include //getpagesize( )
- #include
- #include
- #define MY_SHM_ID 67483
- int main( )
- {
-
- printf( "page size=%d/n",getpagesize( ) );
-
- int shmid,ret;
- shmid=shmget( MY_SHM_ID,4096,0666|IPC_CREAT );
-
-
- if( shmid>0 )
- printf( "Create a shared memory segment %d/n",shmid );
-
- struct shmid_ds shmds;
-
- ret=shmctl( shmid,IPC_STAT,&shmds );
- if( ret==0 )
- {
- printf( "Size of memory segment is %d/n",shmds.shm_segsz );
- printf( "Numbre of attaches %d/n",( int )shmds.shm_nattch );
- }
- else
- {
- printf( "shmctl( ) call failed/n" );
- }
-
- ret=shmctl( shmid,IPC_RMID,0 );
- if( ret==0 )
- printf( "Shared memory removed /n" );
- else
- printf( "Shared memory remove failed /n" );
- return 0;
- }
-
-
-
- #include
- #include
- #include
- #include
- #define MY_SHM_ID 67483
- int main( )
- {
-
- int shmid,ret;
- void* mem;
- shmid=shmget( MY_SHM_ID,0,0 );
- if( shmid>=0 )
- {
- mem=shmat( shmid,( const void* )0,0 );
-
- if( ( int )mem!=-1 )
- {
- printf( "Shared memory was attached in our address space at %p/n",mem );
-
- strcpy( ( char* )mem,"This is a test string./n" );
- printf( "%s/n",(char*)mem );
-
- ret=shmdt( mem );
- if( ret==0 )
- printf( "Successfully detached memory /n" );
- else
- printf( "Memory detached failed %d/n",errno );
- }
- else
- printf( "shmat( ) failed/n" );
-
- }
- else
- printf( "shared memory segment not found/n" );
- return 0;
- }
-
-
-
-
-
-
- int shmid;
-
- shmid=shmget( MY_SHM_ID,0,0 );
- ret=shmctl( shmid,SHM_LOCK,0 );
- if( ret==0 )
- printf( "Locked!/n" );
-
-
-
-
-
-
-
-
-
-
- #include
- #include
- #include
- #include
- #include
- #include
- #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;
-
- if( argc>=2 )
- {
-
-
- if( !strncmp(argv[ 1 ],"create",6) )
- {
-
- 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;
-
- 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 );
-
- shmdt( ( void* )block );
- printf( "Create the shared memory and semaphore successuflly/n" );
-
- }
- else if( !strncmp(argv[ 1 ],"use",3) )
- {
-
-
- if( argc<3 ) exit( -1 );
- user=( char )argv[ 2 ][ 0 ];
-
- 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 );
-
- sb.sem_num=0;
- sb.sem_op=-1;
- sb.sem_flg=0;
- if( semop( block->semID,&sb,1 )!=-1 )
- {
-
-
- 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" );
- }
-
-
- ret=shmdt(( void*)block);
-
- }
- else if( !strncmp(argv[ 1 ],"read",4) )
- {
-
- 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 );
-
- ret=semctl( block->semID,0,IPC_RMID );
- if( ret==0 )
- printf( "Successfully remove the semaphore /n" );
-
- ret=shmctl( shmid,IPC_RMID,0 );
- if( ret==0 )
- printf( "Successfully remove the segment /n" );
- }
- }
- else
- printf( "Unkonw command/n" );
- }
- return 0;
-
- }
++++++++++++++++++++++++++++++++++++++++++
以下两个程序是一个进程间通信的例子。这两个程序分别在不同的进程中运行,使用了共享内存进行通信。b从键盘读入数据,存放在共享内存中。a则从共享内存中读取数据,显示到屏幕上。由于没有使两个进程同步,显示的内容将是杂乱无章的.实例b程序负责向共享内存中写入数据,a程序负责从内存中读出共享的数据,它们之间并没有添加同步操作。
b.c
#include
#include
#include
#include
#define BUF_SIZE 1024
#define MYKEY 25
int main()
{
int shmid;
char *shmptr;
if((shmid = shmget(MYKEY,BUF_SIZE,IPC_CREAT)) ==-1)
{
printf("shmget error \n");
exit(1);
}
if((shmptr =shmat(shmid,0,0))==(void *)-1)
{
printf("shmat error!\n");
exit(1);
}
while(1)
{
printf("input:");
scanf("%s",shmptr);
}
exit(0);
}
=======================================
a.c
#include
#include
#include
#include
#define BUF_SIZE 1024
#define MYKEY 25
int main()
{
int shmid;
char * shmptr;
if((shmid = shmget(MYKEY,BUF_SIZE,IPC_CREAT)) ==-1)
{
printf("shmget error!\n");
exit(1);
}
if((shmptr = shmat(shmid,0,0)) == (void *)-1)
{
printf("shmat error!\n");
exit(1);
}
while(1)
{
printf("string :%s\n",shmptr);
sleep(3);
}
exit(0);
}