Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3280706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:44:04+00:00 2026-05-17T19:44:04+00:00

I am working on a uni assignment, which is to be done in C.

  • 0

I am working on a uni assignment, which is to be done in C. This is my firs time programming in C, and I am VERY close to having a workable solution, but my code is throwing a Segmentation Fault during runtime and I simply cannot locate the cause.

Here is my code:

#include "./shared.h"
#include "./scheduler.h"

PCB_entry      *ready_q ;
PCB_entry      *ready_q_tail;

/*
 * file name priority.c
 *
 * The functions in this file implement a "priority"
 * scheduling scheme. 
 *
 * Functions provided and brief descriptions:
 *
 * PCB_entry * schedule_next (void) - returns next process for CPU
 * 
 * int insert_new (PCB_entry * proc) - insert new process into queue
 *
 * void list_q (void) - debugging function lists queue contents
 *
 * int re_insert (PCB_entry * proc, int run_time) - put process back
 *                 into correct queue after a run on the CPU 
 *
 * int init_q (void) - initialise queue(s)
 *
 */


double pwr (double x, double y)
{
    double i, p;
    p = 1;
    for (i = 1; i <= y; ++i){
        p = p * x;
    }
    return p;
}


/*
 * Function Schedule_next: priority
 * 
 * Called by the central simulation system Returns a 
 * pointer to the PCB entry for the "process" that 
 * should be put on the CPU next
 * 
 */
PCB_entry *
schedule_next (void)
{
   /* if ready_q is null, there are no processes in the system */
   if (ready_q == NULL)
     {
    return NULL;
     }
   else
     { 
       PCB_entry *next;
       PCB_entry *best_proc;

        next = ready_q;
        best_proc = next;
           while (next != NULL)
             {              /* traverse to the end of the list */

            if (next->current_priority < best_proc->current_priority) {
                best_proc = next;   
            }
            next = next->f_link;
           }
        if (best_proc == NULL) {
            return NULL;
        }

        if (best_proc->b_link != NULL){
            best_proc->b_link->f_link = best_proc->f_link;
            if (best_proc->f_link != NULL) {
                best_proc->f_link->b_link = best_proc->b_link;
            }
        } else {

            ready_q = best_proc->f_link;


            if (ready_q != NULL)    /* don't try to de-reference a NULL
                                     * pointer */
               ready_q->b_link = NULL;  /* first process in the queue
                                         * always has a NULL back
                                         * link */
            best_proc->f_link = NULL;   /* just to be tidy -- set both links
                         * in the PCB */
            /* entry that will be returned to NULL */
            best_proc->b_link = NULL;
        }

        return best_proc;

    }
}

/*
 * Function insert_new: Non-preemptive round-robin
 * 
 * Insert a new "process" into the scheduling queue
 * 
 * Accepts a pointer to a PCB entry that will be inserted
 * into the queue returns either OK or NotOK to indicate 
 * success or failure
 * 
 * Since this is FCFS priority scheduling, the new process is
 * simply slotted in at the end of the queue
 * 
 */
int
insert_new (PCB_entry * proc)
{

   if (ready_q == NULL)
     {              /* no entries in table */
    ready_q = proc;     /* insert at the head of the list */
    proc->b_link = NULL;
    proc->f_link = NULL;
    ready_q_tail = ready_q;
    return OK;
     }
   else
     {

        ready_q_tail->f_link = proc; /* Set tail of list pointer */
        proc->f_link = NULL;         /* New tail of list to NULL */
        proc->b_link = ready_q_tail; /* Set the b_link of the new process to previous last record of the list */
        ready_q_tail = proc;         /* Set the tail reference to the new pointer */
    return OK;
     }
#pragma error_messages (off,E_STATEMENT_NOT_REACHED)
   return NotOK; /* this is not really needed, but is here to be defensive */
#pragma error_messages (on,E_STATEMENT_NOT_REACHED)
}

/*
 * Function list_q
 * 
 * Implementation of this function is optional but highly 
 * recommended
 */

void
list_q (void)
{
   PCB_entry *next;

   next = ready_q;

   fprintf (stderr, "\n current state of the ready queue\n");

   next = ready_q;
   while (next != NULL)
     {              /* traverse to the end of the list */
    fprintf (stderr, "%d\t", next->pid);
    next = next->f_link;
     }
   fprintf (stderr, "\n\n");
}


/*
 * Function re_insert: priority
 * 
 * a function to insert a process back into the queue
 * following a run on the CPU. Depending on the
 * scheduling algorithm chosen this would need to
 * do a lot more. In a priority algorithm with boost
 * and reduction, it would need to look at the
 * percentage of the quantum that the process used
 * and determine if a change to the priority was
 * required. Also, in implementing a mulitlevel
 * priority scheme, a variation of the ready_q
 * pointer would be required. The simplest method
 * would be an array of pointers with one element
 * for each priority level.
 *
 * Not that, in this case it is identical to the
 * insert_new code
 */
