Title: POSIX
1POSIX
- 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
2POSIX.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
3POSIX.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
4POSIX.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
5POSIX.4 RT Scheduling
- SCHED_RR
- Round robin used to timeslice among processes at
same priority level - System provided timeslice
- Use for lower priority tasks
6POSIX.4 RT Scheduling
- Setting scheduling policy and attribute
- include ltsched.hgt
- struct sched_param scheduling_parameters
- int scheduling_policy
- int i
- scheduling_parameters.sched_priority17
- isched_setscheduler(getpid( ),SCHED_FIFO,
scheduling_parameters) - getpid( ) used to determine process ID
- Process set to FIFO, priority 17
7POSIX.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_minsched_get_priority_min(SCHED_RR)
- sched_rr_maxsched_get_priority_max(SCHED_RR)
- sched_fifo_minsched_get_priority_min(SCHED_FIFO)
- sched_fifo_maxsched_get_priority_max(SCHED_FIFO)
- Eg. 256 priority levels
- FIFO range 128-255
- RR range 0-127
8POSIX.4 RT Scheduling
- Eg.
- includeltsched.hgt
- 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
- isched_setparam(getpid (),my_sched_params)
9POSIX.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
10POSIX
- includeltunistd.hgt
- includelttime.hgt
- int main()
- struct timespec clock_res
- int stat
- statclock_getres(CLOCK_REALTIME, clock_res)
- printf("Clock resol is d sec, ld
nseconds\n",clock_res.tv_sec,clock_res.tv_nsec)
- return 0
11POSIX.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 1st occasion of timer event
- it_interval interval between subsequent events
- System calls
- timer_create( ) and timer_delete( )
- Can have multiple timers within any process
12POSIX.4 Clocks Timers
- Interval Timer example
- timer_t created_timer
- i timer_create( _ , _ , created_timer)
- struct itimerspec new,old
- new.it_value.tv_sec1
- new.it_value.tv_nsec0
- new.it_interval.tv_sec0
- new.it_interval.tv_nsec100000
- itimer_settime(created_timer, 0,new, old)
- ..
- itimer_delete(created_timer)
13POSIX.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)
14This 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