By liran bh | 9/4/2016 | Linux Kernel & Internals

Linux Scheduler (practical)

 

 

Priorities Demo

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>

pthread_t tid[2];
int pid[2] = { 1, 2 };
int prio[2] = {50,50};
volatile int count0, count1;

void* doSomeThing(void *arg)
{
    unsigned long i = 0;

    if( *((int *)arg) == 1 )
    {
        printf("\n First thread started\n");
    }
    else
    {
        printf("\n Second thread started\n");
    }

    while(1)
    {
        switch( *((int *)arg) )
        {
            case 1:
                ++count0;
                break;
            case 2:
                ++count1;
                break;
            default:
                break;
        }
    }
    return NULL;
}

int main(void)
{
    int i = 0;
    int err;
    struct sched_param param;
    pthread_attr_t attr;
    while(i < 2)
    {
        param.sched_priority = prio[i];
        pthread_attr_init(&attr);
        pthread_attr_setschedpolicy(&attr,SCHED_RR);
        pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
        pthread_attr_setschedparam(&attr, &param);
        err = pthread_create(&(tid[i]), &attr, doSomeThing, &pid[ i ]);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        else
            printf("\n Thread created successfully\n");

        i++;
    }
    for(;;)
    {
        printf("%d %d \n", count0, count1);
        sleep(1);
    }
    pthread_join(tid[0], 0);
    pthread_join(tid[1], 0);
    return 0;
}

 

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now