버퍼를 공유메모리로 설정하고 접근할때 질문있습니다.

kangmaee의 이미지

헤더

#ifndef QUEUE_H
#define QUEUE_H
 
typedef struct tagNode
{
	char data[1400];
} Node;
 
typedef struct tagCircularQueue
{
	int capacity;  
  int front;
	int rear;
	Node* nodes;
} CircularQueue;
 
void CreateQueue(CircularQueue** queue, int capacity);
void DestroyQueue(CircularQueue* queue);
void Enqueue(CircularQueue* queue, char data[1400]);
char *Dequeue(CircularQueue* queue);
int GetSize(CircularQueue* queue);
int IsEmpty(CircularQueue* queue);
int IsFull(CircularQueue* queue);
#endif

라이브러리
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
 
#define SHARED_MEMORY_KEY 9876
 
int shmid;
void *shm_addr = (void *)0;
Node *data;
 
void CreateQueue(CircularQueue** queue, int capacity)
{
	*queue = (CircularQueue*)malloc(sizeof(CircularQueue));
 
	(*queue)->nodes = (Node*)malloc(sizeof(Node)* (capacity + 1));
 
	(*queue)->capacity = capacity;
	(*queue)->front = 0;
	(*queue)->rear = 0;
 
  shmid = shmget((key_t)SHARED_MEMORY_KEY, sizeof(CircularQueue), 0666|IPC_CREAT);
 
  if(shmid == -1)
  {
    perror("shmget failed : ");
    exit(0);
  }
 
  printf("shmid = %d\n", shmid);
 
  shm_addr = shmat(shmid, (void *)0, 0);
  if(shm_addr == (void *)-1)
  {
    perror("shmat failed : ");
    exit(0);
  }
  data = (Node *)shm_addr;
}
 
void DestroyQueue(CircularQueue* queue)
{
  if(shmdt(shmid) == -1)
  {
    perror("shmdt failed : ");
    exit(0);
  }
 
  free(queue->nodes);
	free(queue);
}
 
void Enqueue(CircularQueue* queue, char data[1400])
{
	int position = 0;
 
	if (queue->rear == queue->capacity + 1)
	{	
		queue->rear = 0;
		position = 0;
	}
	else
	  position = queue->rear++;
 
 	strcpy(queue->nodes[position].data, data);
}
 
char *Dequeue(CircularQueue* queue)
{
	int position = queue->front;
 
  shmat(shmid, (void *)0, 0);
  if (position == queue->capacity)
		queue->front = 0;
	else
		queue->front++; 
 
 	return queue->nodes[position].data;
}
 
 
int GetSize(CircularQueue* queue)
{
 
	if (queue->front <= queue->rear)
		return queue->rear - queue->front;
	else
		return (queue->capacity + 1 - queue->front) + queue->rear;
}
 
int IsEmpty(CircularQueue* queue)
{
	return (queue->front == queue->rear);
}
 
 
int IsFull(CircularQueue* queue)
{
	if (queue->front < queue->rear)
		return (queue->rear - queue->front) == queue->capacity;
	else
		return (queue->rear + 1) == queue->front;
}

메인 1(버퍼삽입)
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
   CircularQueue* queue;
   CreateQueue(&queue, 100);
 
   Enqueue(queue, "abcd");
   Enqueue(queue, "dcba");
 
   return 0;
}

메인 2(버퍼 내용 꺼내기)
#include "queue.h"
#include <stdio.h>
 
int main(void)
{
   CircularQueue* queue;
   CreateQueue(&queue, 100);
 
   printf("data : ");
   printf("%s\n", Dequeue(queue));
   printf("%s\n", Dequeue(queue));

이렇게 해놓고 메인1을 실행하고 메인2를 실행하면 아무 자료가 출력되지 않습니다.
ipcs로 확인해보면 같은 메모리로 잡힌거로 보이는데..어디가 잘못된건지 도무지 모르겠네요

shxodlf3의 이미지

입문서 참고하세요

1년차 임베디드 소프트쟁이 입니다

시스템에 관심이 많습니다

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.