中山大学软件工程操作系统实验二报告
来源:互联网 发布:淘宝首页全屏海报尺寸 编辑:程序博客网 时间:2024/06/05 14:46
实验二 多线程程序实验
一、实验目的
1. 进一步理解线程 2. 学习使用pthread线程库
二、实验运行环境
虚拟机VMware下的Ubuntu16.04系统
三、实验内容
1. 用线程生成Fibonacci数列 2. 多线程矩阵乘法
四、实验原理
线程使用说明——主要系统调用: pthread_create():创建线程 pthread_join():阻塞调用线程,直到threadid所指定的线程终止 每个线程只能用pthread_join()一次。若多次调用就会发生逻辑错误。 pthread_exit():终止调用线程 pthread_attr_init():初始化线程属性为默认属性 pthread_attr_getscope():获得线程竞争范围 pthread_attr_setscope():设置线程竞争范围使用pthread的程序编译命令:若程序文件是main.c 传统命令为:gcc main.c -o main -lpthread 现在命令为:gcc main.c -o main -pthread 差别:后一个会选用线程安全的库实现若程序文件是main.cc 传统命令为:g++ main.cc -o main -lpthread 现在命令为:g++ main.cc -o main -pthread 差别:后一个会选用线程安全的库实现
五、实验过程
1. 用线程生成Fibonacci数列 用pthread线程库,按照第四章习题4.11的要求生成并输出Fibonacci数列。
代码如下:
#include <iostream>#include <pthread.h>using namespace std;int n;// the size of fibonacci arrayvoid *fibonacci(void *data) { int *a = (int*)data; // calculate the fibonacci array for (int i = 2; i < n; i++) { a[i] = a[i - 1] + a[i - 2]; } pthread_exit(NULL);}int main() { cout << "Please enter the number n(n>2):" << endl; cin >> n; while (n <= 2) { cout << "The number should be larger than 2." << endl; cout << "Please enter the number n(n>2):" << endl; cin >> n; } int a[1000]; // initial a[0] and a[1] a[0] = 0; a[1] = 1; pthread_t th; // create a thread to calculate pthread_create(&th, NULL, fibonacci, (void*)a); // a thread to be joined upon pthread_join(th, NULL); cout << "Fibonacci:" << endl; // output the result for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; return 0;}
编译运行结果如下:
由于我限制了n的大小,只有当n大于2才输出Fibonacci数列。
输入9:
输入20:
根据上述实验结果可得出:程序正确,证明已实现用线程生成Fibonacci数列。
2. 多线程矩阵乘法 矩阵乘法:给定两个矩阵A和B,其中A是具有M行、K列的矩阵,B为K行、N列的矩阵,A和B的矩阵积为矩阵C,C为M行、N列的矩阵。矩阵C中第i行、第j列的元素Cij就是矩阵A第i行每个元素和矩阵B第j列每个元素乘积的和,即
要求:每个Cij的计算用一个独立的工作线程,因此它将会涉及生成M*N个工作线程。主线程(或称为父线程)将初始化矩阵A和B,并分配足够的内存给矩阵C,它将容纳矩阵A和B的积。这些矩阵将声明为全局数据,以使每个工作线程都能访问矩阵A、B和C。
代码如下:
#include <iostream>#include <pthread.h>#include <stdlib.h>using namespace std;int M, K, N;// the size of matrixint A[100][100];int B[100][100];int C[100][100];// structure for passing data to threadsstruct v{ int i, j;};// calculate the matrix product in C[row][col]void *calculate(void *data) { struct v *a = (struct v*)data; int i = a->i; int j = a->j; for (int k = 0; k < K; k++) { C[i][j] += A[i][k] * B[k][j]; } pthread_exit(NULL);}int main() { cout << "Please enter three numbers(M/K/N) that are less than 100:" << endl; cin >> M >> K >> N; cout << "Please enter the first matrix(M*K):" << endl; for (int i = 0; i < M; i++) { for (int j = 0; j < K; j++) { cin >> A[i][j]; } } cout << "Please enter the second matrix(K*N):" << endl; for (int i = 0; i < K; i++) { for (int j = 0; j < N; j++) { cin >> B[i][j]; } } for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { C[i][j] = 0; } } pthread_t tid[M * N]; pthread_attr_t attr; // get the default attributes pthread_attr_init(&attr); // we have to create M*N pthreads for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { struct v *a = (struct v*)malloc(sizeof(struct v)); a->i = i; a->j = j; pthread_create(&tid[i * N + j], &attr, calculate, (void*)a); } } // join upon each thread for (int i = 0; i < M * N; i++) { pthread_join(tid[i], NULL); } // output the result cout << "The result(M*N) is:" << endl; for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { cout << C[i][j] << " "; if (j == N - 1) cout << endl; } } return 0;}
编译运行结果如下:
与MATLAB软件得出的结果作对比可得出实验结果正确:
再测试一组较大的数据:
运行结果如下图
与MATLAB软件得出的结果对比可得实验结果正确,证明用多线程实现矩阵乘法算法准确。
五、实验总结
总的来说,这次实验相对比较简单,根据课本的知识和老师的提示,可以很快就实现这两个实验内容。但是,我们不能仅仅满足于实验结果的正确,最重要的是我们要去理解线程的意义和作用,学习pthread线程库主要的几个系统调用:pthread_create(), pthread_join(), pthread_exit(), pthread_attr_init(), pthread_attr_getscope(), pthread_attr_setscope()。通过这次实验,我进一步加深了对线程的理解,一个应用程序通常是作为一个具有多个控制线程的独立进程实现的,深刻体会到多线程的4个优点:响应度高、资源共享、经济、多处理器体系结构的利用。
2 0
- 中山大学软件工程操作系统实验二报告
- 中山大学软件工程操作系统实验一报告
- 中山大学软件工程操作系统实验三报告
- 软件工程实验报告二
- 软件工程实验报告 二
- 软件工程实验报告 二
- 高级软件工程实验报告二
- 操作系统实验二实验报告
- 软件工程课程实验报告:实验二
- 软件工程实验报告一
- 软件工程导论实验报告
- 软件工程实验报告一
- 软件工程实验报告 一
- 软件工程实验报告 三
- 软件工程实验报告 一
- 软件工程实验报告 一
- 软件工程实验报告 三
- 软件工程实验报告 三
- Linux学习笔记(三)-设备管理与驱动
- 数据结构—链表—单链表应用-增序排列节点
- BIEE-2、RPD
- intellij idea macos 创建servlet 始终页面打不开 404
- POJ 2400 Supervisor, Supervisee (二分图最大权匹配)
- 中山大学软件工程操作系统实验二报告
- D(1909)Perfect Chocolate
- unix环境高级编程.chapter 1.1
- mongodb3.X版本 用户管理
- Codeforces801B Valued Keys
- POJ 2417 baby_step giant_step 小步大步算法 a^x == b(mod n) 求解0<=x<n的值
- 接笔记一,涉及struts2登录的
- 4月份英语总结博客
- 一个小爬虫 从网页获取信息(图片下载)