Round Robin Scheduling using c.

This is a discussion on Round Robin Scheduling using c. within the C Programming forums, part of the General Programming Boards category; i currently doing round robing scheduling simulation using c. but there is 1 error which i don't how to correct ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    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

    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. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Do more checking for NULL pointers.

    Why all the extraneous semicolons at the end of brace-enclosed code blocks?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Let me guess, the error is something like "cannot convert void * to struct process *". I'd bet my next paycheck that you're trying to compile C code as C++, and the stricter type checking is smacking you in the face. Make sure you're compiling as C and it should work.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    And what happened to the indentation of the code?

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    I finally found the correct way -

    You should convert the void pointer to your object pointer.
    Like this:
    Code:
    proc = (struct process*)malloc(sizeof(struct process));

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No you shouldn't. In C you NEVER need to typecast malloc. Read Slacker's reply.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    because the C++ implementation has undefined all of C's macros, although I'm not sure about malloc. Is malloc a system call?
    Not all of those are macros, most of them are functions...big difference. C++ is required to implement the standard C library in its entirety (but only the C89 library presently), so saying that it has undefined all of them isn't entirely accurate, even though that line of thought does promote "correct" C++ code. And no, malloc isn't a system call, even though it probably does make a system call to do its job.

  8. #8
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >The code, including the comment, is from GCC 4.0
    Implementations can do what they want as long as the result is conforming to the standard. The problem here is that you said "the C++ implementation" instead of "my C++ implementation". There's more than one, and they're all different. Actually, assuming that an implementation's quirks are always accepted or required by the standard is a common problem.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The point, which you will find very quickly if you read the FAQ, is that in C99 a void pointer can be assigned to any other pointer without any hassles, whereas in C++ you need a cast.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. round robin algorithm!
    By visham in forum C Programming
    Replies: 9
    Last Post: 10-13-2005, 03:14 AM
  3. Almost finished with Round Robin algorithm
    By Shinobi-wan in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2004, 02:00 PM
  4. Countout (Round Robin) Game
    By Hexxx in forum C Programming
    Replies: 12
    Last Post: 12-22-2003, 06:58 AM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21