Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /*
  2. Напишите программу, создающую два-три потока,
  3. сообщающую приоритеты этих потоков
  4. */
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <sys/wait.h>
  10. #include <pthread.h>
  11.  
  12. void * thread_func(void *arg) {
  13. int loc_id = * (int *) arg;
  14.  
  15. for (int i = 0; i < 1; i++) {
  16. printf("Thread %i is running\n", loc_id);
  17. sleep(1);
  18. }
  19. }
  20. int main(int argc, char * argv[]) {
  21.  
  22. int id1, id2, result;
  23. pthread_t thread1, thread2;
  24. pthread_attr_t attr1, attr2;
  25. struct sched_param param1, param2;
  26. int priority1, priority2;
  27. int policy1, policy2;
  28. int newprio1 = 20, newprio2 = 30;
  29.  
  30. pthread_attr_init(&attr1);
  31. pthread_attr_getschedparam(&attr1, &param1);
  32. param1.sched_priority = newprio1;
  33. pthread_attr_setschedparam (&attr1, &param1);
  34. result = pthread_create(&thread1, &attr1, thread_func, &id1);
  35.  
  36. if (result != 0) {
  37. perror("Creating the first thread");
  38. return EXIT_FAILURE;
  39. }
  40.  
  41. priority1 = param1.sched_priority;
  42. printf("Thread #1 priority = %d\n", priority1);
  43.  
  44. id2 = 2;
  45. pthread_attr_init(&attr2);
  46. pthread_attr_getschedparam(&attr2, &param2);
  47. param2.sched_priority = newprio2;
  48. pthread_attr_setschedparam (&attr2, &param2);
  49. result = pthread_create(&thread2, &attr2, thread_func, &id2);
  50.  
  51. if (result != 0) {
  52. perror("Creating the second thread");
  53. return EXIT_FAILURE;
  54. }
  55.  
  56. priority2 = param2.sched_priority;
  57. printf("Thread #2 priority = %d\n", priority2);
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement