Chinaunix首页 | 论坛 | 博客
  • 博客访问: 216970
  • 博文数量: 38
  • 博客积分: 2060
  • 博客等级: 大尉
  • 技术积分: 388
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-17 10:10
文章分类

全部博文(38)

文章存档

2011年(1)

2009年(37)

我的朋友

分类: LINUX

2009-08-21 11:27:41

具体函数参看man手册
 
server
 

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <signal.h>

void sig_int(int sig)
{
    printf("quit?! no door!\n");
    exit(0);
};

int main()
{
    signal(SIGINT, sig_int);
    key_t key = ftok("/home/ddb/13.jpg", 1);
    int shm_no;
    if((shm_no=shmget(key, 1024, 0644 | IPC_CREAT | IPC_EXCL)) == -1)
    {
        printf("shnget error, %s\n", strerror(errno));
    }

    char *p = NULL;
    if((p = (char *)shmat(shm_no, NULL, 0)) == -1)
    {
        printf("shmat error, %s\n", strerror(errno));
    }

    while(1)
    {
        gets(p);
    }
    return 0;
}

 

client

 

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>

int main()
{
    key_t key = ftok("/home/ddb/13.jpg", 1);
    int shm_no;
    if((shm_no=shmget(key, 1024, 0)) == -1)
    {
        printf("shnget error, %s\n", strerror(errno));
    }

    char *p = NULL;
    if((p = (char *)shmat(shm_no, NULL, 0)) == -1)
    {
        printf("shmat error, %s\n", strerror(errno));
    }

    while(1)
    {
        printf("p = %s\n", p);
        if(strcmp(p, "quit") == 0)
        {
            break;
        }
        sleep(1);
    }

    shmctl(shm_no, IPC_RMID, NULL);    
    return 0;
}

阅读(1252) | 评论(0) | 转发(0) |
0

上一篇:信号量的使用

下一篇:unix的标准化及实现

给主人留下些什么吧!~~