-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2B1.c
More file actions
127 lines (102 loc) · 2.99 KB
/
2B1.c
File metadata and controls
127 lines (102 loc) · 2.99 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
Parent.c :-
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
void bubble_asc(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("\nArray in ascending order: ");
for (i = 0; i < n; i++) {
printf("\t%d", arr[i]);
}
printf("\n");
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Please provide numbers to sort.\n");
return 1;
}
int n = argc - 1; // Number of integers provided
int a[n];
pid_t pid;
// Convert arguments to integers
for (int i = 1; i <= n; i++) {
a[i - 1] = atoi(argv[i]); // Store the integers in array
}
bubble_asc(a, n); // Sort in ascending order
// Prepare arguments for child process
char *args[n + 2]; // +2 for program name and NULL termination
args[0] = "./child"; // Assume the child executable is named "child"
for (int i = 0; i < n; i++) {
args[i + 1] = argv[i + 1]; // Pass sorted values
}
args[n + 1] = NULL; // NULL terminate the argument list
pid = fork();
if (pid == 0) { // Child process
printf("\nI am Child Process, my pid is %d \n", getpid());
execve("./child", args, NULL); // Execute child program
perror("execve failed"); // Only reached if execve fails
exit(1);
} else if (pid > 0) { // Parent process
wait(NULL); // Wait for child process to finish
printf("\nI am Parent Process, my pid is %d \n", getpid());
} else {
perror("Fork failed");
return 1;
}
return 0;
}
Child.c :-
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void bubble_dsc(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("\nArray in descending order: ");
for (i = 0; i < n; i++) {
printf("\t%d", arr[i]);
}
printf("\n");
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("No numbers provided.\n");
return 1;
}
int n = argc - 1; // Number of integers
int a[n];
// Convert arguments to integers
for (int i = 1; i <= n; i++) {
a[i - 1] = atoi(argv[i]); // Store the integers in array
}
bubble_dsc(a, n); // Sort in descending order
return 0;
}
Output :-
[Saru1594@localhost 2B]$ gcc -o parent parent.c
[Saru1594@localhost 2B]$ gcc -o child child.c
[Saru1594@localhost 2B]$ ./parent 6 2 4 9 3 1 0
Array in ascending order: 0 1 2 3 4 6 9
I am Child Process, my pid is 12476
Array in descending order: 9 6 4 3 2 1 0
I am Parent Process, my pid is 12475