Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.79 KB | None | 0 0
  1. /*
  2.  ============================================================================
  3.  Name        : task1tRT.c
  4.  Author      : Dr Asghar Bokhari
  5.  Version     : Summer 2016
  6.  Copyright   : Your copyright notice
  7.  Description : Create three tasks with 0 priority and SCHED_OTHER, to observer the need for synchronization
  8.  ============================================================================
  9.  */
  10. // _GNU_SOURCE //Must be defined as a symbol in properties
  11. #include <pthread.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <sched.h>
  16. #include <sys/mman.h>
  17. #include <string.h>
  18. #include <semaphore.h> //Required for sem_t
  19. #include <unistd.h> //removes getpid warning & allows sleep
  20.  
  21.  
  22. #define MY_PRIORITY3 (40) /* we use max value of 49 as the PRREMPT_RT use 50
  23.                             as the priority of kernel tasklets
  24.                             and interrupt handler by default */
  25. #define MY_PRIORITY2 (40)
  26. #define MY_PRIORITY1 (45)
  27.  
  28. #define MAX_SAFE_STACK (8*1024) /* The maximum stack size which is
  29.                                    guaranteed safe to access without
  30.                                    faulting */
  31.  
  32. #define NSEC_PER_SEC    (1000000000) /* The number of nsecs per sec. */
  33. #define INTERVAL (50000)
  34.  
  35. //global variables
  36. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  37. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  38.  
  39. void stack_prefault(void) {
  40.  
  41.         unsigned char dummy[MAX_SAFE_STACK];
  42.  
  43.         memset(dummy, 0, MAX_SAFE_STACK);
  44.         return;
  45. }
  46.  
  47. // Function that will be used by Task1
  48.  
  49. void* tfun1(void*n){
  50.         struct timespec t;
  51.         struct sched_param param;
  52.         int interval = INTERVAL;
  53.         int num_runs = 5; // Instaed of infinite loop, use finite number of iterations
  54.         int num;
  55.  
  56.         /* Assign priority and scheduling policy to the task */
  57.  
  58.         param.sched_priority = MY_PRIORITY1;
  59.         if(sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
  60.                 perror("sched_setscheduler failed");
  61.                 exit(-1);
  62.         }
  63.  
  64.         /* Lock memory */
  65.  
  66.         // if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
  67.         //         perror("mlockall failed");
  68.         //         exit(-2);
  69.         // }
  70.  
  71.         /* Pre-fault our stack */
  72.  
  73.         stack_prefault();
  74.         pthread_mutex_lock(&mutex);
  75.         pthread_cond_wait(&cond, &mutex);
  76.         pthread_mutex_unlock(&mutex);
  77.  
  78.         clock_gettime(CLOCK_MONOTONIC ,&t);
  79.         /* start after one second */
  80.         t.tv_sec++;
  81.  
  82.         while(num_runs) {
  83.                 /* wait until next shot */
  84.                 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
  85.  
  86.                 printf("Hello! This is task1, Priority:%d, PID:%d\n", param.sched_priority, getpid());
  87.                
  88.                 /* calculate next shot */
  89.                 t.tv_nsec += interval;
  90.  
  91.                 while (t.tv_nsec >= NSEC_PER_SEC) {
  92.                        t.tv_nsec -= NSEC_PER_SEC;
  93.                         t.tv_sec++;
  94.                 }
  95.                 num_runs = num_runs -1;
  96.         }
  97.         //pthread_mutex_unlock(&mutex);
  98.         return NULL;
  99. }
  100.  
  101. void* tfun2(void*n){
  102.         struct timespec t;
  103.         struct sched_param param;
  104.         int interval = INTERVAL;
  105.         int num_runs = 5; // replaced infinite loop
  106.         int num;
  107.         /* Assign priority and scheduling policy to the task */
  108.  
  109.         param.sched_priority = MY_PRIORITY2;
  110.         if(sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
  111.                 perror("sched_setscheduler failed");
  112.                 exit(-1);
  113.         }
  114.  
  115.         /* Lock memory */
  116.  
  117.         // if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
  118.         //         perror("mlockall failed");
  119.         //         exit(-2);
  120.         // }
  121.  
  122.         /* Pre-fault our stack */
  123.  
  124.         stack_prefault();
  125.         pthread_mutex_lock(&mutex);
  126.         pthread_cond_wait(&cond, &mutex);
  127.         pthread_mutex_unlock(&mutex);
  128.  
  129.         clock_gettime(CLOCK_MONOTONIC ,&t);
  130.         /* start after one second */
  131.         t.tv_sec++;
  132.  
  133.         while(num_runs) {
  134.                 /* do the stuff */
  135.                 /* wait until next shot */
  136.                 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
  137.                
  138.                 printf("Hello! This is task2, Priority:%d, PID:%d\n", param.sched_priority, getpid());
  139.                
  140.                 /* calculate next shot */
  141.                 t.tv_nsec += interval;
  142.  
  143.                 while (t.tv_nsec >= NSEC_PER_SEC) {
  144.                        t.tv_nsec -= NSEC_PER_SEC;
  145.                         t.tv_sec++;
  146.                 }
  147.                 num_runs = num_runs -1;
  148.         }
  149.         //pthread_mutex_unlock(&mutex);
  150.         return NULL;
  151. }
  152.  
  153. void* tfun3(void*n){
  154.         struct timespec t;
  155.         struct sched_param param;
  156.         int interval = INTERVAL;
  157.         int num_runs = 5; // replaced infinite loop
  158.  
  159.         /* Declare ourself as a real time task */
  160.  
  161.         param.sched_priority = MY_PRIORITY3;
  162.         if(sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
  163.                 perror("sched_setscheduler failed");
  164.                 exit(-1);
  165.         }
  166.  
  167.         /* Lock memory */
  168.  
  169.         // if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
  170.         //         perror("mlockall failed");
  171.         //         exit(-2);
  172.         // }
  173.  
  174.         /* Pre-fault our stack */
  175.  
  176.         stack_prefault();
  177.         pthread_mutex_lock(&mutex);
  178.         pthread_cond_wait(&cond, &mutex);
  179.         pthread_mutex_unlock(&mutex);
  180.  
  181.         clock_gettime(CLOCK_MONOTONIC ,&t);
  182.         /* start after one second */
  183.         t.tv_sec++;
  184.  
  185.         while(num_runs) {
  186.                 /* wait until next shot */
  187.                 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
  188.  
  189.                 /* do the stuff */
  190.                 printf("Hello! This is task3, Priority:%d, PID:%d\n", param.sched_priority, getpid());
  191.  
  192.                 /* calculate next shot */
  193.                 t.tv_nsec += interval;
  194.  
  195.                 while (t.tv_nsec >= NSEC_PER_SEC) {
  196.                        t.tv_nsec -= NSEC_PER_SEC;
  197.                         t.tv_sec++;
  198.                 }
  199.                 num_runs = num_runs -1;
  200.         }
  201.         //pthread_mutex_unlock(&mutex);
  202.         return NULL;
  203. }
  204.  
  205.  
  206. int main(int argc, char* argv[])      
  207. {
  208.         pthread_t tid1, tid2, tid3;
  209.        
  210.         // Create three threads
  211.         pthread_create(&tid2, NULL, tfun2, NULL);
  212.         pthread_create(&tid3, NULL, tfun3, NULL);
  213.         pthread_create(&tid1, NULL, tfun1, NULL);
  214.  
  215.         sleep(1);
  216.         pthread_mutex_lock(&mutex);
  217.         pthread_cond_broadcast(&cond);
  218.         pthread_mutex_unlock(&mutex);
  219.  
  220.         // Wait for the threads to terminate
  221.         pthread_join(tid1, NULL);
  222.         pthread_join(tid2, NULL);
  223.         pthread_join(tid3, NULL);
  224.  
  225.         return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement