항상 우선순위가 가장 높은 사람이 실행하고 있습니다.

7.4.4.4. Actions performed by schedule( ) to make the process switch Now the schedule( ) function has determined the next process to run. In a moment, the kernel will access thetHRead_info data structure ofnext, whose address is stored close to the top ofnext's process descriptor:
switch_tasks:

prefetch(next);
The prefetch macro is a hint to the CPU control unit to bring the contents of the first fields ofnext's process descriptor in the hardware cache. It is here just to improve the performance ofschedule( ), because the data are moved in parallel to the execution of the following instructions, which do not affectnext. Before replacing prev, the scheduler should do some administrative work:
clear_tsk_need_resched(prev);
rcu_qsctr_inc(prev->thread_info->cpu);
The clear_tsk_need_resched( ) function clears the TIF_NEED_RESCHED flag of prev, just in case schedule( ) has been invoked in the lazy way. Then, the function records that the CPU is going through a quiescent state (see the section "Read-Copy Update (RCU)"inChapter 5). The schedule( ) function must also decrease the average sleep time ofprev, charging to it the slice of CPU time used by the process:
prev->sleep_avg -= run_time;
if ((long)prev->sleep_avg <= 0)
    prev->sleep_avg = 0;
prev->timestamp = prev->last_ran = now;
The timestamps of the process are then updated. It is quite possible that prev andnext are the same process: this happens if no other higher or equal priority active process is present in the runqueue. this case에서 the function skips the process switch:
if (prev == next) {
    spin_unlock_irq(&rq->lock);
    goto finish_schedule;
}
cpu는 이벤트 대기열에서 우선순위가 가장 높은 프로세스를 선택하여 실행합니다. 타임 슬라이스가 끝난 후에 이 프로세스는 상호작용이 강하지 않으면 기한이 지난 대기열에 끼워 넣고 이벤트 대기열에서 우선순위가 가장 높은 프로세스를 선택하여 실행합니다.만약에 이 프로세스가 상호작용이 강하고 다른 조건을 만족시킨다면 타임 슬라이스가 다 써도 이벤트 대기열에 넣을 것입니다. 그리고 cpu가 다시 이벤트 대기열에서 우선순위가 가장 높은 프로세스를 선택할 때 방금 실행된 프로세스일 가능성이 높습니다. 그러면 매번 실행된 프로세스가 방금 실행된 프로세스일 수 있습니까?아니오: 기한이 지난 대기열의 대기 시간이 너무 길면 어떤 프로세스의 상호작용이 강하더라도 타임슬립을 다 쓰면 다시 이벤트 대기열에 넣지 않습니다. 일반적으로 상호작용이 강한 프로세스는 자주 막힙니다. 매번 타임슬립의 일부분만 사용합니다. (확실하지 않습니다. 먼저 추측해 보세요) 이벤트 대기열에 프로세스가 없습니다. 즉, cpu가 이벤트 140개 대기열의 프로세스를 한 번 실행했습니다.runqueue 구조의 active와 expired를 교환합니다. 즉, 기한이 지난 대기열은 활동 대기열로 바뀌고, 활동 대기열은 기한이 지난 팀 cpu로 바뀌며, 다음 순환 루트 우선 순위 설정: 값이 클수록 우선 순위가 높습니다.1. 동일한 프로세스 내부에 속하는 스레드에 대해 SCHED_ 사용OTHER 는 SCHED_ 를 사용할 수 있는 일반 스레드를 조정합니다.FIFO 및 SCHED_RR 스케줄링의 스레드 선점프로세스에 SCHED만 있는 경우 _OTHER의 몇 개의 라인은 이 몇 개의 라인이 순환하여 cpu 타임라인을 차지한다.SCHED_OTHER 지원의 우선 순위는 0이며 0입니다.(0이 가장 낮으므로 SCHED_FIFO 및 SCHED_RR이 선점할 수 있음) SCHED_FIFO 및 SCHED_RR, 지원 우선 순위는 1-99입니다.(99가 가장 높고 가장 먼저 스케줄링됨)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <linux/sched.h>
#include <time.h>
#include <sys/time.h>



#define DBG(...) printf("line=%d: ",__LINE__);printf(__VA_ARGS__)
#define NUM 10


