Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <linux/unistd.h>
- #include <sys/syscall.h>
- #include <sched.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/resource.h>
- #define _GNU_SOURCE
- pthread_t t1, t2, t3;
- void *thread1()
- {
- int i;
- int tid, pid;
- tid = syscall(SYS_gettid);
- pid = getpid();
- //int ret = pthread_getschedparam(tid, &policy, ¶m);
- printf("Thread_1 with thread id = %d and pid = %d is started\n", tid, pid);
- for (i = 0; i < 10; i++)
- {
- printf("Thread_1 is working\n");
- sleep(1); // Sleep for 0.1 second to simulate workload
- }
- printf("Thread 1 has stopped working\n");
- return NULL;
- }
- void *thread2()
- {
- int i;
- int tid, pid;
- tid = syscall(SYS_gettid);
- pid = getpid();
- printf("Thread_2 with thread id = %d and pid = %d is started\n", tid, pid);
- for (i = 0; i < 10; i++)
- {
- printf("Thread_2 is working\n");
- sleep(1); // Sleep for 0.1 second to simulate workload
- }
- printf("Thread 2 has stopped working\n");
- return NULL;
- }
- void *thread3()
- {
- int i;
- int tid, pid;
- tid = syscall(SYS_gettid);
- pid = getpid();
- printf("Thread_3 with thread id = %d and pid = %d is started\n", tid, pid);
- for (i = 0; i < 10; i++)
- {
- printf("Thread_3 is working\n");
- sleep(1); // Sleep for 0.1 second to simulate workload
- }
- printf("Thread 3 has stopped working\n");
- return NULL;
- }
- int main(int argc, char *argv[])
- {
- int policy;
- struct sched_param param;
- pthread_attr_t attr_1, attr_2, attr_3;
- if (argc < 4) {
- fprintf(stderr, "Usage: %s <priority_thread_1> <priority_thread_2> <priority_thread_3>\n", argv[0]);
- return 1;
- }
- int priority1 = atoi(argv[1]);
- int priority2 = atoi(argv[2]);
- int priority3 = atoi(argv[3]);
- pthread_attr_init(&attr_1);
- pthread_attr_init(&attr_2);
- pthread_attr_init(&attr_3);
- pthread_attr_setschedpolicy(&attr_1, SCHED_FIFO);
- pthread_attr_setschedpolicy(&attr_2, SCHED_FIFO);
- pthread_attr_setschedpolicy(&attr_3, SCHED_FIFO);
- param.sched_priority = priority1;
- pthread_attr_setschedparam(&attr_1, ¶m);
- pthread_attr_getschedpolicy(&attr_1, &policy);
- printf("Thread_1's priority = %d\n", param.sched_priority);
- param.sched_priority = priority2;
- pthread_attr_setschedparam(&attr_2, ¶m);
- pthread_attr_getschedpolicy(&attr_2, &policy);
- printf("Thread_2's priority = %d\n", param.sched_priority);
- param.sched_priority = priority3;
- pthread_attr_setschedparam(&attr_3, ¶m);
- pthread_attr_getschedpolicy(&attr_3, &policy);
- printf("Thread_2's priority = %d\n", param.sched_priority);
- //pthread_attr_setinheritsched(&attr_1, PTHREAD_EXPLICIT_SCHED);
- //pthread_attr_setinheritsched(&attr_2, PTHREAD_EXPLICIT_SCHED);
- switch (policy)
- {
- case SCHED_FIFO:
- printf("policy SCHED_FIFO\n");
- break;
- case SCHED_RR:
- printf("policy SCHED_RR\n");
- break;
- case SCHED_OTHER:
- printf("policy SCHED_OTHER\n");
- break;
- case -1:
- perror("policy SCHED_GETSCHEDULER");
- break;
- default:
- printf("policy Неизвестная политик планирования\n");
- }
- pthread_create(&t1, &attr_1, thread1, NULL);
- sleep(1);
- pthread_create(&t2, &attr_2, thread2, NULL);
- sleep(1);
- pthread_create(&t3, &attr_3, thread3, NULL);
- pthread_join(t1, NULL);
- pthread_join(t2, NULL);
- pthread_join(t3, NULL);
- pthread_attr_destroy(&attr_1);
- pthread_attr_destroy(&attr_2);
- pthread_attr_destroy(&attr_3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement