Home | History | Annotate | Download | only in sched_setscheduler
      1 /*
      2  *  This program is free software; you can redistribute it and/or modify
      3  *  it under the terms of the GNU General Public License version 2.
      4  *
      5  *  This program is distributed in the hope that it will be useful,
      6  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      7  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      8  *  GNU General Public License for more details.
      9  *
     10  * Test that the underlying kernel-scheduled entities for the process
     11  * contention scope threads have their scheduling policy changed to the
     12  * value specified in param.
     13  */
     14 
     15 #include <sched.h>
     16 #include <stdio.h>
     17 #include <pthread.h>
     18 #include <unistd.h>
     19 #include <errno.h>
     20 #include "posixtest.h"
     21 
     22 void *runner(void *arg)
     23 {
     24 	(void) arg;
     25 
     26 	while (1)
     27 		sleep(1);
     28 	return NULL;
     29 }
     30 
     31 int main(void)
     32 {
     33 	int new_policy, policy, result;
     34 	struct sched_param param;
     35 	pthread_t tid;
     36 	pthread_attr_t attr;
     37 
     38 	/* Make sure new policy != old policy */
     39 	new_policy = (sched_getscheduler(getpid()) == SCHED_FIFO) ?
     40 	    SCHED_RR : SCHED_FIFO;
     41 
     42 	if (pthread_attr_init(&attr) != 0) {
     43 		printf("An error occurs when calling pthread_attr_init()");
     44 		return PTS_UNRESOLVED;
     45 	}
     46 	result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
     47 	if (result == ENOTSUP) {
     48 		printf("Process contention scope threads are not supported.\n");
     49 		return PTS_UNSUPPORTED;
     50 	} else if (result != 0) {
     51 		printf("An error occurs when calling pthread_attr_setscope()");
     52 		return PTS_UNRESOLVED;
     53 	}
     54 	if (pthread_create(&tid, &attr, runner, NULL) != 0) {
     55 		printf("An error occurs when calling pthread_create()");
     56 		return PTS_UNRESOLVED;
     57 	}
     58 
     59 	param.sched_priority = sched_get_priority_min(new_policy);
     60 	if (sched_setscheduler(getpid(), new_policy, &param) == -1) {
     61 		if (errno == EPERM) {
     62 			printf
     63 			    ("This process does not have the permission to set its own scheduling policy.\nTry to launch this test as root.\n");
     64 			return PTS_UNRESOLVED;
     65 		}
     66 		perror("An error occurs when calling sched_setscheduler()");
     67 		return PTS_UNRESOLVED;
     68 	}
     69 
     70 	if (pthread_getschedparam(tid, &policy, &param) != 0) {
     71 		printf("An error occurs when calling pthread_getschedparam()");
     72 		return PTS_UNRESOLVED;
     73 	}
     74 
     75 	pthread_cancel(tid);
     76 
     77 	if (policy == new_policy) {
     78 		printf("Test PASSED\n");
     79 		return PTS_PASS;
     80 	}
     81 	printf("sched_setscheduler() does not set the right policy.\n");
     82 	return PTS_FAIL;
     83 }
     84