Skip to content

Instantly share code, notes, and snippets.

@karngyan
Last active March 14, 2019 11:30
Show Gist options
  • Save karngyan/85fe3790b1d72882cacd8cde7cbdf021 to your computer and use it in GitHub Desktop.
Save karngyan/85fe3790b1d72882cacd8cde7cbdf021 to your computer and use it in GitHub Desktop.
C Linked Queue implementation
/*
Author: @karngyan
Complete the run_scheduler function as per your requiremnts
tweak take_input function as required
To Do:
- Add function to print gantt chart
- Add function to print Process Table
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
typedef struct pcb{
int p_id,
arrival_time,
priority,
burst_time,
start_time,
end_time,
turn_around_time,
waiting_time;
} pcb;
typedef struct process{
pcb x;
struct process *next;
} node;
typedef struct queue{
node *front , *rear;
} queue;
node* new_node(int p_id , int burst_time ,
int arrival_time, int priority)
{
node * tmp = (node *)malloc(sizeof(node));
(tmp->x).p_id = p_id;
(tmp->x).burst_time = burst_time;
(tmp->x).arrival_time = arrival_time;
(tmp->x).priority = priority;
return tmp;
}
queue * create_queue()
{
struct queue *q = (queue *)malloc(sizeof(queue));
q->front = q->rear = NULL;
return q;
}
void daal(queue * q , node *process)
{
if(q->rear == NULL)
{
q -> front = q -> rear = process;
return;
}
q->rear->next = process;
q->rear = process;
return;
}
node* nikaal(queue *q)
{
if(q->front == NULL)
return NULL;
node * tmp = q->front;
q->front = q->front->next;
if(q->front == NULL)
q->rear = NULL;
return tmp;
}
void take_input(int *n , queue *input_queue)
{
pcb x;
int i;
printf("Enter number of processes : ");
scanf("%d" , n);
int p_id , arrival_time , burst_time , priority;
printf("\nEnter p_id , arrival_time , burst_time , priority: \n");
for(i = 0 ; i<(*n) ; ++i)
{
scanf("%d %d %d %d" ,
&p_id , &arrival_time , &burst_time , &priority);
node* process = new_node(p_id , burst_time ,
arrival_time , priority);
daal(input_queue , process);
}
}
int print_queue(queue *q)
{
/*
return value:
0 if queue empty
1 if successful printing
*/
if(q->rear == NULL)
return 0;
node * tmp = q->front;
printf("FRONT\n");
while(tmp != NULL)
{
printf("p_id = %d\n" , (tmp->x).p_id);
tmp = tmp->next;
}
printf("BACK\n");
return 1;
}
void run_scheduler(queue* input_queue , queue* ready_queue)
{
}
signed main()
{
int n;
printf("\t-- {NAME of SCHEDULING ALGO - HERE} -- \n\n");
queue *input_queue = create_queue();
queue *ready_queue = create_queue();
take_input(&n , input_queue);
if(!print_queue(input_queue))
{
printf("Input Queue Empty!\n\n");
return 0;
}
// run_scheduler(input_queue , ready_queue);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment