2016-06-21 118 views
0

分配用於一個隊列的前部元件存儲器I具有在文件queue.h節點和隊列結構分段故障而用C

#ifndef QUEUE_H 
#define QUEUE_H 

#include<stdio.h> 
#include<stdbool.h> 

typedef struct rcb 
{ 
    int seq_no;  
    int file_desc; 
    FILE *fp;  
    long sent;  
    long sizeOfFile; 
    char *request_http_method; 
    char *request_file; 
    char *request_http_version; 
    int level;   
    bool is_new_created; 
    int remaining; 
}* RCB;  

/* 
* Node of the queue. 
*/ 
typedef struct node { 
    RCB info;  //Data of each node. 
    struct node *ptr;//pointer to the next node. 
} * Node; 

/* 
* Queue for the requests. 
*/ 
typedef struct req_queue { 
    Node front;  //front node. 
    Node rear;  //rear node. 
    int size;  //size of the queue. 
} * Queue; 


/** 
* Create the queue. 
*/ 
void create(Queue queue); 


/** 
* For inserting an item to the queue in sorted order of file size. 
* @param data 
*/ 
void enque_SJF(Queue queue, RCB data); 

#endif 

queue.c

#include "queue.h" 

Node temp, front1; 
int count = 0;  

/** 
* For creating the queue. 
*/ 
void create(Queue queue) { 
    queue->front = queue->rear = NULL; 
    queue->size=0; 
} 

/** 
* Enqueing in the order of increasing file size. 
* @param data 
*/ 
void enque_SJF(Queue que, RCB data) { 
    bool found = false; 
    Node temp = que->front; 
    while (!found) { 
     if (que->front == NULL) { //if the queue is empty. 
      que->front = malloc(sizeof (struct node)); 
      que->front->ptr = NULL; 
      que->front->info = data; 
      que->rear = que->front; 
      break; 
     } else { 
      if (temp->ptr == NULL) { 
       Node newnode = (struct node *) malloc(1 * sizeof (struct node)); 
       newnode->info = data; 
       if (temp->info->sizeOfFile >= data->sizeOfFile) { 
        newnode->ptr = temp; 
        que->front = newnode; 
        break; 
       } else { //else enqueue at the rear. 
        temp->ptr = newnode; 
       } 
      } else { 
       if (temp == que->front && temp->info->sizeOfFile >= data->sizeOfFile) { 
        Node newnode = (struct node *) malloc(1 * sizeof (struct node)); 
        newnode->info = data; 
        newnode->ptr = temp; 
        que->front = newnode; 
        break; 
       } 

       if (temp->ptr->info->sizeOfFile >= data->sizeOfFile) { 
        Node newnode = (struct node *) malloc(1 * sizeof (struct node)); 
        newnode->info = data; 
        newnode->ptr = temp->ptr; 
        temp->ptr = newnode; 
        break; 
       } else 
        temp = temp->ptr; 
      } 
     } 
    } 
    que->size++; 
} 

我試圖排隊queue.c文件中的函數enque_SJF中的隊列的新節點。 enqueue函數由函數serve_client函數在sws.c中調用。這裏是sws.c這些文件中有更多的功能,但它們與我的問題沒有關係,所以我沒有寫出所有的功能都更簡單;

#include "Queue.h" 
#include "network.h" 
#include "schedulers.h" 
#include "shared.h" 

char scheduler[4]; 
pthread_t tid[2]; 
int port; 
Queue req_queue; 
bool flag[2]; 
int turn; 
int sequence_number; 


