-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.c
More file actions
88 lines (81 loc) · 1.24 KB
/
Sort.c
File metadata and controls
88 lines (81 loc) · 1.24 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
/*
Name: Chirantan Joshi.
Div: G
Batch: G1
Roll No.: PG13
SORTING WITH FORK CALL
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
pid_t num_pid;
int arr[20],n,i,j,temp,status;
printf("Enter the number of elements in array: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element in array: ");
scanf("%d",&arr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[j] < arr[i])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
num_pid=fork();
if(num_pid==0)
{
printf("\nIn Child process!!\n\nSorted array is:\n");
for(i=n-1;i>=0;i--)
{
printf("%d\n",arr[i]);
}
_exit(0);
}
else
{
waitpid(num_pid,&status,0);
int exit_status=WEXITSTATUS(status);
printf("\nThe exit status of child is: %d\n",exit_status);
printf("\nIn Parent process!!\n\nSorted array is:\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
}
return 0;
}
/*
OUTPUT:
Enter the number of elements in array: 5
Enter element in array: 10
Enter element in array: 3
Enter element in array: 5
Enter element in array: 20
Enter element in array: 15
In Child process!!
Sorted array is:
20
15
10
5
3
The exit status of child is: 0
In Parent process!!
Sorted array is:
3
5
10
15
20
*/