微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 进程间通信之:实验内容

进程间通信之:实验内容

时间:08-13 来源:3721RD 点击:

exit(1);

}

/* 将共享内存地址映射到当前进程地址空间 */

shared_memory = shmat(shmid, (void*)0, 0);

if (shared_memory == (void*)-1)

{

perror("shmat");

exit(1);

}

printf("Memory attached at %X\n", (int)shared_memory);

/* 获得共享内存的映射地址 */

shm_buff_inst = (struct shm_buff *)shared_memory;

do

{

sem_p(semid);

printf("Shared memory was written by process %d :%s"

, shm_buff_inst->pid, shm_buff_inst->buffer);

if (strncmp(shm_buff_inst->buffer, "quit", 4) == 0)

{

break;

}

shm_buff_inst->pid = 0;

memset(shm_buff_inst->buffer, 0, SHM_BUFF_SZ);

sem_v(semid);

} while(1);

/* 删除共享内存到当前进程地址空间中的映射 */

if (shmdt(shared_memory) == -1)

{

perror("shmdt");

exit(1);

}

/* 删除共享内存 */

if (shmctl(shmid, IPC_RMID, NULL) == -1)

{

perror("shmctl(IPC_RMID)");

exit(1);

}

exit(0);

}

4.实验结果

$./producer

Memory attached at B7F90000

Enter some text to the shared memory(enter 'quit' to exit):First message

Enter some text to the shared memory(enter 'quit' to exit):Second message

Enter some text to the shared memory(enter 'quit' to exit):quit

$./customer

Memory attached at B7FAF000

Shared memory was written by process 3815 :First message

Shared memory was written by process 3815 :Second message

Shared memory was written by process 3815 :quit

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top