/* * 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 process specified by the pid argument preempt a lowest * priority running process, if the priority of the process specified by the * pid argument is set higher than that of the lowest priority running process * and if the specified process is ready to run. * * 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). * This test used shared memory. * Steps: * 1. Create a share memory segment. * 2. Change the policy to SCHED_FIFO and set minimum priority. * 3. Create NB_CPU-1 children processes which set their own priority to the * higher value and use all but one processor. * 4. Create a child with the same priority. * 4. Call sched_setparam with an mean priority and the pid value of the * last children. * 5. Check if the shared value has been changed by the child process. If * not, the test fail. */ #define _XOPEN_SOURCE 600 #include #include #include #include #include #include #include #include #include "posixtest.h" #ifdef BSD # include # include # include #endif #ifdef HPUX # include # include #endif /* Max number of loop for child_process */ #define NB_LOOP 100000 int nb_cpu; int *shmptr; /* shared memory */ /* 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(){ struct sched_param param; param.sched_priority = sched_get_priority_max(SCHED_FIFO); if(sched_setparam(getpid(), ¶m) != 0) { perror("An error occurs when calling sched_setparam()"); return; } /* to avoid blocking */ alarm(2); while(1); } void test_process(){ /* to avoid blocking */ alarm(2); while(1) { (*shmptr)++; sched_yield(); } } void kill_children(int *child_pid){ int i; for(i=0; i