⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 创建&写共享内存.txt

📁 创建写共享内存的例子
💻 TXT
字号:
/* Creat the share memory */

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

#define PERM S_IRUSR|S_IWUSR
#define SIZE 512

/*typedef struct shmtype{*/      /* define the type of data stored in shmem */
    /*char name[4];*/
    /*int age;*/
/*}animal;*/

union semun{                /* define the union for the fuction semctl */
    int val;
    struct semid_ds * buf;
    ushort_t * array;
}sem;

int semcre(key_t key){      /* the function to creat the semaphore */
    union semun sem;
    int semid;
    sem.val=0;
    semid=semget(key,1,IPC_CREAT|PERM);
    if(semid==-1){
        printf("Creat Semaphore Error!\n");
        exit(-1);
    }
    printf("Creat Semaphore ok!\n");
    semctl(semid,0,SETVAL,sem);   /* initiate the semaphore */
    return semid;
}

void d_sem(int semid){
    union semun sem;
    sem.val=0;
    semctl(semid,0,IPC_RMID,0);
}

int p(int semid){
    struct sembuf sops={0,+1,IPC_NOWAIT};
    return(semop(semid,&sops,1));
}

int v(int semid){                /* the handle for process that release shmem */
    struct sembuf sops={0,-1,IPC_NOWAIT};
    return(semop(semid,&sops,1));
}

int main( int argc, char **argv ){
    key_t key;
    int shmid,semid,reval,i;
    int  num = 0;
    int a=0;
    /*int init=4;*/
    /*char temp='a';*/
    struct shmid_ds buf;
    /*animal* shm;*/
    char *shm;
    char * pathname="/home/agent/src/ShMemCre.c";

    key=ftok(pathname,'a');
    if(key==-1){
     fprintf(stderr,"Creat Key Error:[%s]\n",strerror(errno));
        exit(-1);
    }

    shmid=shmget(key,SIZE,IPC_CREAT|PERM);
    if(shmid==-1){
        fprintf(stderr,"Creat share memory error:[%s]\n",strerror(errno));
        exit(-1);
    }

    fprintf(stderr,"Creat share memory ok:[%d]\n",shmid);

    /*shm=(animal * )shmat(shmid,0,0);*/
    shm=(char *)shmat(shmid,0,0);

    if(shm==(char *)-1){
        fprintf(stderr,"Attach Shared Memory Error:[%s]\n",strerror(errno));
        exit(-1);
    }

    fprintf(stderr,"attach share memory addr:[%x]\n",shm);

    semid=semcre(key);
     /*p(semid);*/
    /*strcpy(shm,"test");*/
    /*v(semid);*/

    num = atoi(argv[1]);
    for(i=num;i<=num+4;i++){
        sleep(1);
        p(semid);
        /*temp+=1;*/
        /*init=4+i;*/
        /*memcpy((*(shm+i)).name,&temp,1);*/
        /*memcpy((*(shm+i)).age,&init,1);*/
        a++;
        printf("copy data %d!\n",a);
        /*memcpy((shm+i),&a,1);*/
        *(shm+i)=a;
        printf("copy data %d!\n",*(shm+i));
        /* sleep(30); */
        v(semid);
    }


    if(shmdt(shm)==-1){
        printf("detach Shared Memory Error!\n");
        return -1;
    }
    sleep( 120 );

    reval=shmctl(shmid,IPC_RMID,&buf);
    if(reval<0){
        printf("Delete Shared Memory Error!\n");
        return -1;
    }
    else{
        printf("Shared Memory is deleted!\n");
        d_sem(semid);
        return 0;
    }
}

   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -