博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QNX 线程 调度策略 优先级 时钟频率 同步
阅读量:5836 次
发布时间:2019-06-18

本文共 2973 字,大约阅读时间需要 9 分钟。

/*

* barrier1.c
*/

#include <stdio.h>

#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <sys/neutrino.h>
#include <timer.h>

pthread_barrier_t barrier; // barrier synchronization object

void *thread1 (void *not_used)

{

int policy;

struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,&param);
if(policy == SCHED_OTHER)
printf("SCHED_OTHER 1 \n");
if(policy == SCHED_RR);
printf("SCHED_RR 1 \n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO 1 \n");
if(policy==SCHED_SPORADIC)
printf("SCHED_SPARODIC 1 \n");

setprio(0, 11); //设置线程优先级 越大优先级越高 0 代表当前进程

printf("thread1 priority is %d\n",getprio(0));

time_t now;

time (&now);

printf ("thread1 starting at %s", ctime (&now));

// do the computation

// let's just do a sleep here...
while(1)
{
delay (100);
pthread_barrier_wait (&barrier);
// after this point, all three threads have completed.
time (&now);
printf ("barrier in thread1() done at %s", ctime (&now));
}
}

void *thread2 (void *not_used)

{
//查看线程的调度策略 默认为轮询
int policy;
struct sched_param param;
pthread_getschedparam(pthread_self(),&policy,&param);
if(policy == SCHED_OTHER)
printf("SCHED_OTHER 2 \n");
if(policy == SCHED_RR);
printf("SCHED_RR 2 \n");
if(policy==SCHED_FIFO)
printf("SCHED_FIFO 2 \n");
if(policy==SCHED_SPORADIC)
printf("SCHED_SPARODIC 1 \n");

setprio(0, 10); //设置线程优先级 越大优先级越高 0 代表当前进程

printf("thread2 priority is %d\n",getprio(0));

time_t now;

time (&now);

printf ("thread2 starting at %s", ctime (&now));

// do the computation

// let's just do a sleep here...
while(1)
{
delay (200);
pthread_barrier_wait (&barrier);
// after this point, all three threads have completed.
time (&now);
printf ("barrier in thread2() done at %s", ctime (&now));
}
}

int main () // ignore arguments

{

pthread_t threadID1,threadID2;

setprio(0, 12); //设置线程优先级 越大优先级越高 0 代表当前进程

printf("Main priority is %d\n",getprio(0));
//CPU 时钟频率
struct _clockperiod timep;
timep.nsec = 10*1000;
timep.fract = 0;
int ErrCode = 0;
ErrCode = ClockPeriod(CLOCK_REALTIME, &timep, NULL, 0);
if(ErrCode!=0)
{
printf( "Error: %s\n", strerror(ErrCode));
}
/****************************************/
int ret = -1;
int timer_interrupt_id = -1;
ret = InitializeTimerInterrupt(0, 300, &timer_interrupt_id); //设置时钟中断,10ms
/*****************************************/
time_t now;

// create a barrier object with a count of 3

pthread_barrier_init (&barrier, NULL, 3);

// start up two threads, thread1 and thread2

pthread_create (&threadID1, 0, thread1, 0);
pthread_create (&threadID2, NULL, thread2, NULL);
// at this point, thread1 and thread2 are running

// now wait for completion

time (&now);
printf ("main() waiting for barrier at %s", ctime (&now));
while(1)
{
InterruptWait(0, NULL);
pthread_barrier_wait (&barrier);
// after this point, all three threads have completed.
time (&now);
printf ("barrier in main() done at %s", ctime (&now));
}
pthread_exit( NULL );
return (EXIT_SUCCESS);
}

转载于:https://www.cnblogs.com/splovecyk/p/4502388.html

你可能感兴趣的文章
[C++基础]在构造函数内部调用构造函数
查看>>
跟随我在oracle学习php(8)
查看>>
FZU - 1688 Binary land
查看>>
Spring 3.1.0 Hibernate 3.0 Eclipse Spring WEB例子
查看>>
3G路由器、无线接入点(无线AP)、无线路由器!
查看>>
新站如何提高百度权重
查看>>
android .9图的作用
查看>>
转换流,Properties 集合
查看>>
bootstrap列排序
查看>>
redis 常用操作
查看>>
如何用ABP框架快速完成项目(9) - 用ABP一个人快速完成项目(5) - 不要执着于设计模式和DDD理论,避免原教旨主义...
查看>>
用户交互
查看>>
putty title 显示IP
查看>>
编译vlc for android
查看>>
Dreamwearer 使用总结(OS X)
查看>>
日历打印用java实现
查看>>
Javascript去掉字符串左右空白函数
查看>>
【ubuntu 修改root密码】
查看>>
libkyototycoon.so.2: cannot open shared object file: No such file
查看>>
ASP.Net 后台发回错误
查看>>