2024.02.22作业

1. 将互斥机制的代码实现重新敲一遍

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>#define MAXSIZE 128int num = 520;
pthread_mutex_t mutex;void* task1(void* arg)
{printf("111111111111111\n");pthread_mutex_lock(&mutex);num = 1314;sleep(3);printf("task1:num = %d\n", num);pthread_mutex_unlock(&mutex);
}void* task2(void* arg)
{printf("222222222222222\n");pthread_mutex_lock(&mutex);num++;sleep(1);printf("task2:num = %d\n", num);pthread_mutex_unlock(&mutex);
}int main(int argc, char const *argv[])
{pthread_mutex_init(&mutex, NULL);pthread_t thread1, thread2;if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \pthread_create(&thread2, NULL, task2, NULL) != 0){perror("thread create error");return -1;}printf("thread1:%#lx, thread2:%#lx\n", thread1, thread2);pthread_join(thread1, NULL);pthread_join(thread2, NULL);pthread_mutex_destroy(&mutex);return 0;
}

2. 将无名信号量的代码实现重新敲一遍

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>#define MAXSIZE 1024sem_t sem1;
sem_t sem2;
sem_t sem3;void* task1(void* arg)
{int num = 5;while (num--){sem_wait(&sem1);printf("A");sem_post(&sem2);}pthread_exit(NULL);
}void* task2(void* arg)
{int num = 5;while (num--){sem_wait(&sem2);printf("B");sem_post(&sem3);}pthread_exit(NULL);
}void* task3(void* arg)
{int num = 5;while (num--){sem_wait(&sem3);printf("C\n");sem_post(&sem1);}pthread_exit(NULL);
}int main(int argc, char const *argv[])
{sem_init(&sem1, 0, 1);sem_init(&sem2, 0, 0);sem_init(&sem3, 0, 0);pthread_t thread1, thread2, thread3;if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \pthread_create(&thread2, NULL, task2, NULL) != 0 || \pthread_create(&thread3, NULL, task3, NULL) != 0){puts("thread create error");return -1;}pthread_join(thread1, NULL);pthread_join(thread2, NULL);pthread_join(thread3, NULL);sem_destroy(&sem1);sem_destroy(&sem2);sem_destroy(&sem3);return 0;
}

3. 将条件变量的代码实现重新敲一遍

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>#define MAXSIZE 128pthread_cond_t cond;
pthread_mutex_t mutex;void* task1(void* arg)
{int num = 5;while (num--){sleep(1);printf("%#lx:生产一辆车\n", pthread_self());}pthread_cond_broadcast(&cond);pthread_exit(NULL);
}void* task2(void* arg)
{pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);printf("%#lx:获得一辆车\n", pthread_self());pthread_mutex_unlock(&mutex);
}int main(int argc, char const *argv[])
{pthread_mutex_init(&mutex, NULL);pthread_t thread1, thread2, thread3, thread4, thread5, thread6;if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \pthread_create(&thread2, NULL, task2, NULL) != 0 || \pthread_create(&thread3, NULL, task2, NULL) != 0 || \pthread_create(&thread4, NULL, task2, NULL) != 0 || \pthread_create(&thread5, NULL, task2, NULL) != 0 || \pthread_create(&thread6, NULL, task2, NULL) != 0){perror("thread create error");return -1;}printf("thread1:%#lx, thread2:%#lx, thread3:%#lx, thread4:%#lx, thread5:%#lx, thread6:%#lx\n", thread1, thread2, thread3, thread4, thread5, thread6);pthread_join(thread1, NULL);pthread_join(thread2, NULL);pthread_join(thread3, NULL);pthread_join(thread4, NULL);pthread_join(thread5, NULL);pthread_join(thread6, NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;
}

4. 将无名管道的代码实现重新敲一遍

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>#define MAXSIZE 1024int main(int argc, char const *argv[])
{int pipefd[2] = { 0 };if (pipe(pipefd) == -1){perror("pipe error");return -1;} printf("pipefd[0]=%d, pipefd[1]=%d\n", pipefd[0], pipefd[1]);pid_t pid = fork();if (pid > 0){close(pipefd[0]);char wbuf[128] = "";while (1){bzero(wbuf, sizeof(wbuf));fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf) - 1] = 0;write(pipefd[1], wbuf, strlen(wbuf));if (strcmp(wbuf, "quit") == 0){break;}}close(pipefd[1]);wait(NULL);}else if (pid == 0){close(pipefd[1]);char rbuf[128] = "";while (1){bzero(rbuf, sizeof(rbuf));read(pipefd[0], rbuf, sizeof(rbuf));printf("父进程传入的字符串是:%s\n", rbuf);if (strcmp(rbuf, "quit") == 0){break;}}close(pipefd[0]);exit(EXIT_SUCCESS);}return 0;
}

5. 将有名管道的代码实现重新敲一遍

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>#define MAXSIZE 128void menu()
{printf("请选择:\n");printf("1.创建管道\n");printf("2.输入\n");printf("3.输出\n");printf("4.删除管道\n");printf("选择:");
}int init_fifo(char* filename)
{if (mkfifo(filename, 0664) == -1){return -1;}return 0;
}void* send(void* arg)
{char* filename = (char*)arg;int wfd = -1;if ((wfd = open(filename, O_WRONLY)) == -1){perror("open error");exit(EXIT_FAILURE);}char wbuf[MAXSIZE] = "";while (1){printf("输入>>>");fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf) - 1] = 0;write(wfd, wbuf, strlen(wbuf));if (strcmp(wbuf, "quit") == 0){break;}}close(wfd);exit(EXIT_SUCCESS);
}void* receive(void* arg)
{int rfd = -1;if ((rfd = open("./myfifo", O_RDONLY)) == -1){perror("open error");exit(EXIT_FAILURE);}char rbuf[MAXSIZE] = "";while (1){bzero(rbuf, sizeof(rbuf));read(rfd, rbuf, sizeof(rbuf));printf("收到的数据为:%s\n", rbuf);if (strcmp(rbuf, "quit") == 0){break;}}close(rfd);exit(EXIT_SUCCESS);
}int main(int argc, char const *argv[])
{int n;menu();scanf("%d", &n);getchar();char filename[] = "./myfifo";if (n == 1){if (init_fifo(filename) == 0){puts("创建成功!");}else{puts("创建失败!");}return 0;}if (n == 4){char cmd[MAXSIZE];snprintf(cmd, sizeof(cmd), "rm %s", filename);system(cmd);return 0;}pthread_t thread = -1;if (n == 2){if (pthread_create(&thread, NULL, send, filename) != 0){perror("create thread error");return -1;}}if (n == 3){if (pthread_create(&thread, NULL, receive, filename) != 0){perror("create thread error");return -1;}}pthread_join(thread, NULL);return 0;
}

6. 使用有名管道完成两个进程的相互通信(可使用多进程或多线程)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>#define MAXSIZE 128void menu()
{printf("请选择:\n");printf("1.创建管道\n");printf("2.输入输出\n");printf("3.输出输入\n");printf("4.删除管道\n");printf("选择:");
}int init_fifo(char* filename)
{if (mkfifo(filename, 0664) == -1){return -1;}return 0;
}void* send(void* arg)
{char* filename = (char*)arg;int wfd = -1;if ((wfd = open(filename, O_WRONLY)) == -1){perror("open error");exit(EXIT_FAILURE);}char wbuf[MAXSIZE] = "";while (1){fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf) - 1] = 0;write(wfd, wbuf, strlen(wbuf));if (strcmp(wbuf, "quit") == 0){break;}}close(wfd);exit(EXIT_SUCCESS);
}void* receive(void* arg)
{char* filename = (char*)arg;int rfd = -1;if ((rfd = open(filename, O_RDONLY)) == -1){perror("open error");exit(EXIT_FAILURE);}char rbuf[MAXSIZE] = "";while (1){bzero(rbuf, sizeof(rbuf));read(rfd, rbuf, sizeof(rbuf));printf("收到的数据为:%s\n", rbuf);if (strcmp(rbuf, "quit") == 0){break;}}close(rfd);exit(EXIT_SUCCESS);
}int main(int argc, char const *argv[])
{int n;menu();scanf("%d", &n);getchar();char f1[] = "./myfifo0";char f2[] = "./myfifo1";if (n == 1){if (init_fifo(f1) == 0 && init_fifo(f2) == 0){puts("创建成功!");}else{puts("创建失败!");}return 0;}if (n == 4){char cmd[MAXSIZE];snprintf(cmd, sizeof(cmd), "rm %s %s", f1, f2);system(cmd);return 0;}pthread_t thread1 = -1;pthread_t thread2 = -1;if (n == 2){if (pthread_create(&thread1, NULL, send, f1) != 0){perror("create thread error");return -1;}if (pthread_create(&thread2, NULL, receive, f2) != 0){perror("create thread error");return -1;}}if (n == 3){if (pthread_create(&thread1, NULL, send, f2) != 0){perror("create thread error");return -1;}if (pthread_create(&thread2, NULL, receive, f1) != 0){perror("create thread error");return -1;}}pthread_join(thread1, NULL);pthread_join(thread2, NULL);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/696036.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

世界顶级名校计算机专业学习使用教材汇总

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-IauYk2cGjEyljid0 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-siz…

GIS、RS、VORS模型、CCDM模型、geodetecto、GWR模型集成的生态系统健康的耦合协调分析

详情V&#xff1a;gjt0312765817632教授如何集成多源数据&#xff0c;依托ArcGIS Pro和R语言环境&#xff0c;采用“活力-组织力-恢复力-贡献力”&#xff08;VORS&#xff09;模型定量测算生态系统健康指数&#xff08;EHI&#xff09;&#xff1b;如何从经济城镇化&#xff0…

瑞_Redis_初识Redis(含安装教程)

文章目录 1 初识Redis1.1 认识NoSQL1.1.1 结构化与非结构化1.1.2 关联和非关联1.1.3 查询方式1.1.4 事务1.1.5 总结 1.2 认识Redis1.2.1 介绍1.2.2 特征1.2.3 优势 1.3 安装Redis ★★★1.3.1 Linux安装Redis1.3.1.1 安装Redis依赖 1.3.2 Windows安装Redis1.3.2.1 安装步骤1.3.…

springboot实现Aop(通知)切面编程的案例

以下是一个使用Spring Boot AOP的简单案例&#xff1a; 假设我们有一个UserService接口&#xff0c;它包含了两个方法&#xff1a;getUserById和createUser。我们希望在每个方法执行前后打印日志。 首先&#xff0c;我们需要创建一个切面类&#xff0c;用于定义我们的切面逻辑…

在Vue中使用TypeScript时 props指定枚举类型

推荐一款AI网站 AI写作与AI绘画智能创作平台 - 海鲸AI | 智能AI助手&#xff0c;可以免费领取GPT3.5无限卡 在Vue中使用TypeScript时&#xff0c;您可以通过定义一个枚举类型&#xff0c;然后在组件的props定义中使用这个枚举来指定props的类型。以下是一个如何做到这一点的例子…

Angular构建Library报错:error NG3001: Unsupported private class

报错 Unsupported private class ObjTypeSelectorComponent. This class is visible to consumers via SimpleFormsModule -> ObjTypeSelectorComponent, but is not exported from the top-level library entrypoint. 解决方案 未在index.ts / projects.ts中导出&#xf…

ETL快速拉取物流信息

我国作为世界第一的物流大国&#xff0c;但是在目前的物流信息系统还存在着几大的痛点。主要包括以下几个方面&#xff1a; 数据孤岛&#xff1a;有些物流企业各个部门之间的数据标准不一致&#xff0c;难以实现数据共享和协同&#xff0c;容易导致信息孤岛。 操作繁琐&#x…

数据结构D3作业

1. 2. 按位插入 void insert_pos(seq_p L,datatype num,int pos) { if(LNULL) { printf("入参为空&#xff0c;请检查\n"); return; } if(seq_full(L)1) { printf("表已满&#xff0c;不能插入\n"); …

unity学习(34)——角色选取界面(跨场景坑多)

先把SelectMenu中的camera的audio listener去掉。 现在还是平面&#xff0c;直接在camera下面添加两个panel即可&#xff0c;应该是用不到canvas了&#xff0c;都是2D的UI。 加完以后问题来了&#xff0c;角色选择界面的按钮跑到主界面上边了&#xff0c;而且现在账号密码都输…

CoordConv(NeurIPS 2018)

paper&#xff1a;An Intriguing Failing of Convolutional Neural Networks and the CoordConv Solution official implementation&#xff1a;https://github.com/uber-research/coordconv 存在的问题 本文揭示并分析了CNN在两种不同类型空间表示之间转换能力的欠缺&#…

远程连接 vscode 出错 “远程主机可能不符合 glibc 和 libstdc++ VS Code 服务器的先决条件”

原因&#xff1a; vscode 版本是 1.86&#xff0c;服务器上的 glibc 和 libstdc 版本不满足 要求(2.28 和 3.4.25)。 解决&#xff1a; 1、下载 1.85.2&#xff0c;解压直接运行 Code.exe。 2、回退 Remote-ssh 到 0.107.1。 参考&#xff1a; vscode 1.86版本远程ssh不兼容旧…

Leetcode155(设计最小栈)

例题&#xff1a; 分析&#xff1a; 题目要求我们必须在常数时间内检索到最小元素。 我们可以使用两个栈&#xff08;A、B&#xff09;来实现&#xff0c;A栈用来正常存储数据、弹出数据&#xff0c; B栈用于存储A栈中的最小元素&#xff0c;如下图&#xff1a; 刚开始&#…

【webpack】基础介绍

当我们深入分析Webpack时&#xff0c;可以更加详细地了解它的工作原理、构建流程、常用配置和插件。 工作原理&#xff1a; 解析模块&#xff1a; Webpack从入口文件开始&#xff0c;递归地解析模块之间的依赖关系&#xff0c;构建一个依赖图。解析过程中&#xff0c;Webpack会…

windows 10 和 11 的3个杀招软件

大家喜欢的3个windows 小工具 千万别开着杀毒软件解压 建议关闭后解压 小弟弟保证不是病毒 下载地址 : https://download.csdn.net/download/nn_84/88865566

ES6 面试题

1. const、let 和 var 的区别是什么&#xff1f; 答案&#xff1a; var 声明的变量是函数作用域或全局作用域&#xff0c;而 const 和 let 声明的变量是块级作用域。使用 var 声明的变量可以被重复声明&#xff0c;而 const 和 let 不允许重复声明同一变量。const 声明的变量…

Spring 类型转换、数值绑定与验证(一)— DataBinder

DataBinder 是Spring用于数据绑定、类型转换及验证的类。使用场景有&#xff1a;1&#xff09;xml配置文件定义bean,Spring 内部使用DataBinder 来完成属性的绑定&#xff1b;2&#xff09;Web请求参数绑定&#xff0c;在Spring MVC 中&#xff0c;Controller的方法参数通常会自…

Sora - 探索AI视频模型的无限可能

随着人工智能技术的飞速发展&#xff0c;AI视频模型已成为科技领域的新热点。而在这个浪潮中&#xff0c;OpenAI推出的首个AI视频模型Sora&#xff0c;以其卓越的性能和前瞻性的技术&#xff0c;引领着AI视频领域的创新发展。让我们将一起探讨Sora的技术特点、应用场景以及对未…

【Linux】Linux应用程序中需要忽略的信号:SIGPIPE、SIGHUP、SIGINT;以及信号详解

1、常用信号 1.1 SIGPIPE 当服务器关闭一个连接后,若客户端继续发送数据,系统会发送要给SIGPIPE信号给客户端所在的进程,该信号的默认处理是终止进程; 反过来,客户端断开socket连接, 服务端向一个失效的socket发送数据,也将导致服务的进程退出。 如果不想退出,需要忽…

Stable Diffusion 模型分享:Indigo Furry mix(人类与野兽的混合)

本文收录于《AI绘画从入门到精通》专栏,专栏总目录:点这里。 文章目录 模型介绍生成案例案例一案例二案例三案例四案例五案例六案例七案例八案例九案例十

内核栈是什么

内核栈&#xff08;Kernel Stack&#xff09;是操作系统内核为每个运行中的进程或线程分配的一块内存区域&#xff0c;用于保存内核级别的函数调用过程中的局部变量、参数以及返回地址等信息。每个进程或线程都有自己独立的内核栈。 内核栈与用户栈&#xff08;用户空间栈&…