160 likes | 296 Views
Vorlesung Echtzeitbetriebssysteme V. Interprozesskoordination. Dr.-Ing. Frank Golatowski. Ziele der Vorlesung. Scheduling. SCHED_FIFO SCHED_RR SCHED_OTHER. Scheduling. sched_setscheduler (pid, policy, param) sched_setparam (pid,param) int sched_get_priority_min (policy) in Linux 1
E N D
VorlesungEchtzeitbetriebssysteme V. Interprozesskoordination Dr.-Ing. Frank Golatowski
Scheduling • SCHED_FIFO • SCHED_RR • SCHED_OTHER
Scheduling • sched_setscheduler (pid, policy, param) • sched_setparam (pid,param) • int sched_get_priority_min (policy) in Linux 1 • int sched_get_priority_max (policy) in Linux 99 • sched_yield(void) #include <sched.h> struct sched_param param; main(){ printf("RR min: %ld\n",sched_get_priority_min(SCHED_RR)); printf("RR max: %ld\n",sched_get_priority_max(SCHED_RR)); printf("FIFO min: %ld\n",sched_get_priority_min(SCHED_FIFO)); printf("FIFO max: %ld\n",sched_get_priority_max(SCHED_FIFO)); param.sched_priority = 10; fork(); sched_setscheduler(0,SCHED_FIFO, ¶m); tue_etwas(); }
Memlock • mlock (address, length) • munlock (address, length) • mlockall (flags) • munlockall (void) #include <sys/mman.h> main(){ mlock(MCL_CURRENT|MCL_FUTURE); /* */ tue_etwas(); }
Shared memory • void *mmap ( start, len, prot, flags, fd, offset) • munmap (start ,len) #include <sys/types.h> #include <unistd.h> #include <sys/mman.h> main(){ fd = open ("kk.ps",O_RDWR); ptr = mmap(NULL, 100*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED,fd,0L); } ptr[0]=0; if (fork()==0) { /* Sohn */ while (ptr[0]==0); } else { /* Vater */ ptr[0]=1; }
Synchronisation • sem_id sem_open (name, oflags, mode, init_value) • sem_close (sem_id) • sem_unlink (number)
Semaphore • sem_wait (sem_id) • sem_post (sem_id) • sem_trywait (sem_id) • int sem_get_value (sem_id) #include <time.h> #include <signal.h> #include <semaphore.h> main(){ sem_t *mutex; int x; mutex=sem_open("SEMA",O_CREAT,0,1); if (fork() == 0) { printf("Sohn startet" %d\n",x); sem_wait(mutex); printf("Sohn wird beendet\n"); } else { sleep(2); printf("Vater erwacht\n"); sem_post(mutex); printf("Vater wird beendet"\n"); } }
Signale • SIGILL • SIGSEGV • SIGTERM • SIGALRM • SIGKILL • SIGFPE • SIGSTOP, SIGCONT • SIGUSR1, SIGUSR2 • SIGRTMIN...SIGRTMAX
Signal Handling struct sigaction { void (*sa_handler)(); sigset_t sa_mask; int sa_flags; void (*sa_sigaction)(...); }; • int sigaction (int signal,const struct sigaction *act, struct sigaction *oact); • int kill (pid_t pid, int sig); • int sigprocmask(int how, const sigset_t *set, sigset_t *oset); • int sigsuspend(const sigset_t *set); sigemptyset(*s) sigfillset(*s) sigaddset(*s,n) sigdelset(*s,n) sigismember(*s,n) SIG_BLOCK SIG_UNBLOCK SIG_SETMASK
Signal Beispiel #include <signal.h> void handler (int senyal){ printf("an arithmetical exception has taken place\n"); } main(){ struct sigaction man; float a,b; int x; man.sa_flags = 0; man.sa_handler = handler; sigemptyset(&man.sa_mask); sigaction(SIGFPE, &man, NULL); for (x=0 ; x<100 ; x++) b = a / 0; printf("I am going to finalize\n"); }
Messagequeues • mq_open • mq_close • mq_unlink • mq_send • mq_receive • mq_setattr
Uhr (Clock) • clock_gettime • clock_getres • nanosleep #include <time.h> main(){ struct timespec B,A; clock_gettime(CLOCK_REALTIME,&A); clock_gettime(CLOCK_REALTIME,&B); printf("Two consecutive calls: %ld nseg\n", 1000000000L*(B.tv_sec-A.tv_sec)+(B.tv_nsec-A.tv_nsec)); }
Timer • timer_create (clock_id, sig_spec, timer_new) • timer_settimer (timer_id, flags, new_interval, old_interval) • timer_gettimer (timer_id, current_interval) • timer_delete (timer_id) struct itimerspec { struct timerspec it_value; struct timespec it_interval; };
Timer main(){ struct timespec B; struct itimerspec tempor_spec; timer_t tempor; struct sigevent evento; signal (SIGRTMIN,handler); evento.sigev_signo = SIGRTMIN; evento.sigev_notify = SIGEV_SIGNAL; timer_create(CLOCK_REALTIME, &evento,&tempor)); tempor_spec.it_value.tv_sec = 5; tempor_spec.it_value.tv_nsec = 0; tempor_spec.it_interval.tv_sec = 1; tempor_spec.it_interval.tv_nsec = 0; timer_settime(tempor, 0, &tempor_spec, NULL); clock_gettime(CLOCK_REALTIME,&B); printf("Instante => seg: %ld, nseg:%ld\n", B.tv_sec, B.tv_nsec); while (1) { sleep(100); } } #include <time.h> #include <signal.h> void handler (){ struct timespec ahora; signal(SIGRTMIN,manejador); clock_gettime(CLOCK_REALTIME,&ahora); printf("Instante => seg: %ld, nseg: %ld\n", ahora.tv_sec, ahorai.tv_nsec); } Noch nicht fertig