/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * Test that the higher numerical values for the priority represent the higher * priorities for the SCHED_FIFO policy. * * NO OTHER REALTIME PROCESS SHOULD RUN WHEN RUNNING THIS TEST. * * There is no portable way to get the number of CPUs but the test should work * for most of UNIX system (including but not limited to: Linux, Solaris, AIX, * HPUX, *BSD). * Steps: * 1. Get the number of CPUs. * 2. Set the scheduling policy to SCHED_FIFO with a mean priority. * 3. Lauch as many processes than CPU. The last process set its own priority * to the min value. * 4. Father process set its own priority to the max value. * 5. Both children and father increment a counter in a basic loop. * 6. The father send SIGTERM to the last child and get its counter. If child * counter is reasonably lower than the fathers one, the test is * succesfull. * 7. The father kill all other children. * */ #include #include #include #include #include #include #include "posixtest.h" #ifdef BSD # include # include # include #endif #ifdef HPUX # include # include #endif #define NB_LOOP 20000000 #define NB_LOOP_CHILD 200000000 /* shall be much greater than NB_LOOP */ #define ACCEPTABLE_RATIO 2.0 #define STDIN 0 #define STDOUT 1 #define STDERR 2 int nb_child; /* Number of child processes == number of CPUs */ int count = 0; int the_pipe[2]; /* Get the number of CPUs */ int get_ncpu() { int ncpu = -1; /* This syscall is not POSIX but it should work on many system */ #ifdef _SC_NPROCESSORS_ONLN ncpu = sysconf(_SC_NPROCESSORS_ONLN); #else # ifdef BSD int mib[2]; size_t len = sizeof(ncpu); mib[0] = CTL_HW; mib[1] = HW_NCPU; sysctl(mib, 2, &ncpu, &len, NULL, 0); # else # ifdef HPUX struct pst_dynamic psd; pstat_getdynamic(&psd, sizeof(psd), 1, 0); ncpu = (int)psd.psd_proc_cnt; # endif /* HPUX */ # endif /* BSD */ #endif /* _SC_NPROCESSORS_ONLN */ return ncpu; } void child_process(int id){ int i; struct sched_param param; if(id == nb_child-1){ param.sched_priority = sched_get_priority_min(SCHED_FIFO); sched_setparam(getpid(), ¶m); } for(i=0; i= ACCEPTABLE_RATIO) { printf("Test PASSED\n"); return PTS_PASS; } else if(ratio <= (1/ACCEPTABLE_RATIO)) { printf("Higher numerical values for the priority represent the lower priorities.\n"); return PTS_FAIL; } else { printf("The difference between the processes is not representative.\n"); return PTS_UNRESOLVED; } }