操作系统实验上机

更多技术请访问:www.xuanworld.top

部分审核不通过的文章将发至个人博客:www.xuanworld.top

实验名称 实验序号 实验日期 实验人
多线程编程 3 公元2077年 Rytter

一、实验题目

image-20220501215425007

二、相关原理和知识

  1. pthread_t实现线程标识符
  2. pthread_create()函数实现线程创建
  3. pthread_join()实现线程等待完成

三、实验过程

  1. 使用vim进行代码编写
  2. 调试pthread_create()功能
  3. 实现运行排序功能
  4. 实现日合并功能

四、实验结果

ha

五、问题总结

  1. 忘记等待进程结束在合并
  2. 合并代码出现部分bug

六、源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <pthread.h>

int *array;
void* sort(void* ptr){
int *be=(int *)ptr;
int *number=array;
int begin=be[0];
int end=be[1];
for (int i = begin; i <end ; ++i) {
for (int j = begin; j <end ; ++j) {
if(number[j+1]<number[j]){
int c=number[j];
number[j]=number[j+1];
number[j+1]=c;
}
}
}
printf("it func \n");
}
int main() {
printf("please input the number,input e as end\n");
int length=0;
int a[100];
array=a;
while (scanf("%d",&a[length++]));
/* 声明两个线程的描述符 */
pthread_t tid1;
pthread_t tid2;
getchar();
int be1[2]={0,((length-1)/2-1)};
int be2[2]={((length-1)/2) ,(length-2)};
pthread_create(&tid1, NULL, sort, be1);
pthread_create(&tid2, NULL, sort, be2);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
int j=0,k=0,i=(length-1)/2;
int answer[length-2];
while(i<=length-2&&j<=(length-1)/2-1){
if(a[j]>a[i]){
answer[k++]=a[i++];
} else{
answer[k++]=a[j++];
}
}
while (i<=length-2) answer[k++]=a[i++];
while (j<=(length-1)/2-1) answer[k++]=a[j++];

k=0;
while (k<length-1){
printf("%d ",answer[k++]);
}
return 0;
}