操作系统实验上机
更多技术请访问:www.xuanworld.top
部分审核不通过的文章将发至个人博客:www.xuanworld.top
实验名称 | 实验序号 | 实验日期 | 实验人 |
---|---|---|---|
多线程编程 | 3 | 公元2077年 | Rytter |
一、实验题目
二、相关原理和知识
- pthread_t实现线程标识符
- pthread_create()函数实现线程创建
- pthread_join()实现线程等待完成
三、实验过程
- 使用vim进行代码编写
- 调试pthread_create()功能
- 实现运行排序功能
- 实现日合并功能
四、实验结果
五、问题总结
- 忘记等待进程结束在合并
- 合并代码出现部分bug
六、源码
#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;
}