void *serve_client() 
{ 
    static char *buffer;        /* request buffer */ 
    int fd;  

    req_queue = (struct req_queue *) malloc(sizeof (struct req_queue)); 
    create(req_queue); 

    if (port != 0) 
    { 
     network_init(port);        /* init network module */ 
     fprintf(stderr, "Connection port %d\n", port); 
     for(;;) 
      /* main loop */ 
     { 
      network_wait(); 
      if(!buffer)          /* 1st time, alloc buffer */ 
      { 
       buffer = malloc(MAX_HTTP_SIZE); 
       if(!buffer)         /* error check */ 
       { 
        perror("Error while allocating memory"); 
        abort(); 
       } 
      } 

      for(fd = network_open(); fd >= 0; fd = network_open()) /* get clients */ 
      { 
       memset(buffer, 0, MAX_HTTP_SIZE); 
       if(read(fd, buffer, MAX_HTTP_SIZE) <= 0)  /* read req from client */ 
       { 
        perror("Error while reading request"); 
        abort(); 
       } 
       printf("file path %s\n",buffer); 
       //Initializing memory for the job. 
       RCB request = (RCB) malloc(1 * sizeof (struct rcb)); 

       //breaking the request in appropriate format. 
       request-> request_http_method = strtok(buffer, " "); //request method. 
       request->request_file = strtok(NULL, " /"); //request file 
       request->request_http_version = strtok(NULL, "\n"); //HTTP version 
       request->file_desc = fd; 
       request->level = 1; // for multilevel scheduler. 
       request->seq_no = sequence_number; 
       sequence_number++; //increment global counter. 


        enque_SJF(req_queue, request); //Enqueue for Shortest Job First. 


      } 
     } 
    } 
    return 0; 
} 


bool isValidRequest(RCB request) 
{ 

    // the request is parsed and checked the validity 
} 


void *SJF() 
{ 
    // function implemented 
} 




int main(int argc, char **argv) 
{ 
    //default port, if no port is supplied. 
    /* check for and process parameters 
    */ 
    if((argc < 3) || (sscanf(argv[1], "%d", &port) < 1) || (sscanf(argv[2], "%s", scheduler) < 1)) 
    { 
     printf("port %d\n",port); 
     printf("port %s\n",scheduler); 
     printf("usage: sms <port> <scheduler>\n"); 
     return 0; 
    } 
    sequence_number = 1; //counter for number of requests. 
    if (argc == 3) 
    { 
     port = atoi(argv[1]); 
    } 
    printf("port %d\n",port); 
    pthread_create(&(tid[0]), NULL, serve_client, NULL); 

    if(strcmp(scheduler,"SJF") ==0) 
    { 
     pthread_create(&(tid[1]), NULL, SJF, NULL); 
    } 
    else if(strcmp(scheduler,"RR")==0) 
    { 
     pthread_create(&(tid[1]), NULL, Round_Robin, NULL); 
    } 
    else if(strcmp(scheduler,"MLFB")==0) 
    { 
     pthread_create(&(tid[1]), NULL, MultilevelQueueWithFeedback, NULL); 
    } 
    else 
    { 
     printf("Scheduler Algorithm is not defined. Please enter one of them; SJF, RR, MLFB"); 
     return 0; 
    } 

    pthread_join(tid[0], NULL); 
    pthread_join(tid[1], NULL); 


    return 0; 


} 

在給queueu添加一個新節點時,我在下一行發現了一個分段錯誤錯誤;

que->front->ptr = NULL; 

儘管調試,我看到,經過存儲器分配的地址que->前仍然爲0x0。有沒有建議爲什麼它不分配內存?

+1

[請參閱此討論關於爲什麼不在'C'中投射'malloc()'和家族的返回值。](http://stackoverflow.com/q/605845/2173917)。 –

+0

,請勿鍵入指針。 –

+0

檢查'malloc()'是否成功。 –

回答

1

這就是爲什麼你應該總是執行被調用函數的成功檢查。

如果malloc()失敗,它將返回一個NULL指針,該指針被存儲在que->front中。在那之後,沒有一個NULL檢查,如果您嘗試訪問

que->front->ptr 

你會去引用NULL指針(即,訪問無效存儲器)調用undefined behavior

總是對malloc()的返回值進行NULL檢查。