Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Напишите программу, создающую два-три потока,
- сообщающую приоритеты этих потоков
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <pthread.h>
- void * thread_func(void *arg) {
- int loc_id = * (int *) arg;
- for (int i = 0; i < 1; i++) {
- printf("Thread %i is running\n", loc_id);
- sleep(1);
- }
- }
- int main(int argc, char * argv[]) {
- int id1, id2, result;
- pthread_t thread1, thread2;
- pthread_attr_t attr1, attr2;
- struct sched_param param1, param2;
- int priority1, priority2;
- int policy1, policy2;
- int newprio1 = 20, newprio2 = 30;
- pthread_attr_init(&attr1);
- pthread_attr_getschedparam(&attr1, ¶m1);
- param1.sched_priority = newprio1;
- pthread_attr_setschedparam (&attr1, ¶m1);
- result = pthread_create(&thread1, &attr1, thread_func, &id1);
- if (result != 0) {
- perror("Creating the first thread");
- return EXIT_FAILURE;
- }
- priority1 = param1.sched_priority;
- printf("Thread #1 priority = %d\n", priority1);
- id2 = 2;
- pthread_attr_init(&attr2);
- pthread_attr_getschedparam(&attr2, ¶m2);
- param2.sched_priority = newprio2;
- pthread_attr_setschedparam (&attr2, ¶m2);
- result = pthread_create(&thread2, &attr2, thread_func, &id2);
- if (result != 0) {
- perror("Creating the second thread");
- return EXIT_FAILURE;
- }
- priority2 = param2.sched_priority;
- printf("Thread #2 priority = %d\n", priority2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement