Bug 145 - scheduling parameters not set during call to pthread_create
Summary: scheduling parameters not set during call to pthread_create
Status: RESOLVED INVALID
Alias: None
Product: glibc
Classification: Unclassified
Component: nptl (show other bugs)
Version: 2.3.3
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-05-04 21:23 UTC by david ahern
Modified: 2019-04-10 11:58 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description david ahern 2004-05-04 21:23:55 UTC
Scheduling parameters -- policy and priority -- are not set when thread is
created even though the parameters are set in the thread attributes.

Example code below. Output (as root) is:
thread id = 0xf701fbb0, policy = SCHED_OTHER, priority = 0

re-setting priority and policy

now schedule data for thread is:
thread id = 0xf701fbb0, policy = SCHED_RR, priority = 25



#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sched.h>


#define HPRIO  25

void show_sched(void)
{
	struct sched_param sp;
	int policy;
	const char *pstr;
	
	if (pthread_getschedparam(pthread_self(), &policy, &sp) != 0) {
		perror("pthread_getschedparam failed");
		return;
	}

	switch(policy) {
		case SCHED_OTHER:
			pstr = "SCHED_OTHER";
			break;
		case SCHED_FIFO:
			pstr = "SCHED_FIFO";
			break;
		case SCHED_RR:
			pstr = "SCHED_RR";
			break;
		default:
			pstr = "unknown";
	}

	printf("thread id = 0x%lx, policy = %s, priority = %d\n",
			pthread_self(), pstr, sp.sched_priority);

	return;
}

void set_sched(int policy, int priority)
{
	struct sched_param schedp;

	memset(&schedp, 0, sizeof(schedp));
	schedp.sched_priority = priority;
	if (pthread_setschedparam(pthread_self(), policy, &schedp) != 0)
		perror("pthread_setschedparam failed");

}


void thread1(void)
{
	show_sched();

	printf("\nre-setting priority and policy\n");
	set_sched(SCHED_RR, HPRIO);

	printf("\nnow schedule data for thread is:\n");
	show_sched();

	return;
}


int main(void)
{
	pthread_attr_t attr;
	struct sched_param schedp;
	pthread_t id1;
	int rc;



	/* 
	 *   Create thread at high priority 
	 */
	if (pthread_attr_init(&attr) != 0) {
		perror("pthread_attr_init failed");
		return 1;
	}

	/* set schedule policy */
	if (pthread_attr_setschedpolicy(&attr, SCHED_RR) != 0) {
		perror("pthread_attr_setschedpolicy failed");
		return 1;
	}

	/* set schedule priority */
	schedp.sched_priority = HPRIO;
	if (pthread_attr_setschedparam(&attr, &schedp) != 0) {
		perror("pthread_attr_setschedparam failed");
		return 1;
	}

	/* create thread */
	rc = pthread_create(&id1, &attr, (void *(*)(void *))thread1, 0);
	if (rc) {
		perror("pthread_create failed");
		return(1);
	}

	pthread_attr_destroy(&attr);


	pthread_join(id1, NULL);

	return(0);
}
Comment 1 Jakub Jelinek 2004-06-15 08:23:42 UTC
NPTL defaults to PTHREAD_INHERIT_SCHED, so if you want to use the explicit
scheduling parameters, you should also pthread_attr_setinheritsched (&attr,
PTHREAD_EXPLICIT_SCHED).
Comment 2 david ahern 2004-06-18 17:06:10 UTC
------- Additional Comments From jakub at redhat dot com  2004-06-15 
08:23 -------
NPTL defaults to PTHREAD_INHERIT_SCHED, so if you want to use the explicit
scheduling parameters, you should also pthread_attr_setinheritsched (&attr,
PTHREAD_EXPLICIT_SCHED).