Download presentation
Presentation is loading. Please wait.
Published byClaud Thompson Modified over 9 years ago
1
Dr. Hugh Melvin, Dept. of IT, NUI,G1 POSIX POSIX: Portable OS Interface –IEEE standard –Mandatory + Optional parts Mostly based on and adopted by Unix community POSIX.4 = POSIX 1003.1b –Added Realtime functionality POSIX.4a = POSIX 1003.1c –Threads Extensions
2
Dr. Hugh Melvin, Dept. of IT, NUI,G2 POSIX.4 = POSIX 1003.1b –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, LynxOS, VxWorks –POSIX.4b = POSIX 1003.1d –More realtime extensions
3
Dr. Hugh Melvin, Dept. of IT, NUI,G3 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;
4
Dr. Hugh Melvin, Dept. of IT, NUI,G4 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
5
Dr. Hugh Melvin, Dept. of IT, NUI,G5 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
6
Dr. Hugh Melvin, Dept. of IT, NUI,G6 POSIX.4 RT Scheduling Setting scheduling policy and attribute #include 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
7
Dr. Hugh Melvin, Dept. of IT, NUI,G7 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
8
Dr. Hugh Melvin, Dept. of IT, NUI,G8 POSIX.4 RT Scheduling Eg. #include 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);
9
Dr. Hugh Melvin, Dept. of IT, NUI,G9 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
10
Dr. Hugh Melvin, Dept. of IT, NUI,G10 POSIX #include int main(){ struct timespec clock_res; int stat; stat=clock_getres(CLOCK_REALTIME, &clock_res); printf("Clock resol is %d sec, %ld nseconds\n",clock_res.tv_sec,clock_res.tv_nsec); return 0; }
11
Dr. Hugh Melvin, Dept. of IT, NUI,G11 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
12
Dr. Hugh Melvin, Dept. of IT, NUI,G12 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_sec=0; new.it_interval.tv_nsec=100000; i=timer_settime(created_timer, 0,&new, &old).. i=timer_delete(created_timer)
13
Dr. Hugh Melvin, Dept. of IT, NUI,G13 POSIX.4 Clocks & Timers Absolute Timer Events –Eg. Timer event reqd at time t abs Determine interval and use interval timer clock_gettime(CLOCK_REALTIME, &now); Calculate interval –Interval = t abs - 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,&t abs,NULL)
14
Dr. Hugh Melvin, Dept. of IT, NUI,G14 This function reads the hardware clock using the "intel" RDTSC assembly instruction. void get_timestamp(unsigned long* upper32bits, unsigned long* lower32bits) { unsigned long lower32, upper32, first_upper32; do { __asm RDTSC __asm mov upper32, EDX __asm mov lower32, EAX first_upper32 = upper32; __asm RDTSC __asm mov upper32, EDX } while (first_upper32 != upper32); // protect against wraparound *lower32bits = lower32; *upper32bits = upper32; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.