Advertisement
maxim_shlyahtin

24_3

Jun 1st, 2024
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <linux/unistd.h>
  5. #include <sys/syscall.h>
  6. #include <sched.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <sys/resource.h>
  10. #define _GNU_SOURCE
  11.  
  12. pthread_t t1, t2, t3;
  13.  
  14. void *thread1()
  15. {
  16.     int i;
  17.     int tid, pid;
  18.     tid = syscall(SYS_gettid);
  19.     pid = getpid();
  20.     //int ret = pthread_getschedparam(tid, &policy, &param);
  21.     printf("Thread_1 with thread id = %d and pid = %d is started\n", tid, pid);
  22.     for (i = 0; i < 10; i++)
  23.     {
  24.         printf("Thread_1 is working\n");
  25.         sleep(1); // Sleep for 0.1 second to simulate workload
  26.     }
  27.     printf("Thread 1 has stopped working\n");
  28.     return NULL;
  29. }
  30.  
  31. void *thread2()
  32. {
  33.     int i;
  34.     int tid, pid;
  35.     tid = syscall(SYS_gettid);
  36.     pid = getpid();
  37.     printf("Thread_2 with thread id = %d and pid = %d is started\n", tid, pid);
  38.     for (i = 0; i < 10; i++)
  39.     {  
  40.         printf("Thread_2 is working\n");
  41.         sleep(1); // Sleep for 0.1 second to simulate workload
  42.     }
  43.     printf("Thread 2 has stopped working\n");
  44.     return NULL;
  45. }
  46.  
  47. void *thread3()
  48. {
  49.     int i;
  50.     int tid, pid;
  51.     tid = syscall(SYS_gettid);
  52.     pid = getpid();
  53.     printf("Thread_3 with thread id = %d and pid = %d is started\n", tid, pid);
  54.     for (i = 0; i < 10; i++)
  55.     {
  56.         printf("Thread_3 is working\n");
  57.         sleep(1); // Sleep for 0.1 second to simulate workload
  58.     }
  59.     printf("Thread 3 has stopped working\n");
  60.     return NULL;
  61. }
  62.  
  63. int main(int argc, char *argv[])
  64. {
  65.     int policy;
  66.     struct sched_param param;
  67.     pthread_attr_t attr_1, attr_2, attr_3;
  68.  
  69.     if (argc < 4) {
  70.         fprintf(stderr, "Usage: %s <priority_thread_1> <priority_thread_2> <priority_thread_3>\n", argv[0]);
  71.         return 1;
  72.     }
  73.  
  74.  
  75.     int priority1 = atoi(argv[1]);
  76.     int priority2 = atoi(argv[2]);
  77.     int priority3 = atoi(argv[3]);
  78.    
  79.  
  80.     pthread_attr_init(&attr_1);
  81.     pthread_attr_init(&attr_2);
  82.     pthread_attr_init(&attr_3);
  83.     pthread_attr_setschedpolicy(&attr_1, SCHED_FIFO);
  84.     pthread_attr_setschedpolicy(&attr_2, SCHED_FIFO);
  85.     pthread_attr_setschedpolicy(&attr_3, SCHED_FIFO);
  86.    
  87.     param.sched_priority = priority1;
  88.     pthread_attr_setschedparam(&attr_1, &param);
  89.     pthread_attr_getschedpolicy(&attr_1, &policy);
  90.     printf("Thread_1's priority = %d\n", param.sched_priority);
  91.    
  92.     param.sched_priority = priority2;
  93.     pthread_attr_setschedparam(&attr_2, &param);
  94.     pthread_attr_getschedpolicy(&attr_2, &policy);
  95.     printf("Thread_2's priority = %d\n", param.sched_priority);
  96.  
  97.     param.sched_priority = priority3;
  98.     pthread_attr_setschedparam(&attr_3, &param);
  99.     pthread_attr_getschedpolicy(&attr_3, &policy);
  100.     printf("Thread_2's priority = %d\n", param.sched_priority);
  101.  
  102.     //pthread_attr_setinheritsched(&attr_1, PTHREAD_EXPLICIT_SCHED);
  103.     //pthread_attr_setinheritsched(&attr_2, PTHREAD_EXPLICIT_SCHED);
  104.  
  105.     switch (policy)
  106.     {
  107.     case SCHED_FIFO:
  108.         printf("policy SCHED_FIFO\n");
  109.         break;
  110.     case SCHED_RR:
  111.         printf("policy SCHED_RR\n");
  112.         break;
  113.     case SCHED_OTHER:
  114.         printf("policy SCHED_OTHER\n");
  115.         break;
  116.     case -1:
  117.         perror("policy SCHED_GETSCHEDULER");
  118.         break;
  119.     default:
  120.         printf("policy Неизвестная политик планирования\n");
  121.     }
  122.  
  123.     pthread_create(&t1, &attr_1, thread1, NULL);
  124.     sleep(1);
  125.     pthread_create(&t2, &attr_2, thread2, NULL);
  126.     sleep(1);
  127.     pthread_create(&t3, &attr_3, thread3, NULL);
  128.  
  129.     pthread_join(t1, NULL);
  130.     pthread_join(t2, NULL);
  131.     pthread_join(t3, NULL);
  132.  
  133.     pthread_attr_destroy(&attr_1);
  134.     pthread_attr_destroy(&attr_2);
  135.     pthread_attr_destroy(&attr_3);
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement