atshm.c

来自「基于网络编程的例子」· C语言 代码 · 共 45 行

C
45
字号
/* * atshm.c - Attaching a shared memory segment */#include <sys/types.h>#include <sys/ipc.h>#include <sys/shm.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){    int shmid; /* Segment ID */    char *shmbuf; /* Address in process */        /* Expect an segment id on the command-line */    if(argc != 2) {        puts("USAGE: atshm <identifier>");        exit(EXIT_FAILURE);    }    shmid = atoi(argv[1]);        /* Attach the segment */    if((shmbuf = shmat(shmid, 0, 0)) < (char *)0) {        perror("shmat");        exit(EXIT_FAILURE);    }    /* Where is it attached? */    printf("segment attached at %p\n", shmbuf);        /* See, we really are attached! */    system("ipcs -m");    /* Detach */    if((shmdt(shmbuf)) < 0) {        perror("shmdt");        exit(EXIT_FAILURE);    }    puts("segment detached");    /* Yep, we really did detach it */    system("ipcs -m");        exit(EXIT_SUCCESS);}

⌨️ 快捷键说明

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