Hey everyone,
I was assigned a project where we have to simulate virtual memory page replacement algorithms. I decided to use a Queue ADT I made in a previous project, and modify it to enqueue page reference strings of a process, character by character. However, when I initially create the Queue, I get a Bus Error, which is weird, because I haven't changed the create() function in my queue. I was wondering if you could take a look. I'll include a couple of the necessary headers, main.c, and the parts of queue.c i modified as well as the create() function, which I did not modify.
Thanks
Here is main.c
#include "process.h"
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
process *p;
p->pid = 1;
p->reference_string = "000111222";
Queue q = create();
int i = 0;
//Debugging the bus error, was found two lines up
//while(p->reference_string[i] != '\0'){
// enqueue(q, p, i);
// printf("%c\n", p->reference_string[i]);
// i++;
// }
//printf("%s\n", p->reference_string);
//print_queue(q);
return 0;
}
Here is queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include <stdbool.h>
#include <stdio.h>
#include "process.h"
typedef struct linked_list *Queue;
Queue create(void);
void destroy(Queue q);
bool is_empty(Queue q);
bool is_full(Queue q);
int get_size(Queue q);
void enqueue(Queue q, process *p, int index);
char dequeue(Queue q);
void print_queue(Queue q);
#endif
here is process.h
#ifndef PROCESS_H
#define PROCESS_H
typedef struct{
int pid;
struct page_table *pt;
char * reference_string;
}process;
#endif
and the parts of queue.c :
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include "process.h"
#include "queue.h"
struct node{
char element;
struct node *next;
};
struct linked_list{
struct node *head;
struct node *tail;
int size;
};
struct page_table{
int pages[256];
bool paged[256];
};
struct page_frame_table{
int pages[256];
bool used [256];
};
bool free_frame_table[256];
Queue create(void)
{
Queue q = malloc(sizeof(struct linked_list));
if(q == NULL)
terminate("Error in malloc: could not allocate space for a Queue");
q->head = NULL;
q->tail = NULL;
q->size = 0;
}
/*
*enqueue: adds an element to the back of the queue
*/
void enqueue(Queue q, process *p, int index)
{
struct node *new_node = malloc(sizeof(struct node));
if(new_node == NULL)
terminate("Error in enqueue: couldn't allocate memory for a node");
new_node->element = p->reference_string[index];
if(is_empty(q)){
q->head = new_node;
q->tail = new_node;
q->size ++;
new_node->next = NULL;
}
else{
q->tail->next = new_node;
q->tail = q->tail->next;
q->tail->next = NULL;
q->size++;
}
}
/*
* dequeue: removes an element from the head of the queue
*/
char dequeue(Queue q)
{
if(is_empty(q)){
printf("Queue is empty\n");
return -1;
}
struct node *n = q->head;
if(get_size(q) == 2){
q->head = q->head->next;
q->head->next = NULL;
q->tail = q->head;
q->size--;
}
if(get_size(q) == 1){
q->head = NULL;
q->tail = NULL;
q->size--;
}
else{
q->head = q->head->next;
q->size--;
}
char p = n->element;
return p;
}