为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

沫颖 的学生作业:

代码 #include #include #include #include #include #include #define SEM_PATHNAME "." #define SEM_SET_PRO_ID 50 // 公共体,用于初始化信号量 union semun { int val; /* Value for SETVAL */ }; int main(int argc, char const *argv[]) { key_t sem_key; int sem_id; // 生成唯一key sem_key = ftok(SEM_PATHNAME, SEM_SET_PRO_ID); if (sem_key == -1) { perror("[ERROR] ftok():"); exit(EXIT_FAILURE); } // 创建信号量集合 sem_id = semget(sem_key, 2, IPC_CREAT | 0666); if (sem_id == -1) { perror("[ERROR] semget():"); exit(EXIT_FAILURE); } // 打印系统中的信号量集合 system("ipcs -s"); // 初始化信号量中的数量 union semun sem_val1, sem_val2; sem_val1.val = 0; sem_val2.val = 1; int ret, ret1, ret2; // 初始化信号量集合中的第一个信号量 ret1 = semctl(sem_id, 0, SETVAL, sem_val1); ret2 = semctl(sem_id, 1, SETVAL, sem_val2); if (ret1 == -1 || ret2 == -1) { perror("[ERROR] semctl() SETVAL:"); exit(EXIT_FAILURE); } // 删除信号量集合 ret = semctl(sem_id, 0, IPC_RMID); if (ret == -1) { perror("[ERROR] semctl() IPC_RMID:"); exit(EXIT_FAILURE); } // 再次打印系统中的信号量集合 system("ipcs -s"); return 0; } 效果 felix@felixlinux:~/Desktop/Study/CProjects/imooc-embedded/Stage05/Week12/Class05$ ./a.out ------ Semaphore Arrays -------- key semid owner perms nsems 0xd2000c7c 1 root 666 1 0xd3000c7c 2 root 666 1 0xd4000c7c 3 root 666 1 0xd2000c80 4 root 666 1 0xd2000c7d 5 root 666 1 0x320008f5 7 felix 666 2 ------ Semaphore Arrays -------- key semid owner perms nsems 0xd2000c7c 1 root 666 1 0xd3000c7c 2 root 666 1 0xd4000c7c 3 root 666 1 0xd2000c80 4 root 666 1 0xd2000c7d 5 root 666 1

得分 98
学习任务

沫颖 的学生作业:

代码 shm.h #ifndef __SHM_H__ #define __SHM_H__ #include #include #include #include #include #include #include #include #define PATHNAME "." #define PROID 40 #define SHM_SIZE 4096 #endif shm_write.c #include "shm.h" // 清理共享内存资源的函数 void cleanup_shm(int shm_id, void *shm_addr) { if (shm_addr != NULL) { // 解除共享内存映射 if (shmdt(shm_addr) == -1) { perror("[ERROR] shmdt() in cleanup:"); } } if (shm_id != -1) { if (shmctl(shm_id, IPC_RMID, NULL) == -1) { perror("[ERROR] shmctl() in cleanup:"); } } } // 从给定文件描述符读取文件内容并写入共享内存的函数,改进后添加写入数据长度信息 int write_file_to_shm(int fd, void *shm_addr) { char buffer[SHM_SIZE - sizeof(int)]; ssize_t rbytes; ssize_t total_written = sizeof(int); int data_length = 0; while ((rbytes = read(fd, buffer, sizeof(buffer))) > 0) { // 根据实际读取到的字节数来写入共享内存 data_length += rbytes; memcpy((char *)shm_addr + total_written, buffer, rbytes); total_written += rbytes; } if (rbytes == -1) { perror("[ERROR] read() :"); return -1; } memcpy(shm_addr, &data_length, sizeof(int)); return 0; } int main(int argc, char const *argv[]) { // 判断控制台输入参数(需要传输的文件路径) if (2 != argc) { printf("[ERROR] Usage : %s \n", argv[0]); exit(EXIT_FAILURE); } int fd; // 打开文件 fd = open(argv[1], O_RDONLY); if (fd == -1) { perror("[ERROR] open() :"); exit(EXIT_FAILURE); } key_t key_shm; // 生成唯一key key_shm = ftok(PATHNAME, PROID); if (key_shm == -1) { perror("[ERROR] ftok() :"); exit(EXIT_FAILURE); } int shm_id; // 创建共享内存 shm_id = shmget(key_shm, SHM_SIZE, IPC_CREAT | 0666); if (shm_id == -1) { perror("[ERROR] shmget() :"); exit(EXIT_FAILURE); } // 映射共享内存 void *shm_addr = NULL; shm_addr = shmat(shm_id, NULL, 0); if (shm_addr == (void *)-1) { perror("[ERROR] shmat() :"); close(fd); exit(EXIT_FAILURE); } // 读取文件内容并写入共享内存 if (write_file_to_shm(fd, shm_addr) == -1) { close(fd); // 出现错误时,先清理资源再返回错误码 cleanup_shm(shm_id, shm_addr); exit(EXIT_FAILURE); } // // 正常结束前手动调用清理资源的函数 // cleanup_shm(shm_id, shm_addr); // 成功写入共享内存 printf("write to shm success!\n"); // 关闭文件 close(fd); return 0; } shm_read.c #include "shm.h" // 清理共享内存资源的函数 void cleanup_resources(int shm_id, void *shm_addr, int fd) { if (shm_addr != NULL) { // 解除共享内存映射 if (shmdt(shm_addr) == -1) { perror("[ERROR] shmdt() in cleanup:"); } } if (shm_id != -1) { if (shmctl(shm_id, IPC_RMID, NULL) == -1) { perror("[ERROR] shmctl() in cleanup:"); } } if (fd != -1) { // 关闭文件描述符 close(fd); } } // 从共享内存中读取固定长度的数据 ssize_t read_fixed_length_from_shm(void *shm_addr, char *buffer, size_t length) { ssize_t rbytes = 0; while (rbytes < length) { buffer[rbytes] = ((char *)shm_addr)[rbytes]; rbytes++; } return rbytes; } int main(int argc, char const *argv[]) { int fd; // 打开文件 fd = open(argv[1], O_CREAT | O_RDWR, 0666); if (fd == -1) { perror("[ERROR] open() :"); exit(EXIT_FAILURE); } key_t key_shm; // 生成唯一key key_shm = ftok(PATHNAME, PROID); if (key_shm == -1) { perror("[ERROR] ftok() :"); exit(EXIT_FAILURE); } int shm_id; // 创建共享内存 shm_id = shmget(key_shm, SHM_SIZE, IPC_CREAT | 0666); if (shm_id == -1) { perror("[ERROR] shmget() :"); exit(EXIT_FAILURE); } // 映射共享内存 void *shm_addr = NULL; shm_addr = shmat(shm_id, NULL, 0); if (shm_addr == (void *)-1) { perror("[ERROR] shmat() :"); close(fd); exit(EXIT_FAILURE); } int data_length; memcpy(&data_length, shm_addr, sizeof(int)); char buffer[SHM_SIZE]; ssize_t rbytes; if (data_length > 0) { // 根据获取到的数据长度从共享内存读取相应字节的数据到缓冲区 rbytes = read_fixed_length_from_shm((char *)shm_addr + sizeof(int), buffer, data_length); if (rbytes == -1) { perror("[ERROR] read from shm:"); // 出现错误时,先清理资源再返回错误码 cleanup_resources(shm_id, shm_addr, fd); return EXIT_FAILURE; } // 将缓冲区中的数据写入文件 if (write(fd, buffer, rbytes) == -1) { perror("[ERROR] write() :"); // 出现错误时,先清理资源再返回错误码 cleanup_resources(shm_id, shm_addr, fd); return EXIT_FAILURE; } } // 正常结束前手动调用清理资源的函数 cleanup_resources(shm_id, shm_addr, fd); // 成功写入文件 printf("write success\n"); // 关闭文件 close(fd); return 0; }

得分 98
学习任务

别摸我的键盘 的学生作业:

(1)要求大家是设计⼀个design_arary()函数,自己定义返回值和参数。 要求desigan_array()函数中定义一个,static char a[100] = {0};,然后 把数组首地址和长度返回。 (2)设计一个input_array()函数,自定义返回和参数,要求用户从键盘 输入任意的字符串,存放到a数组中 (3)设计一个output_array()函数,要求输出a数组中的每一个字符, 以空格作为区分. w u h a n s h a n g h a i (4)设计一个cout_space()函数,自定义返回和参数,要求用户统计数组a中用户 输入的空格个数,并返回值给main函数。 (5)main()函数调用以上函数,并输出空格个数。 #include char * design_array(int * p_len){ static char a[100] = {0}; int len = sizeof(a) / sizeof(char); * p_len = len; return a; } void input_array(char * p_arr, int len){ printf("please char:"); gets(p_arr); } void output_array(char * p_arr, int len){ for(int i = 0; i < len; i++){ printf("%c", p_arr[i]); } printf("\n"); } int count_space(char * p_arr, int len){ int cnt = 0; for(int i = 0; i < len; i++){ if(p_arr[i] == ' '){ cnt++; } } return cnt; } int main(){ int len = 0; char * p_arr = NULL; p_arr = design_array(&len); //printf("%p %d\n", p_arr, len); input_array(p_arr, len); output_array(p_arr, len); int space_cnt = count_space(p_arr, len); printf("space count %d\n", space_cnt); return 0; }

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号