-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.c
More file actions
142 lines (117 loc) · 3.47 KB
/
31.c
File metadata and controls
142 lines (117 loc) · 3.47 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
============================================================================
Name : 31a
Author : Piyush Singh
Description : Write a program to create a semaphore and initialize value to the semaphore.
a. create a binary semaphore
b. create a counting semaphore
Date: 26th Sep, 2025.
============================================================================
*/
// #include <stdio.h>
// #include <stdlib.h>
// #include <sys/ipc.h>
// #include <sys/sem.h>
// // Union required by semctl
// union semun
// {
// int val;
// };
// int main()
// {
// key_t key = ftok("sem.key", 33); // Ensure this file exists
// if (key == -1)
// {
// perror("❌ ftok failed");
// exit(1);
// }
// // Create a semaphore set with 1 semaphore
// int semid = semget(key, 1, IPC_CREAT | 0666);
// if (semid == -1)
// {
// perror("❌ semget failed");
// exit(1);
// }
// // Initialize to 1 (binary semaphore)
// union semun arg;
// arg.val = 1;
// if (semctl(semid, 0, SETVAL, arg) == -1)
// {
// perror("❌ semctl SETVAL failed");
// exit(1);
// }
// printf("✅ Binary semaphore created with value: 1\n");
// return 0;
// }
// #include <stdio.h>
// #include <stdlib.h>
// #include <semaphore.h>
// int main()
// {
// sem_t binary_sem;
// sem_t counting_sem;
// int value;
// // Step 1: Initialize binary semaphore to 1
// if (sem_init(&binary_sem, 0, 1) != 0)
// {
// perror("❌ sem_init (binary) failed");
// exit(1);
// }
// // Step 2: Initialize counting semaphore to 5
// if (sem_init(&counting_sem, 0, 5) != 0)
// {
// perror("❌ sem_init (counting) failed");
// exit(1);
// }
// // Step 3: Get and print values
// sem_getvalue(&binary_sem, &value);
// printf("✅ Binary semaphore initialized to: %d\n", value);
// sem_getvalue(&counting_sem, &value);
// printf("✅ Counting semaphore initialized to: %d\n", value);
// // Step 4: Cleanup
// sem_destroy(&binary_sem);
// sem_destroy(&counting_sem);
// return 0;
// }
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <fcntl.h> // For O_CREAT, O_EXCL
#include <sys/stat.h> // For mode constants
#include <unistd.h>
int main()
{
sem_t *binary_sem;
sem_t *counting_sem;
int value;
// Step 1: Create named binary semaphore with initial value 1
binary_sem = sem_open("/binary_sem", O_CREAT | O_EXCL, 0666, 1);
if (binary_sem == SEM_FAILED)
{
perror("❌ sem_open (binary) failed");
exit(1);
}
// Step 2: Create named counting semaphore with initial value 5
counting_sem = sem_open("/counting_sem", O_CREAT | O_EXCL, 0666, 5);
if (counting_sem == SEM_FAILED)
{
perror("❌ sem_open (counting) failed");
sem_unlink("/binary_sem");
exit(1);
}
// Step 3: Print confirmation (no sem_getvalue on macOS)
printf("✅ Named binary semaphore created with initial value 1\n");
printf("✅ Named counting semaphore created with initial value 5\n");
// Step 4: Cleanup
sem_close(binary_sem);
sem_close(counting_sem);
sem_unlink("/binary_sem");
sem_unlink("/counting_sem");
return 0;
}
/*
Output:
╰─ ./31 ─╯
✅ Named binary semaphore created with initial value 1
✅ Named counting semaphore created with initial value 5
*/