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

作业社区

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

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

SamstagBaron 的学生作业:

#include #include #include #include #include #include #include #include #define FIFO_NAME "./fifo" int main(void) { // for(;;){ // ret = poll(&pfds,1,1000); // if (ret == -1){ // perror("[ERROR] poll(): "); // exit(EXIT_FAILURE); // }else if (ret == 0){ // printf("Timeout.\n"); // }else if (ret > 0){ // if (pfds.revents & POLLIN){ //判断返回的就绪事件是否为 POLLIN // fgets(buffer,sizeof(buffer),stdin); // 如果是 POLLIN,则表示有用户输入 // printf("buffer : %s ",buffer); // } // } // } pid_t pid = fork(); if(pid==-1){ perror("[ERROR] fork() : "); exit(EXIT_FAILURE); } else if(pid==0){ if(access(FIFO_NAME,F_OK)==-1) mkfifo(FIFO_NAME,0644); char wbuffer[100]={0}; int fd = open(FIFO_NAME,O_WRONLY|O_NONBLOCK); int wbytes; while(1){ fgets(wbuffer,sizeof(wbuffer),stdin); wbytes = write(fd,wbuffer,strlen(wbuffer)); printf("write ok\n"); if(wbuffer0){ while(access(FIFO_NAME,F_OK)==-1); int fd = open(FIFO_NAME,O_RDONLY|O_NONBLOCK); int ret,maxfd = 0; struct pollfd pfds; char buffer[64] = {0}; pfds.fd = fd; pfds.events = POLLIN; maxfd = pfds.fd; while(1){ ret = poll(&pfds,1,1000); if (ret == -1){ perror("[ERROR] select(): "); exit(EXIT_FAILURE); }else if (ret == 0){ // 超时返回 printf("Timeout.\n"); }else if (ret > 0){ int rbytes; if (pfds.revents & POLLIN){ // 判断是否在集合中 printf("buffer :"); while(1){ rbytes = read(fd,buffer,sizeof(buffer)-1); if(rbytes==0){ printf("Read Done\n"); break; } else if(rbytes>0){ buffer[rbytes]='\0'; printf(" %s",buffer); if(strncmp(buffer,"quit",4)==0){ goto END; } } else{ perror("Read Error"); break; } } } } } END: close(fd); } return 0; } 【图片】

得分 100
学习任务

jelasin 的学生作业:

#ifndef __USE_GNU #define __USE_GNU #endif #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // system v ipc headers #include #include #include #include // posix ipc headers #include #include #include int main(int argc, char const *argv[]) { // system v ipc shared memory key_t key = ftok("/tmp", 'A'); int shm_id_sysv = shmget(key, 1024, IPC_CREAT | 0666); if (shm_id_sysv == -1) { perror("shmget"); exit(1); } void *shm_ptr_sysv = shmat(shm_id_sysv, NULL, 0); if (shm_ptr_sysv == NULL) { perror("shmat"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shm_id_sysv); FILE *fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } char line[1024]; void *ptr_shm = shm_ptr_sysv; ssize_t nbytes = 0; while ((nbytes = fread(line, 1, 1024, fp)) > 0) { strncpy(ptr_shm, line, nbytes); ptr_shm = (int8_t*)ptr_shm + nbytes; } pclose(fp); strcpy(ptr_shm, "Hello, system v ipc shared memory!\n"); pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(1); } else if (pid == 0) { // child process char buf[1024]; key_t key = ftok("/tmp", 'A'); int shm_id = shmget(key, 1024, 0); void *shm_ptr = shmat(shm_id, NULL, 0); strcpy(buf, shm_ptr); fprintf(stderr, "Child process: %d\n", getpid()); fprintf(stderr, "Read from Shared memory: %s\n", buf); exit(EXIT_SUCCESS); } else { // parent process int status; waitpid(pid, &status, 0); } if (shmctl(shm_id_sysv, IPC_RMID, NULL) == -1) { perror("shmctl"); return -1; } // -------------------------------------------------------------------------------------- // posix ipc shared memory int shm_fd_posix = shm_open("/posix_shm", O_CREAT | O_RDWR, 0666); if (shm_fd_posix == -1) { perror("shm_open"); exit(1); } if (ftruncate(shm_fd_posix, 1024) == -1) { perror("ftruncate"); exit(1); } void *shm_ptr_posix = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_posix, 0); if (shm_ptr_posix == MAP_FAILED) { perror("mmap"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shm_fd_posix); fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } ptr_shm = shm_ptr_posix; while ((nbytes = fread(line, 1, 1024, fp)) > 0) { strncpy(ptr_shm, line, nbytes); ptr_shm = (int8_t*)ptr_shm + nbytes; } pclose(fp); strcpy(ptr_shm, "Hello, posix ipc shared memory!\n"); pid = fork(); if (pid == -1) { perror("fork"); exit(1); } else if (pid == 0) { // child process int shm_fd = shm_open("/posix_shm", O_RDONLY, 0666); void *shm_ptr = mmap(NULL, 1024, PROT_READ, MAP_SHARED, shm_fd, 0); char buf[1024]; strcpy(buf, shm_ptr); fprintf(stderr, "Child process: %d\n", getpid()); fprintf(stderr, "Read from Shared memory: %s\n", buf); exit(EXIT_SUCCESS); } else { // parent process int status; waitpid(pid, &status, 0); } if (munmap(shm_ptr_posix, 1024) == -1) { perror("munmap"); exit(1); } if (close(shm_fd_posix) == -1) { perror("close"); exit(1); } if (shm_unlink("/posix_shm") == -1) { perror("shm_unlink"); exit(1); } return 0; } ➜ 5 ./main Shared memory: 15 Child process: 18101 Read from Shared memory: ------------ 共享内存段 -------------- 键 shmid 拥有者 权限 字节 连接数 状态 0x00000000 3 jelasin 606 10483788 2 目标 0x00000000 4 jelasin 606 10483788 2 目标 0x41030001 15 jelasin 666 1024 1 Hello, system v ipc shared memory! Shared memory: 3 Child process: 18104 Read from Shared memory: ------------ 共享内存段 -------------- 键 shmid 拥有者 权限 字节 连接数 状态 0x00000000 3 jelasin 606 10483788 2 目标 0x00000000 4 jelasin 606 10483788 2 目标 0x00000000 15 jelasin 666 1024 1 目标 Hello, posix ipc shared memory!

得分 100
学习任务

jelasin 的学生作业:

#ifndef __USE_GNU #define __USE_GNU #endif #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // system v ipc headers #include #include #include #include // posix ipc headers #include #include #include int main(int argc, char const *argv[]) { // system v ipc shared memory key_t key = ftok("/tmp", 'A'); int shmid = shmget(key, 1024, IPC_CREAT | 0666); if (shmid == -1) { perror("shmget"); exit(1); } void *shm = shmat(shmid, NULL, 0); if (shm == NULL) { perror("shmat"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shmid); FILE *fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } char line[1024]; while (fgets(line, sizeof(line), fp)) { fprintf(stderr, "%s", line); } pclose(fp); if (shmctl(shmid, IPC_RMID, NULL) == -1) { perror("shmctl"); return -1; } // posix ipc shared memory int shm_fd = shm_open("/posix_shm", O_CREAT | O_RDWR, 0666); if (shm_fd == -1) { perror("shm_open"); exit(1); } if (ftruncate(shm_fd, 1024) == -1) { perror("ftruncate"); exit(1); } void *shm_ptr = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); if (shm_ptr == MAP_FAILED) { perror("mmap"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shm_fd); fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } while (fgets(line, sizeof(line), fp)) { fprintf(stderr, "%s", line); } pclose(fp); if (munmap(shm_ptr, 1024) == -1) { perror("munmap"); exit(1); } if (close(shm_fd) == -1) { perror("close"); exit(1); } if (shm_unlink("/posix_shm") == -1) { perror("shm_unlink"); exit(1); } return 0; } ➜ 5 clang main.c -o main -lpthread ➜ 5 ./main Shared memory: 6 ------------ 共享内存段 -------------- 键 shmid 拥有者 权限 字节 连接数 状态 0x00000000 3 jelasin 606 10483788 2 目标 0x00000000 4 jelasin 606 10483788 2 目标 0x41030001 6 jelasin 666 1024 1 Shared memory: 3 ------------ 共享内存段 -------------- 键 shmid 拥有者 权限 字节 连接数 状态 0x00000000 3 jelasin 606 10483788 2 目标 0x00000000 4 jelasin 606 10483788 2 目标 0x00000000 6 jelasin 666 1024 1 目标

得分 100
学习任务

向佐佐 的学生作业:

#include #include #include #include #include #include static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int number = 0; // 记录仓库里的产品的数量 static bool done = false; // 标志位,表示生产是否完成 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 生产者线程的执行函数 void* thread_handler(void* arg) { int cnt = atoi((char*)arg); for (int i = 0; i < cnt; i++) { pthread_mutex_lock(&mutex); printf("线程[%ld]生产一个产品,产品数量为:%d\n", pthread_self(), ++number); pthread_mutex_unlock(&mutex); pthread_cond_broadcast(&cond); // 唤醒所有在cond阻塞的消费者线程 } pthread_exit(NULL); return NULL; } // 消费者线程执行函数 void* thread_handler_consume(void* arg) { while (true) { pthread_mutex_lock(&mutex); while (number == 0 && !done) { // 仓库中没有产品且生产未完成,则等待 pthread_cond_wait(&cond, &mutex); } if (done && number == 0) { // 生产完成且仓库为空,退出 pthread_mutex_unlock(&mutex); break; } // 消费产品 //total_of_consume++; printf("线程[%ld]消费一个产品,产品数量:%d\n", pthread_self(), --number); sleep(1); // 模拟消费时间 pthread_mutex_unlock(&mutex); } pthread_exit(NULL); return NULL; } // main函数输入argc个参数 // argv[1]~argv[argc-2]代表创建argc-2个生产者线程,每个线程分别生产1,2,3,...,argc-2个产品 // argv[argc-1]代表创建argv[argc-1]个消费者线程 int main(int argc, char* argv[]) { pthread_t tid_p[argc - 2]; pthread_t tid_c[atoi(argv[argc - 1])]; int i, err; // 创建生产者线程 for (i = 1; i < argc - 1; i++) { err = pthread_create(&tid_p[i - 1], NULL, thread_handler, (void*)argv[i]); if (err != 0) { fprintf(stderr, "[ERROR]pthread_product_create():\n", strerror(err)); exit(EXIT_FAILURE); } } // 创建消费者线程 for (i = 0; i < atoi(argv[argc - 1]); i++) { err = pthread_create(&tid_c[i], NULL, thread_handler_consume, NULL); if (err != 0) { fprintf(stderr, "[ERROR]pthread_consume_create():\n", strerror(err)); exit(EXIT_FAILURE); } } // 等待所有生产者线程完成 for (i = 0; i < argc - 2; i++) { pthread_join(tid_p[i], NULL); } // 设置生产完成标志 done = true; // 等待所有消费者线程完成 for (i = 0; i < atoi(argv[argc - 1]); i++) { pthread_join(tid_c[i], NULL); } printf("所有产品已消费完毕,程序结束。\n"); return 0; } 【图片】

微信客服

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

帮助反馈 APP下载

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

公众号

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