Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : task1tRT.c
- Author : Dr Asghar Bokhari
- Version : Summer 2016
- Copyright : Your copyright notice
- Description : Create three tasks with 0 priority and SCHED_OTHER, to observer the need for synchronization
- ============================================================================
- */
- // _GNU_SOURCE //Must be defined as a symbol in properties
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <sched.h>
- #include <sys/mman.h>
- #include <string.h>
- #include <semaphore.h> //Required for sem_t
- #include <unistd.h> //removes getpid warning & allows sleep
- #define MY_PRIORITY3 (40) /* we use max value of 49 as the PRREMPT_RT use 50
- as the priority of kernel tasklets
- and interrupt handler by default */
- #define MY_PRIORITY2 (40)
- #define MY_PRIORITY1 (45)
- #define MAX_SAFE_STACK (8*1024) /* The maximum stack size which is
- guaranteed safe to access without
- faulting */
- #define NSEC_PER_SEC (1000000000) /* The number of nsecs per sec. */
- #define INTERVAL (50000)
- //global variables
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
- void stack_prefault(void) {
- unsigned char dummy[MAX_SAFE_STACK];
- memset(dummy, 0, MAX_SAFE_STACK);
- return;
- }
- // Function that will be used by Task1
- void* tfun1(void*n){
- struct timespec t;
- struct sched_param param;
- int interval = INTERVAL;
- int num_runs = 5; // Instaed of infinite loop, use finite number of iterations
- int num;
- /* Assign priority and scheduling policy to the task */
- param.sched_priority = MY_PRIORITY1;
- if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
- perror("sched_setscheduler failed");
- exit(-1);
- }
- /* Lock memory */
- // if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
- // perror("mlockall failed");
- // exit(-2);
- // }
- /* Pre-fault our stack */
- stack_prefault();
- pthread_mutex_lock(&mutex);
- pthread_cond_wait(&cond, &mutex);
- pthread_mutex_unlock(&mutex);
- clock_gettime(CLOCK_MONOTONIC ,&t);
- /* start after one second */
- t.tv_sec++;
- while(num_runs) {
- /* wait until next shot */
- clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
- printf("Hello! This is task1, Priority:%d, PID:%d\n", param.sched_priority, getpid());
- /* calculate next shot */
- t.tv_nsec += interval;
- while (t.tv_nsec >= NSEC_PER_SEC) {
- t.tv_nsec -= NSEC_PER_SEC;
- t.tv_sec++;
- }
- num_runs = num_runs -1;
- }
- //pthread_mutex_unlock(&mutex);
- return NULL;
- }
- void* tfun2(void*n){
- struct timespec t;
- struct sched_param param;
- int interval = INTERVAL;
- int num_runs = 5; // replaced infinite loop
- int num;
- /* Assign priority and scheduling policy to the task */
- param.sched_priority = MY_PRIORITY2;
- if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
- perror("sched_setscheduler failed");
- exit(-1);
- }
- /* Lock memory */
- // if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
- // perror("mlockall failed");
- // exit(-2);
- // }
- /* Pre-fault our stack */
- stack_prefault();
- pthread_mutex_lock(&mutex);
- pthread_cond_wait(&cond, &mutex);
- pthread_mutex_unlock(&mutex);
- clock_gettime(CLOCK_MONOTONIC ,&t);
- /* start after one second */
- t.tv_sec++;
- while(num_runs) {
- /* do the stuff */
- /* wait until next shot */
- clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
- printf("Hello! This is task2, Priority:%d, PID:%d\n", param.sched_priority, getpid());
- /* calculate next shot */
- t.tv_nsec += interval;
- while (t.tv_nsec >= NSEC_PER_SEC) {
- t.tv_nsec -= NSEC_PER_SEC;
- t.tv_sec++;
- }
- num_runs = num_runs -1;
- }
- //pthread_mutex_unlock(&mutex);
- return NULL;
- }
- void* tfun3(void*n){
- struct timespec t;
- struct sched_param param;
- int interval = INTERVAL;
- int num_runs = 5; // replaced infinite loop
- /* Declare ourself as a real time task */
- param.sched_priority = MY_PRIORITY3;
- if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
- perror("sched_setscheduler failed");
- exit(-1);
- }
- /* Lock memory */
- // if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
- // perror("mlockall failed");
- // exit(-2);
- // }
- /* Pre-fault our stack */
- stack_prefault();
- pthread_mutex_lock(&mutex);
- pthread_cond_wait(&cond, &mutex);
- pthread_mutex_unlock(&mutex);
- clock_gettime(CLOCK_MONOTONIC ,&t);
- /* start after one second */
- t.tv_sec++;
- while(num_runs) {
- /* wait until next shot */
- clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
- /* do the stuff */
- printf("Hello! This is task3, Priority:%d, PID:%d\n", param.sched_priority, getpid());
- /* calculate next shot */
- t.tv_nsec += interval;
- while (t.tv_nsec >= NSEC_PER_SEC) {
- t.tv_nsec -= NSEC_PER_SEC;
- t.tv_sec++;
- }
- num_runs = num_runs -1;
- }
- //pthread_mutex_unlock(&mutex);
- return NULL;
- }
- int main(int argc, char* argv[])
- {
- pthread_t tid1, tid2, tid3;
- // Create three threads
- pthread_create(&tid2, NULL, tfun2, NULL);
- pthread_create(&tid3, NULL, tfun3, NULL);
- pthread_create(&tid1, NULL, tfun1, NULL);
- sleep(1);
- pthread_mutex_lock(&mutex);
- pthread_cond_broadcast(&cond);
- pthread_mutex_unlock(&mutex);
- // Wait for the threads to terminate
- pthread_join(tid1, NULL);
- pthread_join(tid2, NULL);
- pthread_join(tid3, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement