操作系统概念第四章编程项目-多线程应用程序排序

Multithreaded Sorting Application

Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice. The two sublists are then merged by a third thread—a merging thread—which merges the two sublists into a single sorted list.
在这里插入图片描述

Figure 4.20 Multithreaded sorting.

Because global data are shared cross all threads, perhaps the easiest way to set up the data is to create a global array. Each sorting thread will work on one half of this array. A second global array of the same size as the unsorted integer array will also be established. The merging thread will then merge the two sublists into this second array. Graphically, this program is structured according to Figure 4.20.
This programming project will require passing parameters to each of thesorting threads. In particular, it will be necessary to identify the starting index from which each thread is to begin sorting. Refer to the instructions in Project 1 for details on passing parameters to a thread.
The parent thread will output the sorted array once all sorting threads have exited.

多线程应用程序排序

写一个多线程排序程序,工作如下:一个整数列表被分成两个大小相等的更小的列表。两个独立的线程(我们称之为排序线程)使用您选择的排序算法对每个子列表进行排序。然后,这两个子列表被第三个线程合并——合并线程——将这两个子列表合并成一个排序过的列表。
因为全局数据是跨所有线程共享的,所以设置数据的最简单方法可能是创建一个全局数组。每个排序线程将在这个数组的一半上工作。还将建立第二个与未排序整数数组相同大小的全局数组。然后合并线程将这两个子列表合并到第二个数组中。从图形上看,这个程序的结构如图4.20所示。
这个编程项目将需要向每个排序线程传递参数。具体来说,有必要标识每个线程开始排序的起始索引。有关向线程传递参数的详细信息,请参阅项目1中的说明。
一旦所有排序线程退出,父线程将输出已排序的数组。

代码:

#include <iostream>
#include <pthread.h>
#include <stdio.h>

#define SIZE                10
#define NUMBER_OF_THREADS    3

void* sorter(void* params);    /* thread that performs basic sorting algorithm */
void* merger(void* params);    /* thread that performs merging of results */

int list[SIZE]={7,12,19,3,18,4,2,6,15,8};

int result[SIZE];

typedef struct
{
    int from_index;
    int to_index;
} parameters;

int main(int argc, const char* argv[])
{
    parameters* data_1 = (parameters*)malloc(sizeof(parameters));
    parameters* data_2 = (parameters*)malloc(sizeof(parameters));
    
    pthread_t tid_sort1;
    pthread_t tid_sort2;
    pthread_t tid_merge;
    
    data_1->from_index = 0;
    data_1->to_index = 4;
    pthread_create(&tid_sort1, NULL, sorter, (void*)data_1);
    
    data_2->from_index = 5;
    data_2->to_index = 9;
    pthread_create(&tid_sort2, NULL, sorter, (void*)data_2);
    
    pthread_join(tid_sort1,NULL);
    pthread_join(tid_sort2,NULL);
    
    pthread_create(&tid_merge, NULL, merger, NULL);
    pthread_join(tid_merge, NULL);
    
    printf("The sorted array is :\n");
    for (int p = 0; p < 10; p++)
        printf("%d ", list[p]);
    return 0;
}

void* sorter(void* params){
    parameters* index;
    index=(parameters*)params;
    int temp;
    //printf("%d\t%d\n",index->from_index,index->to_index);
    for (int i = index->from_index; i <= index->to_index; i++){
        for (int j = i + 1; j <= index->to_index; j++){
            if (list[i] > list[j]){
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
        //printf("%d",list[i]);
    }
    //printf("\n");
    pthread_exit(0);
}


void* merger(void* params){
    int start=0,end=9,mid=4;
    int i=start;
    int j=mid+1;
    int temp[end+1];
    int k=0;
    while (i <= mid && j <= end) {
        if (list[i] < list[j]){
            temp[k++] = list[i++];
        }
        else{
            temp[k++] = list[j++];
        }
    }
    if (i == mid + 1) {
        while(j <= end)
            temp[k++] = list[j++];
    }
    if (j == end + 1) {
        while (i <= mid)
            temp[k++] = list[i++];
    }
    for (j = 0, i = start ; j < k; i++, j++) {
        list[i] = temp[j];
    }
    pthread_exit(0);
}

merge线程采用的是归并排序,整合前面两个线程排完序之后的数组。

输出结果:
在这里插入图片描述

  • 6
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值