zoukankan      html  css  js  c++  java
  • 共享内存示例代码如下:

    //共享内存示例代码如下:

    #include <stdio.h>
    #include <sys/sem.h>
    #include <sys/ipc.h>
    #include <string.h>

    typedef int sem_t;
    union semun {
    int val;
    struct semid_ds *buf;
    unsigned short *array;
    } arg;
    sem_t CreateSem(key_t key, int value)
    {
    union semun sem;
    sem_t semid;
    sem.val = value;

    semid = semget(key,value,IPC_CREAT|0666);
    if (-1 == semid)
    {
    printf(“create semaphore error\n”);
    return -1;
    }

    semctl(semid,0,SETVAL,sem);

    return semid;
    }
    /*
     * struct sembuf{
     * ushort sem_num;
     * short  sem_op;
     * short  sem_flg;
     * };
     * */
    void SetvalueSem(sem_t semid, int value)
    {
    union semun sem;
    sem.val = value;

    semctl(semid,0,SETVAL,sem);

    return ;
    }

    int GetvalueSem(sem_t semid)
    {
    union semun sem;

    return semctl(semid,0,GETVAL,sem);

    return sem.val;
    }

    void DestroySem(sem_t semid)
    {
    union semun sem;
    sem.val = 0;

    semctl(semid,0,IPC_RMID,sem);
    }

    int Sem_P(sem_t semid)
    {
    struct sembuf sops={0,+1,IPC_NOWAIT};

    return (semop(semid,&sops,1));
    }

    int Sem_V(sem_t semid)
    {
    struct sembuf sops={0,-1,IPC_NOWAIT};

    return (semop(semid,&sops,1));
    }

    static char msg[]=”你好,共享内存\n”;

    int main(void)
    {
    key_t key;
    int semid,shmid;
    char i,*shms,*shmc;
    struct semid_ds buf;
    int value = 0;
    char buffer[80];
    pid_t p;

    key = ftok(“/ipc/sem/”,’a');
    shmid = shmget(key,1024,IPC_CREAT|0604);

    semid = CreateSem(key,1);

    p = fork();
    if(p > 0)
    {
    /* 父进程 */
    /* 建立共享内存 */
    shms = (char *)shmat(shmid,0,0);

    memcpy(shms, msg, strlen(msg)+1);
    sleep(10);
    Sem_P(semid);
    shmdt(shms);

    DestroySem(semid);
    }
    else if(p == 0)
    {
    shmc = (char *)shmat(shmid,0,0);
    Sem_V(semid);
    printf(“共享内存的值为:%s\n”,shmc);
  • 相关阅读:
    hdu 1108 最小公倍数
    hdu 1106 排序
    hdu 1097 A hard puzzle
    hdu 1076 An Easy Task
    hdu 1064 Financial Management
    hdu 1061 Rightmost Digit
    hdu 1050 Moving Tables
    hdu 1060 Leftmost Digit
    hdu 1049 Climbing Worm
    hdu1104
  • 原文地址:https://www.cnblogs.com/yuzaipiaofei/p/4124388.html
Copyright © 2011-2022 走看看