Linux 共享内存编程

1.// 创建或获取一个共享内存:成功返回共享内存ID,失败返回-1
   int shmget(key_t key, size_t size, int flag);

 2.// 连接共享内存到当前进程的地址空间:成功返回指向共享内存的指针,失败返回-1
   void *shmat(int shm_id, const void *addr, int flag);
 
 3.// 断开与共享内存的连接:成功返回0,失败返回-1
   int shmdt(void *addr);
  
 4.// 控制共享内存的相关信息:成功返回0,失败返回-1
   int shmctl(int shm_id, int cmd, struct shmid_ds *buf);
#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<stdlib.h>
#include<string.h>
int main()
{
        char *shmaddr;

        key_t key;
        key = ftok(".",1);

        int shmid = shmget(key,1024*4,IPC_CREAT|0666);
        if(shmid == -1){
                printf("shmget noOK\n");
                exit(-1);
        }

        shmaddr = shmat(shmid,0,0);

        printf("shmat ok\n");
        strcpy(shmaddr,"welcome to beijing");

        sleep(10);
        shmdt(shmaddr);
        shmctl(shmid,IPC_RMID,0);

        printf("quit\n");


        return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<stdlib.h>
#include<string.h>
int main()
{
        char *shmaddr;

        key_t key;
        key = ftok(".",1);

        int shmid = shmget(key,1024*4,0);
        if(shmid == -1){
                printf("shmget noOK\n");
                exit(-1);
        }

        shmaddr = shmat(shmid,0,0);

        printf("shmat ok\n");
        printf("data:%s\n",shmaddr);


        shmdt(shmaddr);


        printf("quit\n");


        return 0;
}

ipcs -m  查看共享内存
ipcrm -m + 共享内存ID号  删除共享内存
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux共享内存编程是指在Linux操作系统中使用共享内存实现进程间通信的编程技术。共享内存是一种特殊的内存区域,它可以被多个进程同时访问,从而实现进程间数据的共享。 在Linux中,使用共享内存需要经过以下几个步骤: 1. 创建共享内存区域:使用shmget()函数创建一个共享内存区域,并返回该区域的标识符。 2. 连接共享内存区域:使用shmat()函数将进程与共享内存区域连接起来,并返回连接后的内存地址。 3. 使用共享内存:进程可以使用连接后的内存地址读写共享内存中的数据。 4. 分离共享内存区域:使用shmdt()函数将进程与共享内存区域分离,使得进程不能再访问该内存区域。 5. 删除共享内存区域:使用shmctl()函数删除共享内存区域。 下面是一个使用共享内存实现进程间通信的例子: ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include <sys/shm.h> #define SHM_SIZE 1024 int main() { int shmid; key_t key; char *shm, *s; // 生成共享内存key值 key = ftok(".", 'a'); // 创建共享内存区域 if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } // 连接共享内存区域 if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } // 写入共享内存数据 s = shm; for (char c = 'a'; c <= 'z'; c++) { *s++ = c; } *s = '\0'; // 分离共享内存区域 if (shmdt(shm) == -1) { perror("shmdt"); exit(1); } // 重新连接共享内存区域 if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } // 读取共享内存数据 printf("Shared memory content: "); for (s = shm; *s != '\0'; s++) { putchar(*s); } putchar('\n'); // 分离共享内存区域 if (shmdt(shm) == -1) { perror("shmdt"); exit(1); } // 删除共享内存区域 if (shmctl(shmid, IPC_RMID, 0) == -1) { perror("shmctl"); exit(1); } return 0; } ``` 这个例子中,程序通过ftok()函数生成一个共享内存的key值,然后使用shmget()函数创建共享内存区域,使用shmat()函数将进程与共享内存区域连接起来,然后向共享内存中写入数据,再使用shmdt()函数分离共享内存区域,重新连接共享内存区域并读取数据,最后使用shmctl()函数删除共享内存区域。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值