|
楼主 |
发表于 2007-5-24 12:31:39
|
显示全部楼层
請問錯在哪裡...
不能將 紅色部分 share 出去...
shm_read..讀取不到 數字 (999)
shm_allocate.c
- #include <stdio.h>
- #include <sys/shm.h>
- #include <sys/stat.h>
- int main(int argc, char *argv[])
- {
- int seg_id;
- int seg_size = 0x4000;
- int *shared_memory;
- /* shared memory: allocate */
- seg_id = shmget(IPC_PRIVATE, seg_size,
- IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
- printf("Shared Memory Segment ID: %d\n", seg_id);
- /* shared memory: attach */
- shared_memory = (int *)shmat(seg_id, 0, 0);
-
- /* write to shared memory */
- //sprintf(shared_memory, argv[1]);
- [color=red]shared_memory = (int *)999;[/color]
- printf("Address: %p and Message:: %d\n",shared_memory, shared_memory);
- /* shared memory: detach */
- shmdt(shared_memory);
- exit(0);
- }
复制代码
shm_read.c
- #include <stdio.h>
- #include <sys/shm.h>
- #include <sys/stat.h>
- int main(int argc, char *argv[])
- {
- int seg_id;
- int *shared_memory;
- /* get shmid */
- seg_id = atoi(argv[1]);
- /* shared memory: attach */
- shared_memory = (int *)shmat(seg_id, 0, 0);
-
- /* read shared memory */
- for (;;) {
- printf("Address: %p and Message: %d\n", shared_memory, shared_memory);
- sleep(1);
- }
- /* shared memory: detach */
- shmdt(shared_memory);
- exit(0);
- }
复制代码 |
|