Find the answer to your Linux question:
Results 1 to 3 of 3
Hi.. I am trying to create 2 pthreads from my main thread with different scheduling priorities. I want to create a scenario when one of my threads (running at higher ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    2

    Implementing priority based preemtive pthread


    Hi..

    I am trying to create 2 pthreads from my main thread with different scheduling priorities. I want to create a scenario when one of my threads (running at higher priority) is not preempted by the other thread (which is at lower priority) until it finishes its job.

    For this purpose, I have set different priorities (5, 10) to my threads and have set the scheduling algorithm to SCHED_FIFO for both of them.
    I am using pthread_setschedparam().
    Within both of my threads, I have put infinite loops.

    I would expect the thread running at higher priority to continue indefinitely. However in this case both the threads are getting executed in a round robin fashion.
    My kernel version is - 2.6.18-1.2798.fc6xen

    Can anyone please help me ? Will appreciate a response.

    Below is the code snippet for reference.

    #include <stdio.h>
    #include <unistd.h>
    #include <linux/unistd.h>
    #include <sys/types.h>
    #include <sys/syscall.h>
    #include <sched.h>
    #include <pthread.h>
    #include <string.h>
    #include <errno.h>

    pthread_t thread1, thread2;

    void (*FuncPointer)(int);

    void Func1(void * pI)
    {
    int i = 0, err = 0;
    struct sched_param new_sched_param;

    memset(&new_sched_param, 0, sizeof(struct sched_param));

    new_sched_param.sched_priority = 10;
    err = pthread_setschedparam(thread1, SCHED_FIFO, &new_sched_param);
    if(0 != err) {
    printf("Inside Func1..pthread_setschedparam failed with %d", err);
    return;
    }

    for(; ; ) {
    printf("Inside Func1 - Running with priority %d.\n", new_sched_param.sched_priority);
    }

    return;
    }

    void Func2(void * pI)
    {
    int i = 0, err = 0;
    struct sched_param new_sched_param;

    memset(&new_sched_param, 0, sizeof(struct sched_param));

    new_sched_param.sched_priority = 10;
    err = pthread_setschedparam(thread2, SCHED_FIFO, &new_sched_param);
    if(0 != err) {
    printf("Inside Func2..pthread_setschedparam failed with %d", err);
    return;
    }
    for(; ; ) {
    printf("Inside Func2 - Running with priority %d.\n", new_sched_param.sched_priority);
    }

    return;
    }

    int main()
    {
    int err = 0;

    printf("Entered main....Starting threads..\n");

    pthread_create(&thread1, NULL, Func1, NULL);
    pthread_create(&thread2, NULL, Func2, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    return 0;
    }

  2. #2
    Just Joined!
    Join Date
    Nov 2009
    Posts
    2
    i am having similar problem,... did u fix the issue or any updates ?

    i have seen few posts on internet saying update /etc/security/limits.conf

    but thats not working for me as well...

  3. #3
    Administrator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,864
    Please start a new thread with all your details. This thread is over one year old. *locking*
    I do not respond to private messages asking for Linux help, Please keep it on the forums only.
    All new users please read this.** Forum FAQS. ** Adopt an unanswered post.

    I'd rather be lost at the lake than found at home.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •