Tests whether a System V message queue with the specified numeric key exists.
What It Does and When to Use It
Read the state for a known numeric System V key. A producer publishes work, a consumer executes it, and the broker or scheduler controls delivery and retries.
Requirements, Inputs and Expected Behavior
Confirm the runtime, extension, environment variables, and permissions before running the examples in production. System V IPC is platform-specific; existence alone does not imply permissions.
Example 1: Basic Operation
Read the state for a known numeric System V key.
<?php
$key = 0x12345;
echo msg_queue_exists($key) ? 'Queue exists' : 'Queue not found';
Example 2: Behavior and Validation
Derive a repeatable key with ftok when the script file is real.
<?php
$key = ftok(__FILE__, 'Q');
if ($key === -1) throw new RuntimeException('Cannot create IPC key');
if (!msg_queue_exists($key)) echo 'Create queue during initialization';
Practical Example: Application Use
Create a queue only after checking its existence.
<?php
$key = 0x56789;
if (!msg_queue_exists($key)) {
$queue = msg_get_queue($key, 0600);
if (!$queue) throw new RuntimeException('Unable to create queue');
}
echo 'Queue ready';
Important Behavior and Edge Cases
The second example highlights: Derive a repeatable key with ftok when the script file is real. The practical example adds: Create a queue only after checking its existence.
Comparison with Related Approaches
| Approach | What changes |
|---|---|
| Producer, consumer and scheduler | A producer publishes work, a consumer executes it, and the broker or scheduler controls delivery and retries. |
| Choosing safely | Acknowledge completed work and use idempotency to make retries safe. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Acknowledging a job before it finishes | Mark delivery successful only after durable processing succeeds. |
| Treating retries as harmless | Use idempotency keys and a bounded retry policy with dead-letter handling. |
| Skipping operational constraints | System V IPC is platform-specific; existence alone does not imply permissions. |
Key Takeaways
- Tests whether a System V message queue with the specified numeric key exists.
- Create a queue only after checking its existence.
- Acknowledge completed work and use idempotency to make retries safe.