-
Notifications
You must be signed in to change notification settings - Fork 0
/
a3Zombie.c
78 lines (72 loc) · 1.3 KB
/
a3Zombie.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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
void arrayStore(char* arr[],int* i_arr, int n)
{
for(int i=0;i<n;i++)
{
i_arr[i] = atoi(arr[i+1]);
}
}
void arraySort(int* arr, int n, int falg)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(falg == -1)
{
if(arr[j]<arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
else if(falg == 1)
{
if(arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
void arrayDisplay(int* arr, int n)
{
printf("Displaying Array.... \n");
for(int i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
printf("\n\n");
}
void main(int argc, char* argv[])
{
int arr[argc-1];
int len = argc - 1;
arrayStore(argv,arr,len);
pid_t pid, tpid, status;
pid = fork();
if(pid != 0)
{
sleep(1); //Zombie State
system("ps");
printf("\nParent Process with ID = %d \n",getpid());
arraySort(arr,len,1);
arrayDisplay(arr,len);
}
else if(pid == 0)
{
system("ps");
printf("\nChild Process with ID = %d\n",getpid());
printf("Child's Parent ID = %d\n",getppid());
arraySort(arr,len,-1);
arrayDisplay(arr,len);
}
}