void gettime()
{
	struct timeval tv;
	gettimeofday(&tv,NULL);
	printf("now tv_sec=%d,tv_usec=%d
",tv.tv_sec,tv.tv_usec); } void show_thread_priority( int policy ) { int prioritymax = sched_get_priority_max( policy ); int prioritymin = sched_get_priority_min( policy ); printf("min_priority=%d,min_priority=%d
",prioritymin,prioritymax); } void * Thread1(void * pp) { int i,j; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER 1
"); if(policy == SCHED_RR); printf("SCHED_RR 1
"); if(policy==SCHED_FIFO) printf("SCHED_FIFO 1
"); for(i=1;i<NUM;i++) { for(j=1;j<5000000;j++) { printf("thread1 ,i=%d,j=%d ",i,j); gettime(); usleep(1000*100); } printf("thread 1
"); } printf("Pthread 1 exit
"); } void * Thread2(void * pp) { int i,j,m; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER 2
"); if(policy == SCHED_RR); printf("SCHED_RR 2
"); if(policy==SCHED_FIFO) printf("SCHED_FIFO 2
"); for(i=1;i<NUM;i++) { for(j=1;j<5000000;j++) { printf("thread22 ,i=%d,j=%d ",i,j); gettime(); usleep(1000*100); } printf("thread 2
"); } printf("Pthread 2 exit
"); } void * Thread3(void * pp) { int i,j; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER 3
"); if(policy == SCHED_RR) printf("SCHED_RR 3
"); if(policy==SCHED_FIFO) printf("SCHED_FIFO 3
"); for(i=1;i<NUM;i++) { for(j=1;j<5000000;j++) { printf("thread333 ,i=%d,j=%d ",i,j); gettime(); usleep(1000*100); } printf("thread 3
"); } printf("Pthread 3 exit
"); } int main() { int i; i = getuid(); if(i==0) printf("The current user is root
"); else printf("The current user is not root
"); pthread_t ppid1,ppid2,ppid3; struct sched_param param; pthread_attr_t attr1,attr2,attr3; show_thread_priority(SCHED_OTHER); show_thread_priority(SCHED_RR); show_thread_priority(SCHED_FIFO); pthread_attr_init(&attr1); pthread_attr_init(&attr2); pthread_attr_init(&attr3); param.sched_priority = 1;//SCHED_OTHER not support pri pthread_attr_setschedpolicy(&attr1,SCHED_RR); pthread_attr_setschedparam(&attr1,¶m); pthread_attr_setinheritsched(&attr1,PTHREAD_EXPLICIT_SCHED); param.sched_priority = 1; pthread_attr_setschedpolicy(&attr2,SCHED_RR); pthread_attr_setschedparam(&attr2,¶m); pthread_attr_setinheritsched(&attr2,PTHREAD_EXPLICIT_SCHED); pthread_create(&ppid1,&attr1,Thread1,NULL); pthread_create(&ppid2,&attr2,Thread2,NULL); sleep(1); param.sched_priority = 1; pthread_attr_setschedpolicy(&attr3,SCHED_FIFO); pthread_attr_setschedparam(&attr3,¶m); pthread_attr_setinheritsched(&attr3,PTHREAD_EXPLICIT_SCHED);// pthread_create(&ppid3,&attr3,Thread3,NULL); pthread_join(ppid1,NULL); pthread_join(ppid2,NULL); pthread_join(ppid3,NULL); pthread_attr_destroy(&attr1); pthread_attr_destroy(&attr2); pthread_attr_destroy(&attr3); return 0; }
프로세스의 우선순위.값이 작을수록 우선순위가 높습니다. 우선nice값이 있습니다. 수치범위-20~~19,-20은 우선순위가 가장 높음(가장 아낌없음)을 나타냅니다.프로세스의 우선순위를 설정하고nice-nxx를 사용합니다. 예를 들어nice-n19ls를 19로 설정하고 최저 우선순위 ncie-n-20ps를 -20으로 설정합니다. 최고 우선순위는renice xxpid를 사용합니다. 예를 들어 -10,renice-10pidps-el
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0     1     0  0  99  19 -   765 poll_s ?        00:00:00 init
1 S     0     2     0  0  80   0 -     0 kthrea ?        00:00:00 kthreadd
을 설정한 다음에 프로세스의 PRI=80+NI를 사용할 수 있습니다. 그래서 만약에 NI=-20이면 PRI=60,우선 순위 최대 top - H pstree-p pidcat/proc/pid/status 프로세스의 스레드 수를 볼 수 있음

좋은 웹페이지 즐겨찾기