# 将队列设计为共享内存[英] Designing a Queue to be a shared memory

### 问题描述

```typedef struct _q {
int q_size;
int q_front;
int q_rear;
int *q_data;
}queue;
```

```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);
```

## 其他推荐答案

```typedef struct
{
char a[24];
int i;
int j;
} somestruct;

void fillshm(int shmid) {
somestruct *p;

if ( (p = shmat (shmid, NULL, 0)) < 0 )
{
perror("shmat");
exit(1);
}

printf("writing to shared memory\n");
strcpy(p->a, "my shared memory");
p->i = 123;
p->j = 456;
}

void printshm(int shmid)
{
somestruct *p;
if ( (p = shmat (shmid, NULL, 0)) < 0 )
{
perror("shmat");
exit(1);
}

printf( "%s, %d, %d\n", p->a, p->i, p->j );
}

int main( int argc, char *argv[] ) {

int shmid;

// see if the memory exists and print it if so
if ( (shmid = shmget (1234, 0, 0)) >= 0 )
printshm( shmid );
else
{
// didn't exist, so create it
if ( (shmid = shmget (1234, sizeof( somestruct ), IPC_CREAT | 0600)) < 0 )
{
perror("shmget");
exit(1);
}

printf( "shmid = %d\n", shmid );

fillshm(shmid);
printf( "Run another instance of this app to read the memory... (press a key): " );
getchar();

// delete it
if ( shmctl (shmid, IPC_RMID, NULL) < 0 )
{
perror("semctl");
exit(1);
}
}

return 0;
}
```