-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path032.c
More file actions
108 lines (84 loc) · 3.03 KB
/
032.c
File metadata and controls
108 lines (84 loc) · 3.03 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
/*
..........................................................................................................................................
Name : 032a.c
Author : SHRUTI VERMA
Description : Write a program to implement semaphore to protect any critical section.
a. rewrite the ticket number creation program using semaphore
b. protect shared memory from concurrent write access
c. protect multiple pseudo resources ( may be two) using counting semaphore
d. remove the created semaphore
Date : 29 Sep 2025
..........................................................................................................................................
*/
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/sem.h>
#include<semaphore.h>
#include<string.h>
#include<sys/stat.h>
#include<fcntl.h>
#define SEM_TICKET_UPDATE "/sem_ticket_update"
#define SEM_RESOURCE1 "/sem_resource1"
#define SEM_RESOURCE2 "/sem_resource2"
int main() {
struct {
int trainNo;
int ticketNo;
}db;
sem_t *sem_ticket = sem_open(SEM_TICKET_UPDATE, O_CREAT, 0644, 1);
sem_t *sem_resource1 = sem_open(SEM_RESOURCE1, O_CREAT, 0644, 1);
sem_t *sem_resource2 = sem_open(SEM_RESOURCE2, O_CREAT, 0644, 1);
int fd = open("record.txt", O_RDWR);
int train;
printf("Enter the train no (1-4): ");
scanf("%d", &train);
printf("Waiting to acquire semaphore for ticket update...\n");
sem_wait(sem_ticket);
printf("Semaphore acquired for ticket update\n");
lseek(fd, (train - 1) * sizeof(db), SEEK_SET);
read(fd, &db, sizeof(db));
printf("Number of tickets sold is %d\n", db.ticketNo);
db.ticketNo++;
lseek(fd, (train - 1) * sizeof(db), SEEK_SET);
write(fd, &db, sizeof(db));
printf("Your ticket number is %d\n", db.ticketNo);
sem_post(sem_ticket);
printf("Semaphore released for ticket update\n");
printf("Accessing resource 1...\n");
sem_wait(sem_resource1);
printf("Resource 1 accessed\n");
printf("Accessing resource 2...\n");
sem_wait(sem_resource2);
printf("Resource 2 accessed\n");
printf("Working on both resources...\n");
sleep(2);
sem_post(sem_resource1);
sem_post(sem_resource2);
printf("Released both resources\n");
close(fd);
printf("Cleaning up semaphores...\n");
sem_close(sem_ticket);
sem_close(sem_resource1);
sem_close(sem_resource2);
sem_unlink(SEM_TICKET_UPDATE);
sem_unlink(SEM_RESOURCE1);
sem_unlink(SEM_RESOURCE2);
printf("Semaphores removed\n");
return 0;
}
/*----------------------------------OUTPUT------------------------------
Enter the train no (1-4): 1
Waiting to acquire semaphore for ticket update...
Semaphore acquired for ticket update
Number of tickets sold is 32764
Your ticket number is 32765
Semaphore released for ticket update
Accessing resource 1...
Resource 1 accessed
Accessing resource 2...
Resource 2 accessed
Working on both resources...
Released both resources
Cleaning up semaphores...
Semaphores removed*/