프로그래밍

[Thread] pthread_setschedparam() function

지니아부지 2011. 10. 10. 21:27

http://www.howto.pe.kr/zboard/zboard.php?id=lecture&page=2&sn1=&divpage=1&category=6&sn=off&ss=on&sc=on&select_arrange=headnum&desc=asc&no=80

/***************************************************
*--------------------------------------------------
* Creat : 2001. 04. 10 (programed by Cori-Young )
* Site: http://www.byoneself.co.kr
* Update :
*--------------------------------------------------
* Compile : cc -o xxx xxx.c -lpthread -lrt
*--------------------------------------------------
* Machine hardware: sun4u
* OS version: 5.7
* Processor type: sparc
* Hardware: SUNW,Ultra-60
***************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <sched.h>

#define _MULTI_THREADED
#define BUMP_PRIO 1

static void checkResults(char *string, int rc) {

if (rc) {
printf("Error on : %s, rc=%d", string, rc);
exit(EXIT_FAILURE);
}
return;
}

int thePriority = 0;

int showSchedParam(pthread_t thread)
{

struct sched_param param;
int policy;
int rc;

printf("Get scheduling parametersn");

rc = pthread_getschedparam(thread, &policy, ¶m);
checkResults("pthread_getschedparam()n", rc);

printf("The thread scheduling parameters indicate:npriority = %dn", param.sched_priority);

return param.sched_priority;
}


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

printf("Inside secondary threadn");

thePriority = showSchedParam(pthread_self());
sleep(5); /* Sleep isn't 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_OTHER;
int theChangedPriority=0;

printf("Enter Testcase - %sn", argv[0]);
printf("Create thread using default attributesn");

rc = pthread_create(&thread, NULL, threadfunc, NULL);
checkResults("pthread_create()n", rc);

sleep(2); /* Sleep isn't a very robust way to serialize threads */

memset(¶m, 0, sizeof(param));

printf("Set scheduling parameters, prio=%dn", param.sched_priority);

rc = pthread_setschedparam(thread, policy, ¶m);
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 didn't get priority set correctly, "
"first=%d last=%d expected=%dn",
thePriority, theChangedPriority, param.sched_priority);
exit(1);
}

sleep(5); /* Sleep isn't a very robust way to serialize threads */
printf("Main completedn");
return 0;
}