How to Create a Thread and Execute It on Specific CPU Core
What is Thread?
A thread is a path of execution within a process and a process can contain multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc. They can either execute in User space or kernel space [sudo].
Linux does not distinguish between processes and threads — it uses the more generic term "Tasks".
Creating of Thread
Header files used for thread handling:
#include <pthread.h>
/**********************************************************
* Author :- Aditya Gaurav *
* Mail :- adiitrack7@gmail.com *
* *
* Visit and You know what to do www.errbits.com *
*********************************************************/
/* @brief Create a new thread, starting with execution of
* START-ROUTINE getting passed ARG. Creation
* attributes come from ATTR. The new handle
* is stored in *NEWTHREAD.
* @param __newthread thread instance
* @param __attr attribute created for this variable
* @param A function pointer
* @param parameter passed to function pointer
* @return returns zero when the call completes successfully.
*/
extern int pthread_create (
pthread_t *__restrict __newthread,
const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROWNL __nonnull ((1, 3)
);
/* @brief Make calling thread wait for termination of the thread TH.
* The exit status of the thread is stored in *THREAD_RETURN,
* if THREAD_RETURN is not NULL.
* @param __th thread instance
* @param __thread_return pointer to return from thread
*/
extern int pthread_join (pthread_t __th, void **__thread_return);
One must configure what attributes to set for the thread: the scheduling policy (Round Robin and others), which CPU core to use or to use all cores, and the priority of that thread. You can also pass NULL and let the kernel decide its execution path.
Setting up thread attributes
Data type used for setting up attributes:
/** * @brief Attribute used along with thread creation */ pthread_attr_t fifo_sched_attr;
Three things we need to set:
1. Scheduling Policy
2. Priority of Thread
3. Core of execution
Scheduling Policy
The scheduler is the kernel component that decides which runnable thread will be executed by the CPU next. Each thread has an associated scheduling policy and a static scheduling priority, sched_priority. The scheduler makes its decisions based on knowledge of the scheduling policy and static priority of all threads on the system.
/** * @param pthread_attr_t attribute to setup policy * @param __policy Type of policy which you want to assign **/ pthread_attr_setschedpolicy(pthread_attr_t, __policy);
Priority of Thread
Processes scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR) have a sched_priority value in the range 1 (low) to 99 (high). Real-time threads always have higher priority than normal threads.
//priority value int max_prio, cpuidx; struct sched_param fifo_param; //get priority max_prio=sched_get_priority_max(SCHED_POLICY); fifo_param.sched_priority=max_prio; //setting scheduler with all details pthread_attr_setschedparam(&fifo_sched_attr, &fifo_param);
Core of execution
cpu_set_t cpuset; CPU_ZERO(&cpuset); //setting core id to use cpuidx=(1); CPU_SET(cpuidx, &cpuset); //API to set cpu core with SCHED policy pthread_attr_setaffinity_np(pthread_attr_t, sizeof(cpu_set_t), &cpuset);
For more details on CPU scheduling click here.