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 that are created after this call completes inherit
     12  * their scheduling policy from the process.
     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 	if (sched_getparam(getpid(), &param) != 0) {
     39 		perror("An error occurs when calling sched_getparam()");
     40 		pthread_exit((void *)-1);
     41 	}
     42 
     43 	/* Make sure new policy != old policy */
     44 	new_policy = (sched_getscheduler(getpid()) == SCHED_FIFO) ?
     45 	    SCHED_RR : SCHED_FIFO;
     46 
     47 	param.sched_priority = sched_get_priority_min(new_policy);
     48 	if (sched_setscheduler(getpid(), new_policy, &param) == -1) {
     49 		if (errno == EPERM) {
     50 			printf
     51 			    ("This process does not have the permission to set its own scheduling policy.\nTry to launch this test as root.\n");
     52 			return PTS_UNRESOLVED;
     53 		}
     54 		perror("An error occurs when calling sched_setscheduler()");
     55 		return PTS_UNRESOLVED;
     56 	}
     57 
     58 	if (pthread_attr_init(&attr) != 0) {
     59 		printf("An error occurs when calling pthread_attr_init()");
     60 		return PTS_UNRESOLVED;
     61 	}
     62 	result = pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
     63 	if (result == ENOTSUP) {
     64 		printf("Process contention scope threads are not supported.\n");
     65 		return PTS_UNSUPPORTED;
     66 	} else if (result != 0) {
     67 		printf("An error occurs when calling pthread_attr_setscope()");
     68 		return PTS_UNRESOLVED;
     69 	}
     70 	if (pthread_create(&tid, &attr, runner, NULL) != 0) {
     71 		printf("An error occurs when calling pthread_create()");
     72 		return PTS_UNRESOLVED;
     73 	}
     74 
     75 	if (pthread_getschedparam(tid, &policy, &param) != 0) {
     76 		printf("An error occurs when calling pthread_getschedparam()");
     77 		return PTS_UNRESOLVED;
     78 	}
     79 
     80 	pthread_cancel(tid);
     81 
     82 	if (policy == new_policy) {
     83 		printf("Test PASSED\n");
     84 		return PTS_PASS;
     85 	}
     86 	printf("The thread does not inherit the right policy.\n");
     87 	return PTS_FAIL;
     88 }
     89