嵌入式共享内存作业.doc

上传人:scccc 文档编号:12116470 上传时间:2021-12-02 格式:DOC 页数:6 大小:39.50KB
返回 下载 相关 举报
嵌入式共享内存作业.doc_第1页
第1页 / 共6页
嵌入式共享内存作业.doc_第2页
第2页 / 共6页
嵌入式共享内存作业.doc_第3页
第3页 / 共6页
嵌入式共享内存作业.doc_第4页
第4页 / 共6页
嵌入式共享内存作业.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《嵌入式共享内存作业.doc》由会员分享,可在线阅读,更多相关《嵌入式共享内存作业.doc(6页珍藏版)》请在三一文库上搜索。

1、共享内存作业内容:共享内存实验中,P275代码不能正确运行。请修改代码,并分析原因代码:1、/* sem_com.h */#ifndefSEM_COM_H#defi neSEM_COM_H#i nclude <sys/ipc.h>#i nclude <sys/sem.h> union sem unint val;struct semid_ds *buf; un sig ned short *array;int ini t_sem(i nt, in t); int del_sem(i nt);int sem_p(i nt);int sem_v(i nt);#en dif

2、/* SEM_COM_H */2、/* shm_com.h */#ifndefSHM_COM_H#defi neSHM_COM_H#i nclude <uni std.h>#i nclude <stdlib.h>#i nclude <stdio.h>#include <string.h>#i nclude <sys/types.h>#i nclude <sys/ipc.h>#i nclude <sys/shm.h>#defi ne SHM_BUFF_SZ 20486 / 6struct shm_buffint

3、pid;char bufferSHM_BUFF_SZ; ;#endif /* SHM_COM_H */ 3、/* sem_com.c */#include "sem_com.h"int init_sem(int sem_id, int init_value)union semun sem_union; sem_union.val = init_value;if (semctl(sem_id, 0, SETVAL, sem_union) = -1) perror("Initialize semaphore"); return -1; return 0;in

4、t 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_b.sem_op = -1; /* P operation*/ sem_b.sem_flg = SEM_UNDO;if (semop(sem_id, &sem_b, 1

5、) = -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; /*It's tracks be follow, to automatical free for it*/if (semop(sem_id, &sem_b, 1) =

6、 -1)perror("V operation"); return -1;return 0;4、/* producer.c */#include "shm_com.h"#include "sem_com.h"#include <signal.h>int ignore_signal(void)signal(SIGINT, SIG_IGN); signal(SIGSTOP, SIG_IGN); signal(SIGQUIT, SIG_IGN); return 0;int main()void *shared_memory =

7、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_buf

8、f), 0666|IPC_CREAT);if (shmid = -1)perror("shmget 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)

9、;/* 获得共享内存的映射地址 */shm_buff_inst = (struct shm_buff *)shared_memory;dosem_p(semid);printf("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_buf

10、f_inst->pid = getpid();sem_v(semid); while(strncmp(shm_buff_inst->buffer, "quit", 4) != 0);/* 删除信号量 */ del_sem(semid);/* 删除共享内存到当前进程地址空间中的映射 */ if (shmdt(shared_memory) = 1) perror("shmdt"); exit(1);exit(0);5、/* customer.c */ #include "shm_com.h"#include "sem

11、_com.h" int 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 is'nt exist"); exit(1);/* 获得共享内存 */shmid = shmget(ftok(".", 'b'),

12、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);/* 获得共享内存的映

13、射地址 */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);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1