• 进程间通信——共享内存


    //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
    ");//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
    ");
            exit(EXIT_FAILURE);
        }
        printf("
    Memory attached at %X
    ",(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
    ");
            exit(EXIT_FAILURE);
        }
        if(shmctl(shmid,IPC_RMID,0)==-1)//del
        {
            fprintf(stderr,"shmctl failed
    ");
            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
    ");   
            exit(1);
        }
        shm=shmat(shmid,0,0);
        if(shm==(void*)-1)
        {
            perror("shmat faild
    ");
            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...
    ");
            }
            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
    ");
            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

  • 相关阅读:
    新建MDK工程
    winform如何打开电脑自带小软件
    Winform界面适应不同分辨率
    c#如何为pictureBox控件写单击事件
    winform如何获取记事本内容显示在listBox控件中
    winform如何单击X按钮弹出对话框
    win10系统如何使用自带的备份功能
    模拟iis账号权限
    (转发)在ASP.NET MVC中以post方式传递数组参数的示例
    .net开发windows服务小结 (转发)
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730655.html
Copyright © 2020-2023  润新知