POSIX POSIX Portable OS Interface IEEE standard Mandatory
POSIX • POSIX: Portable OS Interface – IEEE standard – Mandatory + Optional parts • Mostly based on and adopted by Unix community • POSIX. 4 = POSIX 1003. 1 b – Added Realtime functionality • POSIX. 4 a = POSIX 1003. 1 c – Threads Extensions Dr. Hugh Melvin, Dept. of IT, NUI, G 1
POSIX. 4 = POSIX 1003. 1 b – Range of RT Features • Shared Memory / Memory Locking / Priority Scheduling / Signals / Semaphores / Clocks & Timers • Will examine many of these issues • Adopted by many RTOS : QNX, Lynx. OS, Vx. Works – POSIX. 4 b = POSIX 1003. 1 d – More realtime extensions Dr. Hugh Melvin, Dept. of IT, NUI, G 2
POSIX. 4 RT Scheduling • Defines two main scheduling policies – SCHED_FIFO and SCHED_RR • Each have attributes – Also have SCHED_OTHER – Currently a single attribute = priority struct sched_param{ int sched_priority; } – Eg. Could implement EDF by extending structure to include • struct timespec sched_deadline; • struct timespec sched_timerequired; Dr. Hugh Melvin, Dept. of IT, NUI, G 3
POSIX. 4 RT Scheduling • SCHED_FIFO – Simple priority based preemptive scheduler – Most common in RTS – FIFO used to schedule processes within each priority level – If no other process exists at higher priority, process runs until complete • Next process at that priority (if present) then allocated CPU • Highest priority process guaranteed processor time Dr. Hugh Melvin, Dept. of IT, NUI, G 4
POSIX. 4 RT Scheduling • SCHED_RR – Round robin used to timeslice among processes at same priority level – System provided timeslice – Use for lower priority tasks Dr. Hugh Melvin, Dept. of IT, NUI, G 5
POSIX. 4 RT Scheduling • Setting scheduling policy and attribute #include <sched. h> struct sched_param scheduling_parameters; int scheduling_policy; int i; scheduling_parameters. sched_priority=17; i=sched_setscheduler(getpid( ), SCHED_FIFO, &scheduling_parameters); • getpid( ) used to determine process ID – Process set to FIFO, priority 17 Dr. Hugh Melvin, Dept. of IT, NUI, G 6
POSIX. 4 RT Scheduling • Process priority ranges differ among OS – Need this info before setting priority level int sched_rr_min, sched_rr_max; int sched_fifo_min, sched_fifo_max; sched_rr_min=sched_get_priority_min(SCHED_RR); sched_rr_max=sched_get_priority_max(SCHED_RR); sched_fifo_min=sched_get_priority_min(SCHED_FIFO); sched_fifo_max=sched_get_priority_max(SCHED_FIFO); – Eg. 256 priority levels • FIFO range 128 -255 • RR range 0 -127 Dr. Hugh Melvin, Dept. of IT, NUI, G 7
POSIX. 4 RT Scheduling Eg. #include<sched. h> int i; struct sched_param my_sched_params; // determine max FIFO priority level my_sched_params. sched_priority= sched_get_priority_max(SCHED_FIFO); // Set priority i=sched_setparam(getpid (), &my_sched_params); Dr. Hugh Melvin, Dept. of IT, NUI, G 8
POSIX. 4 Clocks & Timers • Must be at least one clock – CLOCK_REALTIME • clock_gettime( ), clock_settime( ) – Replaces gettimeofday( ), settimeofday() – timespec structure (sec + nsec) • struct timespec{ time_t tv_sec; time_t tv_nsec; } • clock_getres(CLOCK_REALTIME, &realtime_res) – Returns clock resolution : must be at least 50 Hz – realtime_res is timespec structure • Realtime libraries reqd Dr. Hugh Melvin, Dept. of IT, NUI, G 9
POSIX #include<unistd. h> #include<time. h> int main(){ struct timespec clock_res; int stat; stat=clock_getres(CLOCK_REALTIME, &clock_res); printf("Clock resol is %d sec, %ld nsecondsn", clock_res. tv_sec, clock_res. tv_nsec); return 0; } Dr. Hugh Melvin, Dept. of IT, NUI, G 10
POSIX. 4 Clocks & Timers • nanosleep(&nap, &time_left) – Can delay process (both nap & time_left are timespec) • Interval Timers – Useful to specify precise intervals • struct itimerspec{ struct timespec it_value; struct timespec it_interval; } it_value = 1 st occasion of timer event it_interval = interval between subsequent events • System calls – timer_create( ) and timer_delete( ) – Can have multiple timers within any process Dr. Hugh Melvin, Dept. of IT, NUI, G 11
POSIX. 4 Clocks & Timers • Interval Timer example timer_t created_timer; i = timer_create( _ , &created_timer); struct itimerspec new, old; new. it_value. tv_sec=1; new. it_value. tv_nsec=0; new. it_interval. tv_nsec=100000; i=timer_settime(created_timer, 0, &new, &old). . i=timer_delete(created_timer) Dr. Hugh Melvin, Dept. of IT, NUI, G 12
POSIX. 4 Clocks & Timers • Absolute Timer Events – Eg. Timer event reqd at time tabs • Determine interval and use interval timer • clock_gettime(CLOCK_REALTIME, &now); • Calculate interval – Interval = tabs - now • Create and set Interval timer as above • But – Process may be preempted between step 1 and 2 Use absolute times timer_settime(created_timer, TIMER_ABSTIME, &tabs , NULL) Dr. Hugh Melvin, Dept. of IT, NUI, G 13
This function reads the hardware clock using the "intel" RDTSC assembly instruction. void get_timestamp(unsigned long* upper 32 bits, unsigned long* lower 32 bits) { unsigned long lower 32, upper 32, first_upper 32; do { __asm RDTSC __asm mov upper 32, EDX __asm mov lower 32, EAX first_upper 32 = upper 32; __asm RDTSC __asm mov upper 32, EDX } while (first_upper 32 != upper 32); // protect against wraparound *lower 32 bits = lower 32; *upper 32 bits = upper 32; } Dr. Hugh Melvin, Dept. of IT, NUI, G 14
- Slides: 14