-
Notifications
You must be signed in to change notification settings - Fork 0
/
sec
290 lines (249 loc) · 7.43 KB
/
sec
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#define SHM_SIZE 1024
int main() {
int fd[2]; // File descriptors for pipe
pid_t pid;
char input[SHM_SIZE];
// Create pipe
if (pipe(fd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
// Fork process
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid > 0) { // Parent process
close(fd[0]); // Close read end of pipe
// Get user input
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
// Write input to pipe
write(fd[1], input, strlen(input) + 1);
close(fd[1]); // Close write end of pipe
wait(NULL); // Wait for child process to finish
// Open shared memory
int shm_fd = shm_open("/shared_memory", O_RDONLY, 0666);
if (shm_fd == -1) {
perror("shm_open");
exit(EXIT_FAILURE);
}
// Map shared memory
char *data = mmap(NULL, SHM_SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
if (data == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
// Print data from shared memory
printf("Received data: %s\n", data);
// Clean up
munmap(data, SHM_SIZE);
close(shm_fd);
shm_unlink("/shared_memory");
} else { // Child process
close(fd[1]); // Close write end of pipe
// Read from pipe
char received_data[SHM_SIZE];
read(fd[0], received_data, sizeof(received_data));
close(fd[0]); // Close read end of pipe
// Convert received data to lowercase
for (int i = 0; received_data[i]; i++) {
received_data[i] = tolower(received_data[i]);
}
// Create and map shared memory
int shm_fd = shm_open("/shared_memory", O_CREAT | O_RDWR, 0666);
if (shm_fd == -1) {
perror("shm_open");
exit(EXIT_FAILURE);
}
ftruncate(shm_fd, SHM_SIZE);
char *data = mmap(NULL, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (data == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
// Write lowercase data to shared memory
strcpy(data, received_data);
// Clean up
munmap(data, SHM_SIZE);
close(shm_fd);
exit(EXIT_SUCCESS);
}
return 0;
}
#!/bin/bash
file="cities.txt"
# Function to add a new record
add_record() {
echo "Enter city name:"
read city
echo "Enter 2020 population:"
read pop_2020
echo "Enter 2015 population:"
read pop_2015
echo "Enter 2010 population:"
read pop_2010
echo "$city, $pop_2020, $pop_2015, $pop_2010" >> "$file"
echo "Record added successfully"
}
# Function to delete a record
delete_record() {
echo "Enter city name to delete:"
read city
sed -i "/^$city/d" "$file"
echo "Record deleted successfully"
}
# Function to modify a record
modify_record() {
echo "Enter city name to modify:"
read city
echo "Enter new 2020 population:"
read pop_2020
echo "Enter new 2015 population:"
read pop_2015
echo "Enter new 2010 population:"
read pop_2010
sed -i "/^$city/c\\$city, $pop_2020, $pop_2015, $pop_2010" "$file"
echo "Record modified successfully"
}
# Function to sort records based on 2020 population
sort_by_population() {
sort -t',' -k2 -nr "$file"
}
# Function to find cities with largest percentage decrease in population between 2005 and 2015
largest_decrease() {
awk -F', ' '{ decrease = ($3 - $4) / $4 * 100; print $1, decrease }' "$file" | sort -k2 -nr | head -n 1
}
# Function to display number of Indians lived outside cities and towns in 2015
outside_cities() {
awk -F', ' '{ total += ($3 - $2) } END { print total }' "$file"
}
# Function to display ten largest cities
ten_largest_cities() {
sort -t',' -k2 -nr "$file" | head -n 10
}
# Function to display details of cities matching a pattern
city_pattern() {
echo "Enter city name pattern:"
read pattern
grep "^$pattern" "$file"
}
# Main menu
while true; do
echo "Menu:"
echo "1. Add a new record"
echo "2. Delete a record"
echo "3. Modify a record"
echo "4. Sort records by 2020 population"
echo "5. Find cities with largest percentage decrease in population between 2005 and 2015"
echo "6. Number of Indians lived outside cities and towns in 2015"
echo "7. Ten largest cities"
echo "8. Display details of cities matching a pattern"
echo "9. Exit"
read choice
case $choice in
1) add_record ;;
2) delete_record ;;
3) modify_record ;;
4) sort_by_population ;;
5) largest_decrease ;;
6) outside_cities ;;
7) ten_largest_cities ;;
8) city_pattern ;;
9) exit ;;
*) echo "Invalid choice";;
esac
done
#!/bin/bash
file="cities.txt"
# Function to add a new record
add_record() {
echo "Enter city name:"
read city
echo "Enter 2020 population:"
read pop_2020
echo "Enter 2015 population:"
read pop_2015
echo "Enter 2010 population:"
read pop_2010
echo "$city, $pop_2020, $pop_2015, $pop_2010" >> "$file"
echo "Record added successfully"
}
# Function to delete a record
delete_record() {
echo "Enter city name to delete:"
read city
sed -i "/^$city/d" "$file"
echo "Record deleted successfully"
}
# Function to modify a record
modify_record() {
echo "Enter city name to modify:"
read city
echo "Enter new 2020 population:"
read pop_2020
echo "Enter new 2015 population:"
read pop_2015
echo "Enter new 2010 population:"
read pop_2010
sed -i "/^$city/c\\$city, $pop_2020, $pop_2015, $pop_2010" "$file"
echo "Record modified successfully"
}
# Function to sort records based on 2020 population
sort_by_population() {
sort -t',' -k2 -nr "$file"
}
# Function to find cities with largest percentage decrease in population between 2005 and 2015
largest_decrease() {
awk -F', ' '{ decrease = ($3 - $4) / $4 * 100; print $1, decrease }' "$file" | sort -k2 -nr | head -n 1
}
# Function to display number of Indians lived outside cities and towns in 2015
outside_cities() {
awk -F', ' '{ total += ($3 - $2) } END { print total }' "$file"
}
# Function to display ten largest cities
ten_largest_cities() {
sort -t',' -k2 -nr "$file" | head -n 10
}
# Function to display details of cities matching a pattern
city_pattern() {
echo "Enter city name pattern:"
read pattern
grep "^$pattern" "$file"
}
# Main menu
while true; do
echo "Menu:"
echo "1. Add a new record"
echo "2. Delete a record"
echo "3. Modify a record"
echo "4. Sort records by 2020 population"
echo "5. Find cities with largest percentage decrease in population between 2005 and 2015"
echo "6. Number of Indians lived outside cities and towns in 2015"
echo "7. Ten largest cities"
echo "8. Display details of cities matching a pattern"
echo "9. Exit"
read choice
case $choice in
1) add_record ;;
2) delete_record ;;
3) modify_record ;;
4) sort_by_population ;;
5) largest_decrease ;;
6) outside_cities ;;
7) ten_largest_cities ;;
8) city_pattern ;;
9) exit ;;
*) echo "Invalid choice";;
esac
done