Guı́a de Referencia Rápida de POSIX de Tiempo Real PThreads–Hebras Lı́mites Fichero de Configuración: /etc/security/limits.conf #include <pthread.h> pthread_attr_init(&attr) pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) pthread_attr_getinheritsched(&attr, &mode) pthread_attr_setschedpolicy(&attr, policy) pthread_attr_getschedpolicy(&attr, &policy) pthread_attr_setschedparam(&attr, &param) pthread_attr_getschedparam(&attr, &param) pthread_attr_setdetachstate(&attr, state) pthread_attr_getdetachstate(&attr, &state) pthread_attr_setstacksize(&attr, stacksize) pthread_attr_getstacksize(&attr, &stacksize) pthread_attr_destroy(&attr) alumno - memlock 51200 alumno - rtprio 50 #include <sys/time.h> #include <sys/resource.h> #include <unistd.h> getrlimit(resource, &rlim) setrlimit(resource, &rlim) // resource: RLIMIT_MEMLOCK, RLIMIT_RTPRIO Memoria Virtual #include <sys/mman.h> mlockall(flags) // flags: MCL_CURRENT | MCL_FUTURE pthread_t pthread_self() pthread_create(&thread_id, &attr, proc, arg) pthread_cancel(thread_id) pthread_join(thread_id, &ptr_retval) pthread_setschedparam(thread_id, policy, &param) pthread_getschedparam(thread_id, &policy, &param) pthread_exit(ptr_retval) // policy: SCHED_FIFO, SCHED_RR, SCHED_OTHER // param.sched_priority = priority; Relojes de Tiempo Real #include <time.h> clock_gettime(clk_id, &ts) clock_nanosleep(clk_id, flags, &ts, &remain) // clk_id: CLOCK_MONOTONIC, flags: TIMER_ABSTIME Señales #include <signal.h> sigemptyset(&sigset) sigaddset(&sigset, signum) sigprocmask(mode, &sigset, &oldset) sigwait(&sigset, &signum) sigwaitinfo(&sigset, &siginfo) sigqueue(pid, signum, value) sigaction(signum, &sigact, &oldact) PThreads–Exclusión Mutua #include <unistd.h> #define __USE_UNIX98 #include <pthread.h> pthread_mutexattr_init(&attr) pthread_mutexattr_setprotocol(&attr, protocol) pthread_mutexattr_getprotocol(&attr, &protocol) pthread_mutexattr_setprioceiling(&attr, prioceiling) pthread_mutexattr_getprioceiling(&attr, &prioceiling) pthread_mutexattr_destroy(&attr) // protocol: PTHREAD_PRIO_INHERIT, PTHREAD_PRIO_PROTECT #include <pthread.h> pthread_sigmask(mode, &sigset, &oldset) pthread_kill(thread_id, signum) // mode: SIG_BLOCK, SIG_UNBLOCK pthread_mutex_init(&mutex, &attr) pthread_mutex_lock(&mutex) pthread_mutex_trylock(&mutex) pthread_mutex_unlock(&mutex) pthread_mutex_destroy(&mutex) Timers #include <signal.h> #include <time.h> timer_create(clk_id, &sgev, &timer_id) timer_settime(timer_id, flags, &its, &oldits) timer_gettime(timer_id, &its) timer_delete(timer_id) // clk_id: CLOCK_MONOTONIC, flags: 0 // sgev.sigev_notify = SIGEV_SIGNAL; // sgev.sigev_signo = signum; PThreads–Variables de Condición #include <pthread.h> pthread_condattr_init(&attr) pthread_condattr_destroy(&attr) pthread_cond_init(&cond, &attr) pthread_cond_wait(&cond, &mutex) pthread_cond_timedwait(&cond, &mutex, &abstimeout) pthread_cond_signal(&cond) pthread_cond_broadcast(&cond) pthread_cond_destroy(&cond) Planificación de Procesos #include <sys/types.h> #include <unistd.h> pid_t getpid() PThreads–Semáforos #include <sched.h> sched_setparam(pid, &param) sched_getparam(pid, &param) sched_setscheduler(pid, policy, &param) sched_getscheduler(pid) // policy: SCHED_FIFO, SCHED_RR, SCHED_OTHER #include <semaphore.h> sem_init(&sem, pshared, val) sem_post(&sem) sem_wait(&sem) sem_trywait(&sem) sem_timedwait(&sem, &abstimeout) Compilación y Enlazado sem_getvalue(&sem, &val) gcc -Wall -Wextra -Werror -pthread -o main main.c -lpthreadsem_destroy(&sem) -lrt 1