进程间通信——共享内存

//shmdata.h

//test if define
#ifndef _SHMDATA_H_HEADER
#define _SHMDATA_H_HEADER

#define TEXT_SIZE 100
struct shared_use_set
{
    int readed;//为1时可写入,为0时不可写入
    char text[TEXT_SIZE];
};
#endif

//shmread.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/shm.h>
#include<stdlib.h>
#include"shmdata.h"
int main(){
    struct shared_use_set *shared;
    int running=1;
    void *shm=NULL;
    int shmid;
    shmid=shmget((key_t)1357,sizeof(struct shared_use_set),0666|IPC_CREAT);//shmget is for create share place,which return sharedid,1357 is to sign it
    if(shmid==-1){//test if create succeed
        fprintf(stderr,"shmget failed\n");//if can't create print this
        exit(EXIT_FAILURE);
    }
    shm=shmat(shmid,0,0);//return shared place addr,the first 0 is for system choose addr,another always is 0
    if(shm==(void*)-1){
        fprintf(stderr,"shmat failed\n");
        exit(EXIT_FAILURE);
    }
    printf("\nMemory attached at %X\n",(int)shm);
    shared=(struct shared_use_set*)shm;//set share place
    shared->readed=1;//can write
    while(running)
    {
        if(shared->readed!=1)//can't write,so can printf
        {
            printf("you wrote: %s",shared->text);
            sleep(rand()%3);
            shared->readed=1;//turn to write status
            if(strncmp(shared->text,"end",3)==0)//when input end ,exit while
            {
                running=0;
            }
        }
        else
            sleep(1);

    }
    if(shmdt(shm)==-1)
    {
        fprintf(stderr,"shmdt failed\n");
        exit(EXIT_FAILURE);
    }
    if(shmctl(shmid,IPC_RMID,0)==-1)//del
    {
        fprintf(stderr,"shmctl failed\n");
        exit(EXIT_FAILURE);
    }
return 0;
}

//shmwrite.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include"shmdata.h"
int main()
{   
    struct shared_use_set *shared;
    int running=1;
    char buf[2048];
    void * shm;
    int shmid;
    shmid=shmget((key_t)1357,sizeof(struct shared_use_set),0666|IPC_CREAT); 
    if(shmid==-1)
    {
        perror("shmget faild\n");   
        exit(1);
    }
    shm=shmat(shmid,0,0);
    if(shm==(void*)-1)
    {
        perror("shmat faild\n");
        exit(1);
    }
    shared=(struct shared_use_set*)shm;
    while(running)
    {
        while(shared->readed==0)//can't write,wait for write
        {
            sleep(1);
            printf("please waiting...\n");
        }
        printf("input some text:");
        fgets(buf,2048,stdin);//get string
        strncpy(shared->text,buf,2048);//put to shared->text
        shared->readed=0;//turn to read status
        if(strncmp(buf,"end",3)==0)
            running=0;
    }
    if(shmdt(shm)==-1)
    {
        perror("shmdt faild\n");
        exit(1);
    }
    sleep(2);
return 0;
}

my run result:
[root@bogon bp]# ./read &
[1] 41004
[root@bogon bp]#
Memory attached at 8C0CC000

[root@bogon bp]# ./write
input some text:linux
you wrote: linux
please waiting…
please waiting…
input some text:ok
you wrote: ok
please waiting…
please waiting…
input some text:end
you wrote: end
[1]+ Done ./read
[root@bogon bp]# ls

本文参考文章
http://blog.csdn.net/ljianhui/article/details/10253345

posted on 2017-10-25 18:06  标配的小号  阅读(157)  评论(0编辑  收藏  举报

导航