int
re_insert (PCB_entry * proc, int run_time)
{
    insert_new(proc);
}


/*
 * Function init_q: FCFS priority
 * 
 * Initialises the ready queue
 * 
 * Written as a function so that, if the ready_q
 * becomes an array of pointers, the initialisation
 * can be changed without re-building the main part
 * of the simulator
 */

int
init_q (void)
{
   ready_q = NULL;
    ready_q_tail = NULL;    
   return OK;
}



/*
 * function end_run () 
 * Insert code to do any end of processing tasks for the
 * functions written by the student
 */
int end_run(void)
{
   fprintf(stderr, "This run had %d concurrent processes\n", Get_num_concurrent());
   return 0;
}

Can anyone please help – I really dont want to fail this assignment (due in less than 24 hours). So far, its not looking good.

UPDATE:

I’ve gone and added a bucketload of fprintf’s all over the place, and after a while I think I have isolated the problem at this line:

if (best_proc->f_link != NULL) {

I dont know how this helps me, but in the vain hope that it helps someone see the problem (because I sure dont), then so much the better.

Also, just to be clear, I dont believe this is a multithreaded application (although I am a complete noob, so who really knows) – and I didn’t know I could HAVE a debugger or a stack-trace (working from 1st edition K & R “The C Programming Language”) so I dont have those either. (On a side note, does programming in C normally feel like a fat japanese man is punching you in the face?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:44:05+00:00Added an answer on May 17, 2026 at 7:44 pm

    It looks alright to me – can’t find any errors in the code.

    Does it fail immediately, or after having run a little while? Are you sure that the init_q function actually runs before starting to use the queue? Have you tried checking if you’re getting NULL as a parameter to new_insert or re_insert functions?

    By the way, this program is not running in multi-threaded mode, right? If it does you need to protect your global variables in the critical sections.

    EDIT4:
    NOW I think I got it! ready_q_tail is never set to new value when removing the last task from the queue.

    Here’s a (hopefully) fixed schedule_next function:

    PCB_entry *
    schedule_next (void)
    {
       /* if ready_q is null, there are no processes in the system */
       if (ready_q == NULL)
         {
        return NULL;
         }
       else
         { 
           PCB_entry *next;
           PCB_entry *best_proc;
    
            next = ready_q;
            best_proc = next;
               while (next != NULL)
                 {              /* traverse to the end of the list */
    
                if (next->current_priority current_priority) {
                    best_proc = next;   
                }
                next = next->f_link;
               }
            if (best_proc == NULL) {
                return NULL;
            }
    
            // Remove task from queue
            {
                int procHasNext = best_proc->f_link != NULL;
                int procHasPrev = best_proc->b_link != NULL;
    
                if (!procHasNext && !procHasPrev) {
                    // There's only one task in the queue
                    ready_q = NULL;
                    ready_q_tail = NULL;
                } else {
                    if (procHasNext) {
                        if (procHasPrev) {
                            best_proc->b_link->f_link = best_proc->f_link;
                        } else {
                            // Proc to remove is the first in queue
                            ready_q = best_proc->f_link;
                            ready_q->b_link = NULL;
                        }
                    }
    
                    if (procHasPrev) {
                        if (procHasNext) {
                            best_proc->f_link->b_link = best_proc->b_link;
                        } else {
                            // Proc to remove is last in queue
                            ready_q_tail = best_proc->b_link;
                            ready_q_tail->f_link = NULL;
                        }
                    }
                }
            }
    
            // Ensure that the links in PCB to remove doesn't cause any problems
            best_proc->f_link = NULL;
            best_proc->b_link = NULL;
    
            return best_proc;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with Excel interop, I'm trying be very careful about not implicitly creating any
Working with MySQL lately, from PHP, I am wondering about this: What is the
Working on trying out the market licensing service, and I'm running into a few
Working on an html5 app for a touchscreen windows xp computer that will be
Working with spring, below, the code of applicationContext-service.xml: <bean id=mediaObjectService class=path.MediaObjectServiceImpl> <property name=mediaObjectDao >
Working on a legacy ASP.NET application we've found that ASP.NET session gets used for
Working on site development using Drupal. Using View module also. I did defined severar
Working on Windows with VS2005 and struggling to understand the error messages I'm getting.
I have a wierd problem that i need to work out how to resolve,
Hi all , i'm testing out a service for internal ads on our website...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.