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

  • Home
  • SEARCH
  • 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 6768715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:06:26+00:00 2026-05-26T15:06:26+00:00

I am developing a userspace premptive thread library(fibre) that uses context switching as the

  • 0

I am developing a userspace premptive thread library(fibre) that uses context switching as the base approach. For this I wrote a scheduler. However, its not performing as expected. Can I have any suggestions for this.
The structure of the thread_t used is :

typedef struct thread_t {
    int thr_id;
    int thr_usrpri;
    int thr_cpupri;
    int thr_totalcpu;
    ucontext_t thr_context;
    void * thr_stack;
    int thr_stacksize;
    struct thread_t *thr_next;
    struct thread_t *thr_prev;
} thread_t;

The scheduling function is as follows:

void schedule(void)
{
        thread_t *t1, *t2;
    thread_t * newthr = NULL;
    int newpri = 127;
    struct itimerval tm;
    ucontext_t dummy;
    sigset_t sigt;


    t1 = ready_q;

    // Select the thread with higest priority
    while (t1 != NULL)
    {
        if (newpri > t1->thr_usrpri + t1->thr_cpupri)
        {
            newpri = t1->thr_usrpri + t1->thr_cpupri;
            newthr = t1;
        }

        t1 = t1->thr_next;
    }

    if (newthr == NULL)
    {
        if (current_thread == NULL)
        {
            // No more threads? (stop itimer)
            tm.it_interval.tv_usec = 0;
            tm.it_interval.tv_sec = 0;
            tm.it_value.tv_usec = 0; // ZERO Disable
            tm.it_value.tv_sec = 0;
            setitimer(ITIMER_PROF, &tm, NULL);
        }
        return;
    }
    else
    {
        // TO DO :: Reenabling of signals must be done.
        // Switch to new thread
        if (current_thread != NULL)
        {
            t2 = current_thread;
            current_thread = newthr;
            timeq = 0;
            sigemptyset(&sigt);
            sigaddset(&sigt, SIGPROF);
            sigprocmask(SIG_UNBLOCK, &sigt, NULL);
            swapcontext(&(t2->thr_context), &(current_thread->thr_context));
        }
        else 
        {
            // No current thread? might be terminated
            current_thread = newthr;
            timeq = 0;
            sigemptyset(&sigt);
            sigaddset(&sigt, SIGPROF);
            sigprocmask(SIG_UNBLOCK, &sigt, NULL);
            swapcontext(&(dummy), &(current_thread->thr_context));
        }
    }
}
  • 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-26T15:06:26+00:00Added an answer on May 26, 2026 at 3:06 pm

    It seems that the “ready_q” (head of the list of ready threads?) never changes, so the search of the higest priority thread always finds the first suitable element. If two threads have the same priority, only the first one has a chance to gain the CPU. There are many algorithms you can use, some are based on a dynamic change of the priority, other ones use a sort of rotation inside the ready queue. In your example you could remove the selected thread from its place in the ready queue and put in at the last place (it’s a double linked list, so the operation is trivial and quite inexpensive).
    Also, I’d suggest you to consider the performace issues due to the linear search in ready_q, since it may be a problem when the number of threads is big. In that case it may be helpful a more sophisticated structure, with different lists of threads for different levels of priority.
    Bye!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Developing a web application that I've registered with Twitter. In this app, I might
I am developing a active messaging protocol for parallel computation that replaces TCP/IP. My
Developing for iPhone, I have a collection of points that I need to make
Developing a VSS writer app: see this error during backup, initiated by windows backup
I don't understand why the ABI is important context of developing user-space applications. Is
Developing a .net library based on an industry standard. The standard includes data structures
Developing a little survey webapp, ran into problem that deals with ranges for rating
Developing an application for Android, i want to record data that will be usefull
I am relatively new to C#/.Net. I'm developing a desktop application that requires multi
Developing is more of a hobby at the moment; so apologies if this is

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.