日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux c 进程策略 优先级,当两个线程拥有相同优先级时,linux c的线程调度策略问题...

發(fā)布時間:2024/1/23 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux c 进程策略 优先级,当两个线程拥有相同优先级时,linux c的线程调度策略问题... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

/* critical.c

*

* compile with gcc critical.c -o critical -lrt -lpthread

*

* 當主線程和A,B優(yōu)先級相同時,結果為aaaaabbbbb

* 當主線程的優(yōu)先級比另外兩個高時, 結果為bbbbbaaaaa

*/

#include /* header file for pthreads */

#include /* header file for POSIX conformance */

#include /* header file for POSIX time management */

#include /* header file for POSIX scheduling */

#include /* header file for standard input/outputlibrary */

#define _REENTRANT /* macro to ensure system calls are reentrant */

void *threadA(void *); /* predefine threadA routine */

void *threadB(void *); /* predefine threadB routine */

pthread_t threadA_id,threadB_id,main_id; /* thread identifiers */

pthread_attr_t attrA,attrB; /* thread attribute structures */

struct sched_param param; /* scheduling structure for thread attributes */

int policy=SCHED_FIFO;

int priority_min,priority_max; /* for range of priority levels */

/* main routine */

int main()

{

struct timespec start;

int status; /* check that system calls return ok */

clock_gettime(CLOCK_REALTIME, &start); /* get the time */

printf("Start time is: %d seconds %d nano_seconds\n",start.tv_sec,start.tv_nsec);

/* Set processor affinity */

unsigned long mask = 1; /* use only 1 CPU core */

unsigned int len = sizeof(mask);

status = sched_setaffinity(0, len, &mask);

if (status < 0) perror("sched_setaffinity");

status = sched_getaffinity(0, len, &mask);

if (status < 0) perror("sched_getaffinity");

/* Find priority limits */

priority_max = sched_get_priority_max(policy);

priority_min = sched_get_priority_min(policy);

/* Change priority and policy of main thread */

main_id = pthread_self();

param.sched_priority=priority_min;

status = pthread_setschedparam(main_id, policy, &param);

if (status != 0) perror("pthread_setschedparam"); /* error check */

/* Create threadA */

param.sched_priority = priority_min;

pthread_attr_init(&attrA);

status = pthread_attr_setschedpolicy(&attrA,policy);

if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */

status = pthread_attr_setschedparam(&attrA,&param);

if (status != 0) perror("pthread_attr_setschedparam"); /* error check */

status = pthread_create(&threadA_id, &attrA, threadA, NULL);

if (status != 0) perror("pthread_create"); /* error check */

status = pthread_setschedparam(threadA_id,policy,&param);

if (status != 0) perror("pthread_setschedparam");

/* Create threadB */

param.sched_priority = priority_min; /* so that B runs with a higher priority than A */

pthread_attr_init(&attrB);

status = pthread_attr_setschedpolicy(&attrB,policy);

if (status != 0) perror("pthread_attr_setschedpolicy"); /* error check */

status = pthread_attr_setschedparam(&attrB,&param);

if (status != 0) perror("pthread_attr_setschedparam"); /* error check */

status = pthread_create(&threadB_id, &attrB, threadB, NULL);

if (status != 0) perror("pthread_create"); /* error check */

status = pthread_setschedparam(threadB_id,policy,&param);

if (status != 0) perror("pthread_setschedparam");

/* Join threads - force main to wait for the thread to terminate */

printf("main() waiting for threads\n");

status = pthread_join(threadA_id, NULL);

if (status != 0) perror("pthread_join(threadA_id, NULL)"); /* error check */

status = pthread_join(threadB_id, NULL);

if (status != 0) perror("pthread_join(threadB_id, NULL)"); /* error check */

printf("\nmain() reporting that all threads have terminated\n");

return(0);

} /* end of main */

void *threadA(void *arg)

{

int j;

for(j=1;j<=5;j++){

printf("a");

}

return (NULL);

}

void *threadB(void *arg)

{

int j;

for(j=1;j<=5;j++){

printf("b");

}

return (NULL);

}

總結

以上是生活随笔為你收集整理的linux c 进程策略 优先级,当两个线程拥有相同优先级时,linux c的线程调度策略问题...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。