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 policy and scheduling parameters remain unchanged when no
     12  * process can be found corresponding to that specified by pid.
     13  *
     14  * The test create a child process which exit immediately and call
     15  * sched_setscheduler with the pid of defunct child.
     16  * Steps:
     17  *   1. Get the old policy and priority.
     18  *   2. Create a child process which exit immediately.
     19  *   3. Wait for child to exit.
     20  *   4. Call sched_setscheduler with the pid of defunct child.
     21  *   5. Check that the policy and priority have not changed.
     22  */
     23 #include <sched.h>
     24 #include <stdio.h>
     25 #include <errno.h>
     26 #include <unistd.h>
     27 #include <stdlib.h>
     28 #include <sys/wait.h>
     29 #include "posixtest.h"
     30 
     31 int main(void)
     32 {
     33 	int max_priority, old_priority, old_policy, new_policy, policy;
     34 	int child_pid, stat_loc;
     35 	struct sched_param param;
     36 
     37 	if (sched_getparam(getpid(), &param) == -1) {
     38 		perror("An error occurs when calling sched_getparam()");
     39 		return PTS_UNRESOLVED;
     40 	}
     41 	old_priority = param.sched_priority;
     42 
     43 	old_policy = sched_getscheduler(getpid());
     44 	if (old_policy == -1) {
     45 		perror("An error occurs when calling sched_getscheduler()");
     46 		return PTS_UNRESOLVED;
     47 	}
     48 
     49 	/* Make sure that policy != old_policy */
     50 	policy = old_policy == SCHED_FIFO ? SCHED_RR : SCHED_FIFO;
     51 
     52 	/* Make sure that param.sched_priority != old_priority */
     53 	max_priority = sched_get_priority_max(policy);
     54 	param.sched_priority = (old_priority == max_priority) ?
     55 	    sched_get_priority_min(policy) : max_priority;
     56 
     57 	/* Create a child process which exit immediately */
     58 	child_pid = fork();
     59 	if (child_pid == -1) {
     60 		perror("An error occurs when calling fork()");
     61 		return PTS_UNRESOLVED;
     62 	} else if (child_pid == 0) {
     63 		exit(0);
     64 	}
     65 
     66 	/* Wait for the child process to exit */
     67 	if (wait(&stat_loc) == -1) {
     68 		perror("An error occurs when calling wait()");
     69 		return PTS_UNRESOLVED;
     70 	}
     71 
     72 	/* Assume the pid is not yet reatributed to an other process */
     73 	sched_setscheduler(child_pid, policy, &param);
     74 
     75 	if (sched_getparam(getpid(), &param) != 0) {
     76 		perror("An error occurs when calling sched_getparam()");
     77 		return PTS_UNRESOLVED;
     78 	}
     79 
     80 	new_policy = sched_getscheduler(getpid());
     81 	if (new_policy == -1) {
     82 		perror("An error occurs when calling sched_getscheduler()");
     83 		return PTS_UNRESOLVED;
     84 	}
     85 
     86 	if (old_policy == new_policy && old_priority == param.sched_priority) {
     87 		printf("Test PASSED\n");
     88 		return PTS_PASS;
     89 	}
     90 
     91 	if (param.sched_priority != old_priority) {
     92 		printf("The param has changed\n");
     93 	}
     94 	if (new_policy != old_policy) {
     95 		printf("The policy has changed\n");
     96 	}
     97 	return PTS_FAIL;
     98 }
     99