📄 linux_shmem.c
字号:
#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 (\u201cshared memory attached at address %p\n\u201d, shared_memory); /* Determine the segment\u2019s size. */ shmctl (segment_id, IPC_STAT, &shmbuffer); segment_size = shmbuffer.shm_segsz; printf (\u201csegment size: %d\n\u201d, segment_size); /* Write a string to the shared memory segment. */ sprintf (shared_memory, \u201cHello, world.\u201d); /* 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 (\u201cshared memory reattached at address %p\n\u201d, shared_memory); /* Print out the string from shared memory. */ printf (\u201c%s\n\u201d, shared_memory); /* Detach the shared memory segment. */ shmdt (shared_memory); /* Deallocate the shared memory segment. */ shmctl (segment_id, IPC_RMID, 0); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -