Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Threads and Communication. 2 ● One standard rules them all: POSIX IEEE 1003.* ● Some links ●

Similar presentations


Presentation on theme: "1 Threads and Communication. 2 ● One standard rules them all: POSIX IEEE 1003.* ● Some links ●"— Presentation transcript:

1 1 Threads and Communication

2 2 ● One standard rules them all: POSIX IEEE 1003.* ● Some links ● http://en.wikipedia.org/wiki/POSIX http://en.wikipedia.org/wiki/POSIX ● Portable Operating System Interface [for Unix] ● en.wikipedia.org/wiki/Microsoft_POSIX_subsystem ● www.lynuxworks.com/products/posix/function- calls.php3

3 3 Why a standard ? ● Independent of different operating systems variants

4 4 Life is not simple IEEE 1003.... ● 1 standard OS ● 1b-1993 Realtime Extension ● 1c-1995 Threads ● 1d-1999 Additional Realtime Extensions ● 1j-2000 Advanced Realtime Extensions ● 1q-2000 Tracing ● www.opengroup.org ● 1, 1b and 1c a comprehensive list... http://www.lynuxworks.com/products/posix/function- calls.php3 http://www.lynuxworks.com/products/posix/function- calls.php3

5 5 POSIX... - simplified ● POSIX.1: POSIX core services ● (the feature set usually found in UNIX® operating systems) ● POSIX.1b: real-time extensions ● POSIX.1c: thread extensions ● 1b and 1c are considered importanted when dealing with real time systems ● Not considered to be wide supported...

6 6 Reminder ● POSIX is an interface description not an (buyable) reference implementation. ● POSIX support assures code portability between systems and is increasingly mandated for commercial applications and government contracts. ● For instance, the USA's Joint Technical Architecture— Army (JTA-A) standards set specifies that conformance to the POSIX specification is critical to support software interoperability.

7 7 And... DO 178 (b) ● Software Considerations in Airborne Systems and Equipment Certification ● Develop objectives for the life-cycle processes ● Provide a description of the activities and design considerations for achieving those objectives ● Provide a description of the evidence indicating the objectives have been satisfied ● All about quality ● You will meet it some day in your life when dealing with safety critical systems

8 8 DO 178 (b) and OS'es ● Lynx, QNX, vxWorks,montevista,enea,... ● My guess << 20, < 10 worldwide... ● And no open source ● OS'es like Lynx is DO178b AND POSIX certified :-) ●... and influences other domains including medical devices, transportation, and telecommunications.

9 9

10 10 Picking up an Operating System ● POSIX 1,1b,1c: +200 system calls... ● many many lines of code... ● Not really implementable in 8 bit embedded systems ● Look for conformance classes/profiles ● We do support xxx from 1003.1b etc etc ● Certification – costs (many) money ● Or embedded OS'es trying to mimic POSIX calls

11 11 Threads #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; printf("Hello World! It's me, thread # %ld ! \n", tid); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0; t<NUM_THREADS; t++){ printf("In main: creating thread %ld\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); }} pthread_exit(NULL);}

12 12 Thread ● Thread compared to ● Local application ● Operating system ● HW (read CPU's)

13 13 Scheduling basic – POSIX ! ● Bound versus unbound threads

14 14 Scheduling Valid settings for policy include: ● SCHED_FIFO Threads are scheduled in a first-in-first-out order within each priority. ● SCHED_OTHER Scheduling behavior is determined by the operating system. ● SCHED_RR Threads are scheduled in a round-robin fashion within each priority. ● NB NB NB NB: even equal interface you may not see 100% the same behaviour on different OS !!!!!!! !!!! #include int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

15 15 Bound thread pthread_attr_t attr; pthread_attr_init(&attr); /* initialize attr with default attributes */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&tid, &attr, start_func, arg); And here after (or before you may select fifo scheduling...)

16 16 Ubound thread pthread_attr_t attr; pthread_attr_init(&attr); /* initialize attr with default attributes */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&tid, &attr, start_func, arg);

17 17 struct sched_param sp; int rc, policy, cnt=0; rc=pthread_getschedparam(pthread_self(), &policy, &sp); printf ("Thrd: Running at %s/%d\n", (policy == SCHED_FIFO ? "FIFO" : (policy == SCHED_RR ? "RR" : (policy == SCHED_OTHER ? "OTHER" : "unknown"))), sp.sched_priority);... sp.sched_priority = 12; pthread_setschedparam(thread_self(), policy, &sp );

18 18 Resume ● Thread Sceduling classes. ● FIFO for strict real time ● RR (round robbin) ● Other – os dependent ● Thread Priority within classes ● Bound threads (on top of HW) ● Suited for real time ● Unbound threads ● normal threads ● And much much more not covered here – beware !!!

19 19 ! ● Supported by ● Mac ● Miscrosoft – NT – Vista & W7 (ultimate edition – not home ed...) ● Unix'es ● Linux'es ● Lynx,QNX,... (embedded “unix” variants)

20 20 Timing and timers ● Goal ● To have code running on – A one shot basis – Regular (e.q. For periodic sampling)

21 21 Std HW timers ● On “small systems” ● Std ISR + link to an embedded kernel void interrupt xx() { do_stuff(); sema_post();... } Interrupt may be disabled Inside ISR

22 22 Posix signal variant ● A longer way to you ● Your handler runs with interrupt enabled. ● Beware of bound/unbound problems ● Timing might be ok -> accurate ● Old variants may loose second “interrupt” !!! OS ISR codeOS appl director Your handler

23 23 /*from http://www.cis.temple.edu/~ingargio/cis307/software/posix4/periodic_timer.p4.c*/ void timer_intr(int sig, siginfo_t *extra, void *cruft) { printf or sema_post or whatever – interrupt is enable } main(int argc, char **argv) { int c; struct itimerspec i; struct sigaction sa; sigset_t allsigs; struct sigevent timer_event; timer_t mytimer; i.it_interval.tv_sec = 0; i.it_interval.tv_nsec = DEFAULT_NSECS; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_SIGINFO; /* Real-Time signal */ sa.sa_sigaction = timer_intr; /* Create a timer based upon the CLOCK_REALTIME clock */ timer_event.sigev_notify = SIGEV_SIGNAL; timer_event.sigev_signo = SIGRTMIN; timer_event.sigev_value.sival_ptr = (void *)&mytimer; timer_create(CLOCK_REALTIME, &timer_event, &mytimer); timer_settime(mytimer, 0, &i, NULL): }

24 24 Communication ● Triggering or the action itself ● A signal as seen on previous slides ● Semaphore between threads ● Information passing ● asynchronous ● partly asynchronous ● buffered or not ● with/with out OS

25 25 Memory models

26 26 Shared mem – the cheap way Int shared_var; sema_tp mutex_sem; sema_wait( &mutex_sem); shared_var = 444; sema_wait(&mutex_sem); sema_post(&mutex_sem);.... sema_post(&mutex_sem);

27 27 a POSIX mutex pthread_mutex_t mutex1; int counter=0; /* Function C */ mutex_init(&mutex1,NULL); void mutex_region() { pthread_mutex_lock( &mutex1 ); counter++ pthread_mutex_unlock( &mutex1 ); } / *http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html*/

28 28 Mutex problem - inheritance HIGH MED LOW BLUE: outside region RED: Inside region HIG/MED/LOW: thread priority blockin g

29 29 Priority Ceiling short pthread_mutexattr_setprioceiling (pthread_mutexattr_t *attr, int prioceiling); “Prevents/avoids” ceiling – much more later in course.

30 30 Shared mem – posix way int shmget(key_t key, size_t size, int shmflg);

31 31 #define SHMSZ 27 char c; int shmid; key_t key; char *shm, *s; key = 5678; shmid = shmget(key, 1024, IPC_CREAT | 0666); /* Now we attach the segment to our data space. */ shm = shmat(shmid, NULL, 0); s = shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; while (*shm != '*') /* wait until other guy change 1 byte */ sleep(1);

32 32 The other guy #define SHMSZ 27 char c; int shmid; key_t key; char *shm, *s; key = 5678; shmid = shmget(key, 1024, IPC_CREAT | 0666); /* Now we attach the segment to our data space. */ shm = shmat(shmid, NULL, 0); s = shm; *s = '*';

33 33 Communication – pre requiest's ● Real time request ● Driven and handled by operating system ● For testing and verification purpose POSIX message queues

34 34 MQ (Message Queues) thread

35 35 Mq calls /* http://mij.oltrelinux.com/devel/unixprg/ */http://mij.oltrelinux.com/devel/unixprg/ ● mq_open ● mq_getattr / mq_setattr ● mq_close ● mq_unlink ● mq_send ● mq_receive ● mq_notify

36 36 msqd_t msgID; msgID = mq_open(“Jens”, O_RDWR | O_CREAT | O_EXCL,...,..., NULL); mq_send(msgID, “cheese”, strlen(“cheese”)+1, 10 );... #define MAX_MSG_LEN 12345 char mcgcontent[MAX_MSG_LEN]; int msgsz,sender; msgsz = mq_receive(msgID, msgcontent, MAX_MSG_LEN, &sender)

37 37 MQ's mq_send(msg, “cheese”,... msgsz = mq_receive(msg,... QUESTIONS: ● Buffering mechanism inside OS ? - size,... ● Whatif space ends ? ● Real time ● Notification ? ● Beware of msq ID's like “Jens” ● File system like access rights. (see attribute part)


Download ppt "1 Threads and Communication. 2 ● One standard rules them all: POSIX IEEE 1003.* ● Some links ●"

Similar presentations


Ads by Google