POSIX POSIX: Portable OS Interface Set of IEEE standards Mandatory + Optional parts Objective: Source code portability of applications across multiple OS Standard way for applications to interface to OS Mostly but not exclusively Unix type OS Total portability is not achievable POSIX.1 Set of standard OS system calls File operations, process management, signals, and devices Mostly mandatory Widely supported Unix, Linux, VxWorks, QNX, Solaris, OSX, LynxOS Dr. Hugh Melvin, Dept. of IT, NUI,Galway 1 POSIX Support Compiler Support Options to include/invoke POSIX support Eg. GNU C Compiler gcc lrt o name name.c Headers Set of header files that define POSIX interface supported on particular system #include<unistd.h> Libraries Implement POSIX functionality Dr. Hugh Melvin, Dept. of IT, NUI,Galway 2 POSIX.4 POSIX.4 = POSIX 1003.1b Added Realtime functionality Built on top of POSIX.1 POSIX.4a = POSIX 1003.1c Threads extensions POSIX.4b = POSIX 1003.1d More realtime extensions Dr. Hugh Melvin, Dept. of IT, NUI,Galway 3 1
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, RT Linux, Integrity POSIX Compliant OS Supports mandatory features Specifies which optional features it supports POSIX.4 features are mostly optional so beware Dr. Hugh Melvin, Dept. of IT, NUI,Galway 4 POSIX.4 Found on many Soft RTS Network Switches Multimedia applications DBMS Navigation Systems Increasingly found on Hard RTS See Integrity RTOS Aviation Dr. Hugh Melvin, Dept. of IT, NUI,Galway 5 POSIX.4 Linux & POSIX.4 Many POSIX.4 features supported Convert a conventional time sharing OS to a pseudo RT OS Assignment 3 Implement and test range of POSIX.4 features on Linux OS POSIX References http://www.xtrj.org/collection/posix_rtos.htm POSIX.4 Prog. For the Real World, Bill Gallmeister, O Reilly Advanced UNIX Programming, Rochkind, Add Wesley Dr. Hugh Melvin, Dept. of IT, NUI,Galway 6 2
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,Galway 7 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,Galway 8 SCHED_RR Round robin used to timeslice among processes at same priority level System provided timeslice Use for lower priority tasks SCHED_OTHER Implementation dependent Dr. Hugh Melvin, Dept. of IT, NUI,Galway 9 3
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,Galway 10 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,Galway 11 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,Galway 12 4
Range of features timespec, itimerspec Time structures clock_getres() Determine Clock Resolution clock_gettime(),clock_settime() Get and Set System Clock Time nanosleep( ) timer_create( ),timer_delete( ) Interval timers Dr. Hugh Melvin, Dept. of IT, NUI,Galway 13 Must be at least one clock CLOCK_REALTIME timespec structure (sec + nsec) struct timespec{ time_t tv_sec; time_t tv_nsec; Replaces timeval clock_getres(clock_realtime,&realtime_res) Returns clock resolution : must be at least 50 Hz realtime_res is timespec structure clock_gettime(clock_realtime, &time), clock_settime( ) Replace gettimeofday( ),settimeofday() Realtime libraries reqd Dr. Hugh Melvin, Dept. of IT, NUI,Galway 14 #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 nseconds\n",clock_res.tv_sec,clock_res.tv_nsec); return 0; Dr. Hugh Melvin, Dept. of IT, NUI,Galway 15 5
nanosleep(&nap,&time_left) Can delay process (both nap & time_left are timespec structs) for(i=0;i<num_iter;++i) { clock_gettime(clock_realtime, &c_time1); nanosleep(&sleeptime,null); clock_gettime(clock_realtime, &c_time2); now1=c_time1.tv_sec + c_time1.tv_nsec*0.000000001; now2=c_time2.tv_sec + c_time2.tv_nsec*0.000000001; printf("time is %ld : %ld..slept for %lf nsec\n,c_time2.tv_sec, c_time2.tv_nsec,(now2-now1)*1000000000); Dr. Hugh Melvin, Dept. of IT, NUI,Galway 16 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,Galway 17 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) Dr. Hugh Melvin, Dept. of IT, NUI,Galway 18 6
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) Dr. Hugh Melvin, Dept. of IT, NUI,Galway 19 POSIX.4 Memory Locking Lock all or part of a process in MM Facilitated via memory locking POSIX.4 #include <unistd.h> #ifdef _POSIX_MEMLOCK #include <sys/mman.h> int mlockall(int flags); int munlockall(void); #endif /*_POSIX_MEMLOCK */ Locks complete process in memory Need to ensure that you have enough memory Dr. Hugh Melvin, Dept. of IT, NUI,Galway 20 POSIX.4 Memory Locking /*Main routine */ int main(void ){ /* Lock all process down */ mlockall(mcl_current MCL_FUTURE); process code munlockall(); return 0; Locks currently and future mapped pages belonging to process in memory Locked Memory will vary as process runs Can also lock critical sections of memory or functions within a process More complex Dr. Hugh Melvin, Dept. of IT, NUI,Galway 21 7