-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.c
More file actions
77 lines (59 loc) · 1.71 KB
/
admin.c
File metadata and controls
77 lines (59 loc) · 1.71 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
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main()
{
// Create a shared memory segment
key_t key = ftok("admin.c",'A');
if (key == -1){
perror("Error in ftok of Admin");
return(1);
}
int shmid_admin_manager;
int *shmptr_admin_manager;
shmid_admin_manager = shmget(key, sizeof(int), IPC_CREAT | 0666);
if (shmid_admin_manager == -1){
perror("Error in creating shared memory segment of Admin");
return(1);
}
shmptr_admin_manager = shmat(shmid_admin_manager, NULL, 0);
if (shmptr_admin_manager == (void *)-1){
perror("Error in attaching shared memory segment to Admin");
return(1);
}
while(1){
char c;
printf("\nDo you want to close the hotel? Enter Y for Yes and N for No. \n");
scanf(" %c", &c);
if(c == 'Y'|| c == 'y'){
printf("\nHotel is being closed. \n");
break;
}
else if(c == 'N'|| c == 'n'){
printf("\nHotel remains open. \n");
}
else{
printf("Invalid input , please renter input. \n");
}
}
// Initiate termination of the hotel
*shmptr_admin_manager = -1;
while (*shmptr_admin_manager != 1){
sleep(1);
}
if(shmdt(shmptr_admin_manager) == -1){
perror("Error in detaching shared memory segment of Admin");
return(1);
}
if(shmctl(shmid_admin_manager, IPC_RMID, NULL) == -1){
perror("Error in deleting shared memory segment of Admin");
return(1);
}
printf("\nHotel is closed. \n");
return 0;
}