linux两个程序通过共享内存通信的一个简单例子

239 篇文章 2 订阅

写共享内存程序:

 

  1. /*  
  2.  * File:   server.cpp 
  3.  * Author: centos 
  4.  *说明:从键盘读入数据,存放在共享内存中。 
  5.  * Created on 2010年3月1日, 下午3:44 
  6.  */  
  7.   
  8. #include <stdlib.h>  
  9. #include <stdio.h>  
  10. #include <string.h>  
  11. #include <errno.h>  
  12.   
  13. #include <sys/types.h>  
  14. #include <sys/ipc.h>  
  15. #include <sys/shm.h>  
  16. #define BUF_SIZE 1024  
  17. #define MYKEY 114  
  18.   
  19.   
  20. int main(int argc, char** argv)  
  21. {  
  22.   
  23.     int shmid;  
  24.     char *shmptr;  
  25.     struct shmid_ds shmbuf;  
  26.     system("ipcrm -M114"); //调试程序时用  
  27.     shmid = shmget(MYKEY, BUF_SIZE, (IPC_CREAT |0777) );  
  28.     if ( -1 == shmid )  
  29.     {  
  30.         printf("server shmget error!/n");  
  31.         fprintf(stderr, "Error: %d - %s/n", errno, strerror(errno));  
  32.         exit(1);  
  33.     }  
  34.   
  35.     shmptr = (char *) (shmat(shmid, 0, 0)) ;  
  36.     if ( -1 == (int) shmptr )  
  37.     {  
  38.         printf("server shmat error!/n");  
  39.         fprintf(stderr, "Error: %d - %s/n", errno, strerror(errno));  
  40.         if(shmctl(shmid,IPC_RMID,&shmbuf) < 0)   
  41.         {  
  42.             perror("shmctl error");  
  43.             fprintf(stderr, "Error: %d - %s/n", errno, strerror(errno));  
  44.         }  
  45.         exit(1);  
  46.     }  
  47.     strcpy(shmptr,"this is a test. /n");  
  48.     while(1)  
  49.     {  
  50.         printf("Please input:");  
  51.         scanf("%s",shmptr) ;  
  52.         while ('/n' != getchar() );  
  53. //        fflush(stdin);  
  54. //        printf("your input: %s/n", shmptr);  
  55.         if (! strcmp(shmptr,"quit")) break;  
  56.     }  
  57.   
  58.   
  59.     shmdt(shmptr);  
  60.     if(shmctl(shmid,IPC_RMID,&shmbuf) < 0) perror("shmctl error");  
  61.     return (EXIT_SUCCESS);  
  62. }  

 

 

 

 

 

 

 

读共享内存的程序:

/*
 * File:   main.cpp
 * Author: centos
 *
说明:从共享内存中读取数据,显示到屏幕上。
 * Created on 2010年3月2日, 上午10:47
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <errno.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define BUF_SIZE 1024
#define MYKEY 114

/*
 *
 */
int main(int argc, char** argv)
{
    int shmid;
    char *shmptr;
    struct shmid_ds shmbuf;
    char last_str[1024];

    if ( ( shmid = shmget(MYKEY, BUF_SIZE, IPC_CREAT) ) == -1 )
    {
        printf("clinet shmget error!/n");
        exit(1);
    }
    
    shmptr = (char *) shmat(shmid, 0, 0);
    if ( -1 == (int) shmptr )
    {
        printf("clinet shmat error!/n");
        fprintf(stderr, "Error: %d - %s/n", errno, strerror(errno));
        exit(1);
    }

    while(1)
    {
        
//        if((strlen(last_str) != strlen(shmptr)) ||
        if( ( 0 != strcmp(last_str,shmptr)))
        {
            printf("%s  %s/n", last_str,shmptr);
            strcpy(last_str,shmptr);
        }
        if (! strcmp(shmptr,"quit")) break;        
    }
    shmdt(shmptr);
    if(shmctl(shmid,IPC_RMID,&shmbuf) < 0) perror("Close 共享内存出错shmctl error");
    return (EXIT_SUCCESS);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值