Linux嵌入式-共享内存作业(共6页).doc

上传人:飞****2 文档编号:8529577 上传时间:2022-03-19 格式:DOC 页数:6 大小:24KB
返回 下载 相关 举报
Linux嵌入式-共享内存作业(共6页).doc_第1页
第1页 / 共6页
Linux嵌入式-共享内存作业(共6页).doc_第2页
第2页 / 共6页
点击查看更多>>
资源描述

《Linux嵌入式-共享内存作业(共6页).doc》由会员分享,可在线阅读,更多相关《Linux嵌入式-共享内存作业(共6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精选优质文档-倾情为你奉上共享内存作业内容:共享内存实验中,P275代码不能正确运行。请修改代码,并分析原因。代码:1、/* sem_com.h */#ifndefSEM_COM_H#defineSEM_COM_H#include #include union semunint val;struct semid_ds *buf;unsigned short *array;int init_sem(int, int);int del_sem(int);int sem_p(int);int sem_v(int);#endif /* SEM_COM_H */2、/* shm_com.h */#ifn

2、defSHM_COM_H#defineSHM_COM_H#include #include #include #include #include #include #include #define SHM_BUFF_SZ 2048struct shm_buffint pid;char bufferSHM_BUFF_SZ;#endif /* SHM_COM_H */3、/* sem_com.c */#include sem_com.hint init_sem(int sem_id, int init_value)union semun sem_union;sem_union.val = init

3、_value;if (semctl(sem_id, 0, SETVAL, sem_union) = -1)perror(Initialize semaphore);return -1;return 0;int del_sem(int sem_id)union semun sem_union;if (semctl(sem_id, 0, IPC_RMID, sem_union) = -1)perror(Delete semaphore);return -1; int sem_p(int sem_id)struct sembuf sem_b;sem_b.sem_num = 0; /*id*/sem_

4、b.sem_op = -1; /* P operation*/sem_b.sem_flg = SEM_UNDO;if (semop(sem_id, &sem_b, 1) = -1) /*1:first struct*/perror(P operation);return -1;return 0;int sem_v(int sem_id)struct sembuf sem_b;sem_b.sem_num = 0; /* id */sem_b.sem_op = 1; /* V operation */sem_b.sem_flg = SEM_UNDO; /*Its tracks be follow,

5、 to automatical free for it*/if (semop(sem_id, &sem_b, 1) = -1)perror(V operation);return -1;return 0;4、/* producer.c */#include shm_com.h#include sem_com.h#include int ignore_signal(void)signal(SIGINT, SIG_IGN);signal(SIGSTOP, SIG_IGN);signal(SIGQUIT, SIG_IGN);return 0;int main()void *shared_memory

6、 = NULL;struct shm_buff *shm_buff_inst;char bufferBUFSIZ;int shmid, semid;ignore_signal(); /* 防止程序非正常退出 */semid = semget(ftok(., a), 1, 0666|IPC_CREAT); /* 创建一个信号量*/init_sem(semid, 1);/* 初始值为1 */* 创建共享内存 */shmid = shmget(ftok(., b), sizeof(struct shm_buff), 0666|IPC_CREAT);if (shmid = -1)perror(shmg

7、et failed);del_sem(semid);exit(1);/* 将共享内存地址映射到当前进程地址空间 */shared_memory = shmat(shmid, (void*)0, 0);if (shared_memory = (void*)-1)perror(shmat);del_sem(semid);exit(1);printf(Memory attached at %Xn, (int)shared_memory);/* 获得共享内存的映射地址 */shm_buff_inst = (struct shm_buff *)shared_memory;dosem_p(semid);p

8、rintf(Enter some text to the shared memory(enter quit to exit):);/* 向共享内存写入数据 */if (fgets(shm_buff_inst-buffer, SHM_BUFF_SZ, stdin) = NULL)perror(fgets);sem_v(semid);break;shm_buff_inst-pid = getpid();sem_v(semid); while(strncmp(shm_buff_inst-buffer, quit, 4) != 0);/* 删除信号量 */del_sem(semid);/* 删除共享内

9、存到当前进程地址空间中的映射 */if (shmdt(shared_memory) = 1)perror(shmdt);exit(1);exit(0);5、/* customer.c */#include shm_com.h#include sem_com.hint main()void *shared_memory = NULL;struct shm_buff *shm_buff_inst;int shmid, semid;/* 获得信号量 */semid = semget(ftok(., a), 1, 0666);if (semid = -1)perror(Producer isnt ex

10、ist);exit(1);/* 获得共享内存 */shmid = shmget(ftok(., b), sizeof(struct shm_buff), 0666|IPC_CREAT);if (shmid = -1)perror(shmget);exit(1);/* 将共享内存地址映射到当前进程地址空间 */shared_memory = shmat(shmid, (void*)0, 0); if (shared_memory = (void*)-1)perror(shmat);exit(1);printf(Memory attached at %Xn, (int)shared_memory)

11、;/* 获得共享内存的映射地址 */shm_buff_inst = (struct shm_buff *)shared_memory;dosem_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);专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 教育教学

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