-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.c
More file actions
48 lines (42 loc) · 1.42 KB
/
24.c
File metadata and controls
48 lines (42 loc) · 1.42 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
/*
============================================================================
Name : 24
Author : Piyush Singh
Description : Write a program to create a message queue and print the key and message queue id.
Date: 26th Sep, 2025.
============================================================================
*/
// Program: Create a System V message queue and print key + ID
#include <stdio.h> // For printf(), perror()
#include <stdlib.h> // For exit()
#include <sys/ipc.h> // For IPC_CREAT, ftok()
#include <sys/msg.h> // For msgget()
int main()
{
// Step 1: Generate a unique key using ftok
// ftok uses a filename and a project ID to generate a key
key_t key = ftok(".", 'A'); // '.' = current dir, 'A' = project ID
if (key == -1)
{
perror("❌ ftok failed");
exit(1);
}
// Step 2: Create a message queue with the generated key
// IPC_CREAT creates the queue if it doesn't exist
int msgid = msgget(key, IPC_CREAT | 0666); // 0666 = read/write permissions
if (msgid == -1)
{
perror("❌ msgget failed");
exit(1);
}
// Step 3: Print the key and message queue ID
printf("🔑 Message Queue Key: %d\n", key);
printf("🆔 Message Queue ID: %d\n", msgid);
return 0;
}
/*
Output:
╰─ ./24 ─╯
🔑 Message Queue Key: 1091576192
🆔 Message Queue ID: 65537
*/