设计一个队列是一个共享存储器 [英] Designing a Queue to be a shared memory
问题描述
我试图为一个共享内存设计/实施(圆形)队列中(C),以便它可以多线程/进程间共享。
I'm attempting to design/implement a (circular) queue (in C) as a shared memory so that it can be shared between multiple threads/processes.
队列结构如下:
typedef struct _q {
int q_size;
int q_front;
int q_rear;
int *q_data;
}queue;
哪个支持以下功能:
Which supports the following functions:
int empty_q(queue *q);
int display_q(queue *q);
int create_q(queue **q, int size);
int delete_q(queue **q);
int enqueue(queue *q, int data);
int dequeue(queue *q, int *data);
根据用户所提到的队列大小,q_data内存将在create_q()分配的。
As per the queue size mentioned by the user, the memory for q_data will be allocated in create_q().
问题:如何创建使用SYS / shm.h提供的系统功能,此队列中的共享内存?任何code段/例如创建/安装/检索/使用shmget的()的shmat()了shmctl()等将是一个很大的帮助删除共享内存队列的数据结构。
Question: How to create a shared memory for this queue using system functions provided in "sys/shm.h"? Any code snippet/example for creating/attaching/retrieving/deleting shared memory for the queue data-structure using shmget(), shmat(), shmctl(), etc would be a great help.
推荐答案
在我的Unix IPC搞砸,我也跟着 Beej的指南Unix的IPC 。它甚至有一些笑话!您可以直接转到共享内存部分。它具有片段解释每一个步骤,并在最后一个完整的例子。
When I messed with Unix IPC, I followed Beej's guide to Unix IPC. It even has some jokes! You can go directly to the shared memory section. It has snippets explaining each step, and a full example at the end.
这篇关于设计一个队列是一个共享存储器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!