How to use shared memory with Linux in C

前端 未结 5 1567
悲&欢浪女
悲&欢浪女 2020-11-22 08:13

I have a bit of an issue with one of my projects.

I have been trying to find a well documented example of using shared memory with fork() but to no suc

5条回答
  •  鱼传尺愫
    2020-11-22 09:10

    Here is an example for shared memory :

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define SHM_SIZE 1024  /* make it a 1K shared memory segment */
    
    int main(int argc, char *argv[])
    {
        key_t key;
        int shmid;
        char *data;
        int mode;
    
        if (argc > 2) {
            fprintf(stderr, "usage: shmdemo [data_to_write]\n");
            exit(1);
        }
    
        /* make the key: */
        if ((key = ftok("hello.txt", 'R')) == -1) /*Here the file must exist */ 
    {
            perror("ftok");
            exit(1);
        }
    
        /*  create the segment: */
        if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) == -1) {
            perror("shmget");
            exit(1);
        }
    
        /* attach to the segment to get a pointer to it: */
        data = shmat(shmid, NULL, 0);
        if (data == (char *)(-1)) {
            perror("shmat");
            exit(1);
        }
    
        /* read or modify the segment, based on the command line: */
        if (argc == 2) {
            printf("writing to segment: \"%s\"\n", argv[1]);
            strncpy(data, argv[1], SHM_SIZE);
        } else
            printf("segment contains: \"%s\"\n", data);
    
        /* detach from the segment: */
        if (shmdt(data) == -1) {
            perror("shmdt");
            exit(1);
        }
    
        return 0;
    }
    

    Steps :

    1. Use ftok to convert a pathname and a project identifier to a System V IPC key

    2. Use shmget which allocates a shared memory segment

    3. Use shmat to attache the shared memory segment identified by shmid to the address space of the calling process

    4. Do the operations on the memory area

    5. Detach using shmdt

提交回复
热议问题