zoukankan      html  css  js  c++  java
  • IPC:shared memory

    #include <stdio.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    int main ()
    {
    int segment_id;
    char* shared_memory;
    struct shmid_ds shmbuffer;
    int segment_size;
    const int shared_segment_size = 0x6400;
    /* Allocate a shared memory segment. */
    segment_id = shmget (IPC_PRIVATE, shared_segment_size,
    IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
    /* Attach the shared memory segment. */
    shared_memory = (char*) shmat (segment_id, 0, 0);
    printf (“shared memory attached at address %p
    ”, shared_memory);
    /* Determine the segment’s size. */
    shmctl (segment_id, IPC_STAT, &shmbuffer);
    segment_size = shmbuffer.shm_segsz;
    printf (“segment size: %d
    ”, segment_size);
    /* Write a string to the shared memory segment. */
    sprintf (shared_memory, “Hello, world.”);
    /* Detach the shared memory segment. */
    shmdt (shared_memory);
    /* Reattach the shared memory segment, at a different address. */
    shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0);
    printf (“shared memory reattached at address %p
    ”, shared_memory);
    /* Print out the string from shared memory. */
    printf (“%s
    ”, shared_memory);
    /* Detach the shared memory segment. */
    shmdt (shared_memory);
    /* Deallocate the shared memory segment.
    shmctl (segment_id, IPC_RMID, 0);
    */
    return 0;
    }
  • 相关阅读:
    OpenCV特征描述
    OpenCV特征点检测
    expect实现无交互操作
    文件的修改时间
    sshd登录攻击
    tcp三次握手和syn 洪水攻击
    vim使用
    PHP拓展开发
    【转】LINUX 手动建立SWAP文件及删除
    Ubuntu下crontab命令的用法
  • 原文地址:https://www.cnblogs.com/feika/p/3614554.html
Copyright © 2011-2022 走看看