2012-03-04 87 views
0

我遇到了一個奇怪的問題。我正在爲基本上模仿生產者消費者問題的操作系統類製作一個共享內存fifo隊列。在我的一個函數putBuffer()中插入一個項目到共享緩衝區我沒有得到任何輸出之後,所以我通過gdb運行它打印我想在最初的終端運行時打印什麼,當我退出gdb它說程序正常退出,所以我不確定我的錯誤在哪裏。有沒有人經歷過這樣的事情?gdb輸出和終端輸出之間的差異

因此,當我通過gdb運行時,它會打印出「通過初始檢查並將值設置爲fifo->[12],此值僅用於測試目的,但在終端中僅打印」使其通過初始檢查。我甚至確保錯誤不在printf()有什麼想法?繼承人的代碼

int putBuffer(FIFO_QUEUE *fifo, int element) 
{ 
printf("made it past initial check\n"); 
fifo->queue[12] = 23; 
//insert the element at the next available position IFF there is one 

if(printf("made it to putBuffer and fifo->queue[12] = %d\n", fifo->queue[12]) < 0) 
    { 
     printf("error in putBuffer\n"); 
     return -1; 
} 
//determine whether or not we need to "wrap" around to the beginning of the queue 
if(fifo->putPos == fifo->size - 1) 
    fifo->putPos = 0; //wrap to the beginning 
else 
    fifo->putPos++; 

//increment the number of items in the queue 
fifo->numItems++; 

//if all went well return 0 
    return 0; 

}

每個請求這裏是FIFO_QUEUE的DEF我動態分配隊列結構中另一個的功能,但其通過的gdb

typedef struct fifoQueue{ 
int *queue; 
int putPos;  //next position to insert to 
int rmPos;  //next position to remove from 
int numItems; //number of items currently in the queue 
int size;   //the max size of the queue 
}FIFO_QUEUE; 

此存儲的值,並打印是我認爲我會錯的地方我需要在一個函數中動態分配fifo隊列,我想要做的是使用memcpy來基本創建一個fifo隊列,然後將它的內容複製到我的共享內存中,但似乎有一個由於int *在FIFO_QUEUE中,我無法弄清楚。我哪裏錯了。我懷疑它做的在mkBuffer動態分配()函數,我的想法是,memcpy的將剛纔複製的100個字節無論是在那裏,但我可能是錯誤

code for dynamic allocation 

#include<sys/ipc.h> 
#include<sys/shm.h> 
#include<sys/types.h> 
#include<stdlib.h> 
#include<stdio.h> 

#include <fcntl.h>   /* For O_* constants */ 
#include <sys/stat.h> 
#include<string.h> 
#include "fifoQueue.h" 


FIFO_QUEUE *makeBuffer(int size); 


int main(int argc, char *argv[]) 
{ 
//our buffer 
int i = 0; 
int segment_id = 0; 
FIFO_QUEUE *sharedBuff; 
FIFO_QUEUE *fifo = NULL; 

fifo = makeBuffer(25); 

//-------retrieve COMMAND LINE arguments------// 
if(argc != 2) 
    { 
    printf("getBuffer requires 2 command line args\n"); 
    exit(-1); 
    } 

//------SET UP SHARED MEMORY--------// 

//get memory ID 
segment_id = atoi(argv[1]); 
printf("MAKE_BUFFER: Shared mem seg Id in get buffer = %d\n", segment_id); 

//Attach 
sharedBuff = (FIFO_QUEUE*)shmat(segment_id, NULL, SHM_RND); 

    //COPY contents of fifo into shared mem 
memcpy((void*)sharedBuff, (void*)fifo, 120); 


//--------CLEANUP--------// 

//DETACH shared mem 
shmdt(sharedBuff); 

//deallocate memory 
    rmBuffer(fifo); 

    return 0; 
} 

/* makeBuffer() 
    Description:  
     - Creates a FIFO buffer of integers of size <size> 
*/ 
FIFO_QUEUE *makeBuffer(int size) 
{ 
//variables 
int i = 0; 
FIFO_QUEUE *fifo = NULL; 

//allocate room for our struct 
fifo = (FIFO_QUEUE*)malloc(sizeof(FIFO_QUEUE)); 

//allocate room for our queue 
fifo->queue = (int*)malloc(sizeof(int) * size); 

//set the initial position and number of items in the queue to 0 
fifo->putPos = 0; 
fifo->rmPos = 0; 
fifo->numItems = 5; 
fifo->size = size; 
//return our pointer 
return fifo; 

}

+0

你能張貼'FIFO_QUEUE'的定義是什麼? – hmjd 2012-03-04 21:59:47

+0

你可以發佈'fifoQueue.queue'的動態分配代碼嗎? – hmjd 2012-03-04 22:06:41

回答

1

此:

memcpy((void*)sharedBuff, (void*)fifo, 120); /* Why 120 ? */ 

將從fifo複製120個字節:它不會複製的fifo.queue動態分配的陣列。作爲元素爲queue所要求的數量是硬編碼變化FIFO_QUEUE的定義:

typedef struct fifoQueue{ 
    int queue[25]; 
    int putPos;  //next position to insert to 
    int rmPos;  //next position to remove from 
    int numItems; //number of items currently in the queue 
    int size;  //the max size of the queue: always 25 
}FIFO_QUEUE; 

,改變memcpy()到:

memcpy(sharedBuff, fifo, sizeof(*fifo)); 
+0

謝謝,就像一個魅力工作,我不知道爲什麼我沒有靜​​靜地宣佈隊列在第一位 – cpowel2 2012-03-04 23:51:30