LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2006, 12:31 AM   #1
katlina
LQ Newbie
 
Registered: May 2006
Posts: 4

Rep: Reputation: 0
POSIX thread scheduling problem on Fedora Core 5


Hi all,

I tried to use pthread_setschedparam to set the thread schedule policy and priority at run time. The program worked fine if I set the policy to SCHED_OTHER, but if I changed the policy to SCHED_FIFO or SCHED_RR, the function pthread_setschedparam returned error. My system is a Fedora core 5. I wonder if some system setting that not allows me to use other policies. Is there anyway to change the setting or resolve this problem? Please help me.

Here is the program I tried on my system, which I downloaded from IBM's website:

The main program:

#define _MULTI_THREADED
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include "check.h"

#define BUMP_PRIO 1
int thePriority = 0;

int showSchedParam(pthread_t thread)
{
struct sched_param param;
int policy;
int rc;

printf("Get scheduling parameters\n");
rc = pthread_getschedparam(thread, &policy, &param);
checkResults("pthread_getschedparam()\n", rc);

printf("The thread scheduling parameters indicate:\n"
"priority = %d\n", param.sched_priority);
return param.sched_priority;
}

void *threadfunc(void *parm)
{
int rc;

printf("Inside secondary thread\n");
thePriority = showSchedParam(pthread_self());
sleep(5); /* Sleep is not a very robust way to serialize threads */
return NULL;
}

int main(int argc, char **argv)
{
pthread_t thread;
int rc=0;
struct sched_param param;
int policy = SCHED_FIFO; //SCHED_OTHER
int theChangedPriority=0;

printf("Enter Testcase - %s\n", argv[0]);

printf("Create thread using default attributes\n");
rc = pthread_create(&thread, NULL, threadfunc, NULL);
checkResults("pthread_create()\n", rc);

sleep(2); /* Sleep is not a very robust way to serialize threads */

memset(&param, 0, sizeof(param));
/* Bump the priority of the thread a small amount */
if (thePriority - BUMP_PRIO >= PRIORITY_MIN_NP) {
param.sched_priority = thePriority - BUMP_PRIO;
}

printf("Set scheduling parameters, prio=%d\n",
param.sched_priority);
rc = pthread_setschedparam(thread, policy, &param);
checkResults("pthread_setschedparam()\n", rc);

/* Let the thread fill in its own last priority */
theChangedPriority = showSchedParam(thread);

if (thePriority == theChangedPriority ||
param.sched_priority != theChangedPriority) {
printf("The thread did not get priority set correctly, "
"first=%d last=%d expected=%d\n",
thePriority, theChangedPriority, param.sched_priority);
exit(1);
}

sleep(5); /* Sleep is not a very robust way to serialize threads */
printf("Main completed\n");
return 0;
}

The "check.h" file:

#ifndef _CHECK_H
#define _CHECK_H
/* headers used by a majority of the example program */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

/* Simple function to check the return code and exit the program
if the function call failed
*/
static void checkResults(char *string, int rc) {
if (rc) {
printf("Error on : %s, rc=%d",
string, rc);
exit(EXIT_FAILURE);
}
return;
}

#endif
 
Old 11-16-2006, 08:51 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 109Reputation: 109
My understanding is that with FIFO and RR scheduling parameters, you can only use priorities 1-99. It looks like here, your priority could explode (read: go negative). Change your thePriority setting function to the following:

Code:
if (thePriority + BUMP_PRIO <= PRIORITY_MAX_NP) {
  param.sched_priority = thePriority + BUMP_PRIO;
}
 
Old 11-16-2006, 10:07 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,346
Blog Entries: 4

Rep: Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333Reputation: 3333
You may also be restricted from using these policies at all.

Realistically speaking, unless you are doing the magical kernel-style things for which these features were intended, you probably just want to request "ordinary" scheduling and let the scheduler do what the scheduler is well-designed to do.

The scheduler will always provide the best possible response, all things considered, to any thread that needs to use the CPU(s) at any moment. Most of the time, "simply be generic, and you'll be fine." Don't make life difficult for the scheduler, because your process doesn't know, can't know, "[what are the] all things [that currently need to be] considered."
 
Old 11-16-2006, 12:38 PM   #4
katlina
LQ Newbie
 
Registered: May 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you all,

After I changed my code as orgcandman suggested and ran as root, the program worked. The reason I want to do thread scheduling that in my program, I had a thread (not the main program) that has to create other threads and do some important tasks. It worked most of the time but there are times it failed to create new threads, the pthread_create function still returned 0 for success, but there is no new thread created in the system. I don't know what has happened in those moments, so I try to change the scheduling policy and priority of that thread in hope that it will have enough time to complete its tasks. If you have any idea about this issue, please let me know.

Thanks a lot again.
katlina
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Thread Scheduling using POSIX THREAD rameshtekumudi Programming 3 09-01-2006 10:47 AM
thread scheduling vkmgeek Programming 2 08-21-2006 10:29 AM
POSIX Thread programming problem... rajsun Programming 0 10-07-2005 11:18 PM
Fedora Core 3, Windows ACLs, POSIX ?? jabran Fedora 6 06-24-2005 10:01 PM
scheduling thread gives error bndpatel Programming 1 06-24-2005 01:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 AM.

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