-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.c
More file actions
135 lines (118 loc) · 3.3 KB
/
threads.c
File metadata and controls
135 lines (118 loc) · 3.3 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// To compile the following code use gcc -pthread -o threads_test threads_test.c
//./threads_test to execute
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10 //Array size
//Global variables
int array[SIZE] = {8,23,-10,-24,13,32,6,18,42,76};//Array initiazed
int result[SIZE]; //Array to store the result
//Data structure defination
typedef struct
{
int start;
int end;
} parameters;
void *sorter(void *params) //Sort threads will call this function
{
parameters* p = (parameters *)params;
printf("The current thread id is %d\n",pthread_self());
int begin = p->start;
int end = p->end;
printf("The begin index is %d\n",begin);
printf("The end index is %d\n\n",end);
int z;
for(z = begin; z <= end; z++)
{
printf("The array obtained: %d \n", array[z]);
}
printf("\n");
//Sorting the array
int i,j,t,k;
for(i=begin; i <= end; i++)
{
for(j=begin; j <= end-1; j++)
{
if(array[j] > array[j+1])
{
t = array[j];
array[j] = array[j+1];
array[j+1] = t;
}
}
}
//Displays the sorted array
for(k = begin; k <= end; k++)
{
printf("The sorted array: %d\n", array[k]);
}
//Copies the sorted array to the result array
int x;
for(x=begin; x <= end; x++)
{
result[x]=array[x];
}
printf("\n");
//Thread exits
pthread_exit(NULL);
}
void *merger(void *params)//The merge thread will call this function
{
parameters* p = (parameters *)params;
int begin = p->start;
int end = p->end;
//Sorting of the result array
int i,j,t;
for(i=begin; i< end; i++)
{
for(j=begin; j< end-i; j++)
{
if(result[j] > result[j+1])
{
t = result[j];
result[j] = result[j+1];
result[j+1] = t;
}
}
}
int m;
printf("The final sorted array is:\n");
for(m=0; m <= SIZE-1; m++)
{
printf(" %d\n", result[m]);
}
pthread_exit(NULL);
}
void main ()
{
int i;
pthread_t sort_thread1; //Initializing the threads
pthread_t sort_thread2;
pthread_t merge_thread;
printf("\n");
printf("\n");
printf("-------------------Made by Megha Agarwal---------------------\n\n\n");
//Assigning the values for thread 1
parameters *data1 = (parameters *) malloc (sizeof(parameters));
data1->start = 0;
data1->end = (SIZE/2) - 1;
pthread_create(&sort_thread1, 0, sorter, data1); //Creating the thread
//Assigning the values for thread 2
parameters *data2 = (parameters *) malloc (sizeof(parameters));
data2->start = (SIZE/2);
data2->end = SIZE - 1;
pthread_create(&sort_thread2, 0, sorter, data2);
//Waiting for threads 1 & 2 to finish execution
pthread_join(sort_thread1, NULL);
pthread_join(sort_thread2, NULL);
//Assigning the values for thread 3
parameters *data3 = (parameters *) malloc(sizeof(parameters));
data3->start = 0;
data3->end = SIZE-1;
pthread_create(&merge_thread, 0, merger, data3);//Creating the merge thread after sort thread finish execution
//Wait for the merge thread
pthread_join(merge_thread, NULL);
printf("\n");
printf("\n");
printf("***************************THANK YOU*******************************\n");
}