-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos_code.c
More file actions
75 lines (66 loc) · 1.46 KB
/
os_code.c
File metadata and controls
75 lines (66 loc) · 1.46 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
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define MAXSIZE 10000
int a[MAXSIZE];
int currthread = 0;
int n;
struct dataThread
{
int Skey;
int Kidx;
};
int initt()
{
for(int i=0;i<MAXSIZE;i++)
{
a[i] = i;
}
printf("Enter the Key to be Searched(Key should be less than 10000)\n");
scanf("%d", &n);
return 1;
}
void *threadLinearSearch( void *thdargs)
{
struct dataThread *data;
data = (struct dataThread *) thdargs;
int start = (MAXSIZE/2)*currthread;
int end = MAXSIZE/2 + start;
currthread++;
for(int i=start; i<=end; i++)
{
if(a[i] == data->Skey)
{
data->Kidx = i;
pthread_exit(NULL);
}
}
data->Kidx = -1;
pthread_exit(NULL);
}
int main()
{
initt();
pthread_t lthread, rthread;
struct dataThread LeThd, RiThd;
LeThd.Skey = n;
RiThd.Skey = n;
int temp;
temp = pthread_create(<hread, NULL, threadLinearSearch, (void*) &LeThd);
temp = pthread_create(&rthread, NULL, threadLinearSearch, (void*) &RiThd);
pthread_join(lthread, NULL);
pthread_join(rthread, NULL);
if(LeThd.Kidx!=-1)
{
printf("The index of the key %d is %d\n", n, LeThd.Kidx);
pthread_exit(NULL);
}
if(RiThd.Kidx!=-1)
{
printf("The index of the key %d is %d\n", n, RiThd.Kidx);
pthread_exit(NULL);
}
printf("Key not found\n");
pthread_exit(NULL);
return 0;
}