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  *
     11  * Test that the scheduling policy and scheduling parameters are set for
     12  * the calling process when pid == 0.
     13  */
     14 #include <sched.h>
     15 #include <stdio.h>
     16 #include <errno.h>
     17 #include <unistd.h>
     18 #include "posixtest.h"
     19 
     20 int main(void)
     21 {
     22 	int result, new_priority, old_priority, max_prio;
     23 	int old_policy, new_policy, test_policy;
     24 	struct sched_param param;
     25 
     26 	if (sched_getparam(getpid(), &param) != 0) {
     27 		perror("An error occurs when calling sched_getparam()");
     28 		return PTS_UNRESOLVED;
     29 	}
     30 
     31 	old_policy = sched_getscheduler(getpid());
     32 	if (old_policy == -1) {
     33 		perror("An error occurs when calling sched_getscheduler()");
     34 		return PTS_UNRESOLVED;
     35 	}
     36 
     37 	/* Make sure new_policy != old_policy */
     38 	new_policy = (old_policy == SCHED_FIFO) ? SCHED_RR : SCHED_FIFO;
     39 
     40 	/* Make sure new_priority != old_priority */
     41 	max_prio = sched_get_priority_max(new_policy);
     42 	old_priority = param.sched_priority;
     43 	new_priority = (old_priority == max_prio) ?
     44 	    (param.sched_priority = sched_get_priority_min(new_policy)) :
     45 	    (param.sched_priority = max_prio);
     46 
     47 	result = sched_setscheduler(0, new_policy, &param);
     48 
     49 	if (sched_getparam(getpid(), &param) != 0) {
     50 		perror("An error occurs when calling sched_getparam()");
     51 		return PTS_UNRESOLVED;
     52 	}
     53 
     54 	test_policy = sched_getscheduler(getpid());
     55 	if (test_policy == -1) {
     56 		perror("An error occurs when calling sched_getscheduler()");
     57 		return PTS_UNRESOLVED;
     58 	}
     59 
     60 	if (result != -1 && param.sched_priority == new_priority &&
     61 	    test_policy == new_policy) {
     62 		printf("Test PASSED\n");
     63 		return PTS_PASS;
     64 	} else if (result != -1 &&
     65 		   (param.sched_priority == old_priority ||
     66 		    test_policy == old_policy)) {
     67 		if (param.sched_priority == old_priority) {
     68 			printf("The param does not change\n");
     69 		}
     70 		if (test_policy == old_policy) {
     71 			printf("The policy does not change\n");
     72 		}
     73 		return PTS_FAIL;
     74 	} else if (result == -1 && errno == EPERM) {
     75 		printf
     76 		    ("The process have not permission to change its own policy.\nTry to launch this test as root.\n");
     77 		return PTS_UNRESOLVED;
     78 	}
     79 
     80 	perror("Unknow error");
     81 	return PTS_FAIL;
     82 }
     83