Write a program in C, which shows diffrence between pthread_attr_setschedpolicy and sched_setscheduler
Assistant
The main difference between the two functions pthread_attr_setschedpolicy() and sched_setscheduler() is that the first is used for setting the scheduling policy for a new pthread, whereas the second one is used for setting the scheduling policy for an existing thread or process.
Here's a simple illustration of both the functions in the same C program:
c
User
Write a C program, which allows to change priority of a thread from command line.
Assistant
In the C program below, a thread is created, and you can change the priority of the thread using command line arguments.
c
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <unistd.h>
void *thread_function(void *arg) {
printf("Thread started\n");
sleep(1);
printf("Thread end\n");
return NULL;
}
int main() {
pthread_t thread;
pthread_attr_t attr;
struct sched_param param;
// Set the scheduling policy and priority for the newly created pthread
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
param.sched_priority = 10;
pthread_attr_setschedparam(&attr, ¶m);
// Create the pthread with the specified SCHED_FIFO policy and priority 10
if (pthread_create(&thread, &attr, thread_function, NULL) != 0) {
perror("pthread_create");
return 1;
}
// Set the scheduling policy and priority for the main thread or process
param.sched_priority = 20;
if (sched_setscheduler(getpid(), SCHED_RR, ¶m) != 0) {
perror("sched_setscheduler");
return 1;
}
printf("Main started\n");
// Now join the pthread
pthread_join(thread, NULL);
printf("Main end\n");
return 0;
}
In this C program, we're setting up a new pthread_attr_t object and using that attr object with the pthread_create() function to create a separately scheduled thread. We use pthread_attr_setschedpolicy() to set the scheduling policy for the new thread.
For the main thread (the process itself), we use sched_setscheduler() to set the scheduling policy and priority directly. Note that the main thread will continue with the default scheduling policy and priority if we don't change it with sched_setscheduler(). The getpid() function is used to get the process ID (or thread ID, in the case of the main thread).
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *thread_function(void *arg) {
printf("Thread started\n");
sleep(1);
printf("Thread end\n");
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t thread;
pthread_attr_t attr;
struct sched_param param;
if (argc < 2) {
printf("Usage: %s <priority>\n", argv[0]);
return 1;
}
int priority = atoi(argv[1]);
// Set the scheduling policy and priority for the newly created pthread
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
param.sched_priority = priority;
pthread_attr_setschedparam(&attr, ¶m);
// Create the pthread with the specified SCHED_FIFO policy and priority
if (pthread_create(&thread, &attr, thread_function, NULL) != 0) {
perror("pthread_create");
return 1;
}
printf("Main started\n");
// Now join the pthread
pthread_join(thread, NULL);
printf("Main end\n");
return 0;
}
To compile and run the program:
Save the above code to a file named change_thread_priority.c.
Open a terminal and navigate to the file's location.
Compile the program using: gcc change_thread_priority.c -o change_thread_priority -lpthread
Run the program with a priority of your choice: ./change_thread_priority 10
Please note that on some systems, you may need to run the program with root privileges in order to set realtime scheduling policies (SCHED_FIFO or SCHED_RR) and high priorities. In this case, you can run the program using sudo: