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 function return the former scheduling policy of the specified
     12  * process upon successful completion.
     13  *
     14  * Steps:
     15  *   1. Get the old policy.
     16  *   2. Set a new policy.
     17  *   3. Check that sched_setscheduler return the old policy.
     18  */
     19 #include <sched.h>
     20 #include <stdio.h>
     21 #include <errno.h>
     22 #include <unistd.h>
     23 #include "posixtest.h"
     24 
     25 int main(void)
     26 {
     27 	int result, old_policy, new_policy;
     28 	struct sched_param param;
     29 
     30 	old_policy = sched_getscheduler(getpid());
     31 	if (old_policy == -1) {
     32 		perror("An error occurs when calling sched_getscheduler()");
     33 		return PTS_UNRESOLVED;
     34 	}
     35 
     36 	/* Make sure new_policy != old_policy */
     37 	new_policy = (old_policy == SCHED_FIFO) ? SCHED_RR : SCHED_FIFO;
     38 
     39 	param.sched_priority = sched_get_priority_max(new_policy);
     40 	result = sched_setscheduler(0, new_policy, &param);
     41 
     42 	if (result == old_policy) {
     43 		printf("Test PASSED\n");
     44 		return PTS_PASS;
     45 	} else if (result == -1 && errno == EPERM) {
     46 		printf
     47 		    ("The process have not permission to change its own policy.\nTry to launch this test as root.\n");
     48 		return PTS_UNRESOLVED;
     49 	}
     50 
     51 	printf("Returned code == %i.\n", result);
     52 	perror("Unknow error");
     53 	return PTS_FAIL;
     54 
     55 }
     56