-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab-2 - Sorting using fork().c
163 lines (154 loc) · 3.42 KB
/
Lab-2 - Sorting using fork().c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# include<stdio.h>
# include <stdlib.h>
# include<sys/types.h>
# include<unistd.h>
int split ( int a[ ], int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
void quickSort(int a[],int lower, int upper)
{
int i ;
if ( upper > lower )
{
i = split ( a, lower, upper ) ;
quickSort ( a, lower, i - 1 ) ;
quickSort ( a, i + 1, upper ) ;
}
}
void mergeSort(int arr[],int low,int mid,int high)
{
int i,j,k,l,b[20];
l=low;
i=low;
j=mid+1;
while((l<=mid)&&(j<=high))
{
if(arr[l]<=arr[j])
{
b[i]=arr[l];
l++;
}
else
{
b[i]=arr[j];
j++;
}
i++;
}
if(l>mid)
{
for(k=j;k<=high;k++)
{
b[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
b[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=b[k];
}
}
void partition(int arr[],int low,int high)
{
int mid;
if(low<high)
{
double temp;
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void display(int a[],int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d\t\t",a[i]);
}
printf("\n");
}
int main()
{
int pid, child_pid;
int size,i,status;
printf("Enter the number of Integers to Sort::::\t");
scanf("%d",&size);
int a[size];
int pArr[size];
int cArr[size];
for(i=0;i<size;i++)
{
printf("Enter number %d:",(i+1));
scanf("%d",&a[i]);
pArr[i]=a[i];
cArr[i]=a[i];
}
printf("Your Entered Integers for Sorting\n");
display(a,size);
pid=getpid();
printf("Current Process ID is : %d\n",pid);
printf("[ Forking Child Process ... ] \n");
child_pid=fork();
if( child_pid < 0)
{
printf("\nChild Process Creation Failed!!!!\n");
exit(-1);
}
else if( child_pid==0)
{
printf("\nThe Child Process\n");
printf("\nChild process is %d",getpid());
printf("\nParent of child process is %d\n",getppid());
printf("Child is sorting the list of Integers by QUICK SORT::\n");
quickSort(cArr,0,size-1);
printf("The sorted List by Child::\n");
display(cArr,size);
printf("Child Process Completed ...\n");
sleep(10);
}
else
{
printf("Parent process %d started\n",getpid());
printf("Parent of parent is %d\n",getppid());
sleep(30);
printf("\nThe Parent Process\n");
printf("\nParent %d is sorting the list of Integers by MERGE SORT\n",pid);
partition(pArr,0,size-1);
printf("The sorted List by Parent::\n");
display(pArr,size);
wait(&status);
printf("Parent Process Completed ...\n");
}
return 0;
}