GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Dec-2005, 17:25
eclipt eclipt is offline
New Member
 
Join Date: Dec 2005
Posts: 5
eclipt is on a distinguished road
Question

round robin scheduling using c


i currently doing round robing scheduling simulation using c.
but there is 1 error which i don't how to correct it.
the error is in the line "proc = malloc(sizeof(struct process));"
i hope someone could tell me how to correct this problem

CPP / C++ / C Code:
 
/* Round Robin Scheduling Simulation*/

#include <stdio.h>
#include <stdlib.h>

//////////////////////////////////////////////////////////////////////////
/* Process Data Structure */
struct process {
int pid; /* Process ID */
int burst; /* CPU Burst Time */
int priority; /* Priority */
int working; /* Working time, for round-robin scheduling */
int waiting; /* Waiting time, for round-robin scheduling */
struct process *next;
};
//////////////////////////////////////////////////////////////////////////
/* Function Prototype Declarations */
struct process *init_process (int pid, int burst, int priority);
void listprocs (struct process *proc);
void rr (struct process *proc, int quantum);
//////////////////////////////////////////////////////////////////////////
/* Main Program Segment */
int main (void) {
/* Initialize process list */
struct process *plist, *ptmp;
plist = init_process(1, 10, 3);
plist->next = init_process(2, 1, 1); ptmp = plist->next;
ptmp->next = init_process(3, 2, 3); ptmp = ptmp->next;
ptmp->next = init_process(4, 1, 4); ptmp = ptmp->next;
ptmp->next = init_process(5, 5, 2);
////////////////////////////////////////////////////////////////////////// 
/* Perform simulations */
listprocs(plist);
rr(plist, 1);
/* Terminate cleanly */
while (plist != NULL) {
ptmp = plist;
plist = plist->next;
free(ptmp);
};
return(0);
};
////////////////////////////////////////////////////////////////////////// 
/* Process list entry initialization routine */

struct process *init_process (int pid, int burst, int priority) {
struct process *proc;
proc = malloc(sizeof(struct process));
if (proc == NULL) {
printf("Fatal error: memory allocation failure.\nTerminating.\n");
exit(1);
};
proc->pid = pid;
proc->burst = burst;
proc->priority = priority;
proc->working = 0;
proc->waiting = 0;
proc->next = NULL;
return(proc);
};
//////////////////////////////////////////////////////////////////////////
/* Process listing */
void listprocs (struct process *proc) {
struct process *tmp = proc;

printf("BEGIN:\tProcess Listing\n");

while (tmp != NULL) {
printf("PID: %d\t\tPriority: %d\tBurst: %d\n", tmp->pid, tmp->priority, tmp->burst);
tmp = tmp->next;
};

printf("END:\tProcess Listing\n\n");
}; 
//////////////////////////////////////////////////////////////////////////
/* Round-Robin scheduling simulation */
void rr (struct process *proc, int quantum) {
int jobsremain, passes;
struct process *copy, *tmpsrc, *tmp, *slot;

printf("BEGIN:\tRound-Robin scheduling simulation (Quantum: %d)\n", quantum);
/* Duplicate process list */
tmpsrc = proc;
copy = tmp = NULL;
while (tmpsrc != NULL) {
if (copy == NULL) {
copy = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
tmp = copy;
} else {
tmp->next = init_process(tmpsrc->pid, tmpsrc->burst, tmpsrc->priority);
tmp = tmp->next;
};
tmpsrc = tmpsrc->next;
};
//////////////////////////////////////////////////////////////////////////
/* Main routine */
jobsremain = 1;
slot = NULL;
while (jobsremain) {
jobsremain = 0;
//////////////////////////////////////////////////////////////////////////
/* Pick next working slot */
if (slot == NULL) {
slot = copy;
jobsremain = 1;
} else {
passes = 0;
do {
if (slot->next == NULL) {
passes++;
slot = copy;
} else {
slot = slot->next;
};
} while (passes <= 2 && slot->burst == slot->working);
if (passes <= 2) {
jobsremain = 1;
};
};
//////////////////////////////////////////////////////////////////////////
/* Perform a cycle */
tmp = copy;
while (tmp != NULL) {
if (tmp->burst > tmp->working) {
if (tmp == slot) {
tmp->working += quantum;
} else {
tmp->waiting += quantum;
};
};
tmp = tmp->next;
};
};
//////////////////////////////////////////////////////////////////////////
/* Display statistics and clean up copy */
tmp = copy;
while (tmp != NULL) {
printf("Process: %d\tWorking: %d\tWaiting: %d\tTurnaround: %d\n", tmp->pid, tmp->working, tmp->waiting, tmp->working + tmp->waiting);
tmpsrc = tmp;
tmp = tmp->next;
free(tmpsrc);
};

printf("END:\tRR scheduling simulation\n\n");
};
  #2  
Old 27-Dec-2005, 17:34
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: round robin scheduling using c


Hi eclipt,
Welcome to the GIDForums.
And thank you for reading the guidelines.

First, lets look at the syntax of the malloc function:
Quote:
malloc
CPP / C++ / C Code:
   void* malloc(size_t size); 
The function allocates an object of size size, and returns the address of the object if successful; otherwise, it returns a null pointer. The values stored in the object are indeterminate. You can safely convert the return value to an object pointer of any type whose size is not greater than size.


Read the last sentence?
You should convert the void pointer to your object pointer.
Like this:
CPP / C++ / C Code:
proc = (struct process*)malloc(sizeof(struct process));

You should format your code for better readability..
Please take a look here...

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hotmail has been hacked, is there a trojan/virus going round? Hood Computer Software Forum - Windows 5 08-Jul-2005 13:19

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:30.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.