LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-28-2010, 05:46 AM   #1
bhatta_sobs
LQ Newbie
 
Registered: May 2010
Posts: 1

Rep: Reputation: 0
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;
}
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which is best for implementing SMTP servers in unix.. fork or pthread ? vskgopu Linux - Newbie 2 05-07-2006 09:40 PM
Which is best for implementing SMTP relay servers in unix.. fork or pthread ? vskgopu Linux - Networking 1 05-07-2006 07:47 AM
Which is best for implementing SMTP server in unix.. fork or pthread ? vskgopu Programming 1 05-06-2006 07:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 02:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration