首页 > 系统服务 > 详细

共享内存实现进程间通信

时间:2014-12-30 18:40:37      阅读:349      评论:0      收藏:0      [点我收藏+]
#include "stdio.h"
#include "string.h"
#include "unistd.h"
#include "sys/shm.h"
 
int main()
{
    int id = shmget(0x8888, 100, IPC_CREAT|0644);
    if (id==-1)
    {
        perror("Error");
        return -1;
    }
    char *p = shmat(id, NULL, 0);
    if (p == ((char*)-1))
    {
        perror("Error");
        return -1;
    }
    strcpy(p, "Hello Shared Memory!");
    shmdt(p);
 
    return 0;
}
 
 
#include "stdio.h"
#include "string.h"
#include "unistd.h"
#include "sys/shm.h"
 
int main()
{
    int id = shmget(0x8888, 0, 0);
    if (id==-1)
    {
        perror("get Error");
        return -1;
    }
    char *p = shmat(id, NULL, SHM_RDONLY);
    if ( p == ((char*)-1))
    {
        perror("At Error");
        return -1;
    }
    puts(p);
    shmdt(p);
    shmctl(id, IPC_RMID, 0);
    return 0;
}

共享内存实现进程间通信

原文:http://www.cnblogs.com/zhangwuliang/p/4193935.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!