Thanks for your help in advance ^^
Ok, my problem is that every time I run the program I get a Segmentation Fault... I think it's because the memory isn't allocating properly, could someone please take a look? Thanks <3
Queue.h
Code:
#include <stdio.h>
#include <string.h>
typedef struct node Node;
typedef struct queue Queue;
struct node
{
char *process;
struct node *next;
};
struct queue
{
struct node *head;
struct node *tail;
};
Node* newNode(char* p);
Queue* newQueue(Queue *q);
void printQueue(Queue *q);
void enqueue(Queue *q, char *p);
char* dequeue(Queue *q);
Queue* newStack(Queue *q);
void push(Queue *q, char *p);
char* pop(Queue *q);
Queue.c
Code:
#include "Queue.h"
#include <stdio.h>
#include <string.h>
Node* newNode(char* p)
{
Node *n = (Node*)malloc(sizeof(int[10]));
n->process = p;
n->next = NULL;
return n;
}
Queue* newQueue(Queue *q)
{
q = (Queue*)malloc(sizeof(int[10]));
q->head = NULL;
q->tail = NULL;
return q;
}
Queue* newStack(Queue *q)
{
q = (Queue*)malloc(sizeof(int[10]));
Node *n = newNode("IDLE");
q->head = n;
return q;
}
void printQueue(Queue *q)
{
Node *p;
p = q->head;
for(p; p != NULL; p = p->next)
printf("%s ", p->process);
printf("\n");
}
void enqueue(Queue *q, char *p)
{
Node *temp;
temp = newNode(p);
if(q->head == NULL)
{
q->head = temp;
q->tail = q->head;
return;
}
else if(q->head == q->tail)
{
q->tail = temp;
q->head->next = q->tail;
return;
}
q->tail->next = temp;
q->tail = q->tail->next;
}
char* dequeue(Queue *q)
{
char *r;
if(q->head == NULL)
return NULL;
if(q->head == q->tail)
{
r = q->head->process;
q->head->process = NULL;
q->head->next = NULL;
q->tail = q->head;
return r;
}
r = q->head->process;
q->head = q->head->next;
return r;
}
void push(Queue *q, char *p)
{
Node *n = newNode(p);
n->next = q->head;
q->head = n;
}
char* pop(Queue *q)
{
char *temp = q->head->process;
if(q->head->next == NULL)
return temp;
q->head = q->head->next;
return temp;
}
/*
int main()
{
Queue *q; Node *n;
q = newQueue(q);
printQueue(q);
enqueue(q, "A");
printQueue(q);
enqueue(q, "B");
printQueue(q);
enqueue(q, "C");
printQueue(q);
enqueue(q, "D");
printQueue(q);
dequeue(q);
printQueue(q);
dequeue(q);
printQueue(q);
printf("\n\n\n");
Queue *s = newStack(s);
printQueue(s);
push(s, "A");
printQueue(s);
push(s, "B");
printQueue(s);
pop(s);
printQueue(s);
pop(s);
printQueue(s);
pop(s);
printQueue(s);
}
*/
main.c
Code:
#include "Queue.h"
#include <stdio.h>
#include <string.h>
typedef struct cpu CPU;
struct cpu
{
struct queue *Running;
struct queue *Ready;
struct queue *Waiting;
};
CPU* newCPU(CPU *c)
{
c = (CPU*)malloc(sizeof(int[10]));
c->Running = newStack(c->Running);
c->Ready = newQueue(c->Ready);
c->Waiting = newQueue(c->Waiting);
}
void event1(CPU *cpu)
{
printf("**Event 1 Occurs\n");
// if nothing in Ready queue, process remains in Running Queue
if (cpu->Ready->head != NULL)
{
// Moves the running Process to the Ready Queue
enqueue(cpu->Ready, pop(cpu->Running));
}
// Moves head of Ready Queue to the Running Stack
push(cpu->Running, dequeue(cpu->Ready));
}
void event2(CPU *cpu)
{
printf("**Event 2 Occurs\n");
// Running process goes to Waiting Queue
enqueue(cpu->Waiting, pop(cpu->Running));
// Moves head of Ready Queue to the Running Stack
push(cpu->Running, dequeue(cpu->Ready));
}
void event3(CPU *cpu)
{
printf("**Event 3 Occurs\n");
// If Ready Queue is empty and Running state is in IDLE
if (cpu->Running->head == NULL && cpu->Ready->head == NULL)
push(cpu->Running, dequeue(cpu->Waiting));
// Waiting Process goes to Ready state
enqueue(cpu->Ready, dequeue(cpu->Waiting));
}
CPU* initialize(CPU *cpu)
{
push(cpu->Running, "1");
enqueue(cpu->Ready, "5");
enqueue(cpu->Ready, "4");
enqueue(cpu->Ready, "3");
enqueue(cpu->Ready, "2");
enqueue(cpu->Waiting, "6");
enqueue(cpu->Waiting, "7");
enqueue(cpu->Waiting, "8");
enqueue(cpu->Waiting, "9");
return cpu;
}
void printCPU(CPU *cpu)
{
printf("\n**Print CPU **\nRunning: ");
printQueue(cpu->Running);
printf("Ready: ");
printQueue(cpu->Ready);
printf("Waiting: ");
printQueue(cpu->Waiting);
}
int main()
{
CPU *cpu = newCPU(cpu);
printf("%s\n", cpu->Running->head->process);
// printCPU(cpu);
// initialize(cpu);
}