본문 바로가기

Linux(Centos or RHEL)/RHEL 기초

프로세스를 TASK_RUNNING(실행 대기) 상태로 바꾸는 함수...?

TASK_RUNNING(실행 대기)로 바뀔 때 호출되는 함수들에 대해서 공부를 해 보았습니다.

 

아래 블로그를 참고하였습니다.

http://rousalome.egloos.com/10003490

 

[리눅스커널] 스케줄링: TASK_RUNNING(실행 대기)로 바뀔 때 호출되는 함수 - p

TASK_RUNNING(실행 대기)로 바뀔 때 호출하는 함수 분석 프로세스가 다음과 같은 동작을 수행할 때 실행대기(TASK_RUNNING) 상태로 바꿉니다.  프로세스를 깨울 때  프로세스를 처음 생성하고 실행 요청을 할 때  프로세스 관련 정보를 업데이트 할 때 보통 휴면 중에 있는 프로세스를 깨우면 프로세스는 실행대기(TASK_RUNNING)상

rousalome.egloos.com

 

프로세스의 상태를 TASK_RUNNING(실행 대기)로 바꾸는 동작은 보통은 다음과 같은 상황에서 수행됩니다.

(1) 프로세스를 깨울 때

(2) 프로세스를 처음 생성하고 실행 요청을 할 때

(3) 프로세스 관련 정보를 업데이트할 때

 

보통은 휴면 중에 있는 프로세스를 깨우면 프로세스는 실행 대기(TASK_RUNNING) 상태로 바뀝니다.

 

프로세스 상태가 실행 대기로 바꿀 때 호출하는 함수는 다음과 같은 것들이 있습니다.

(1) wake_up_new_task()

(2) wake_up_process()

(3) yield()

 

[https://elixir.bootlin.com/linux/v2.6.39.4/source/kernel/sched.c#L2703]

(1) wake_up_new_task()

 

주석 설명을 보면 프로세스가 처음 만들어졌을 때에 해당 프로세스를 런큐에 넣고, 깨운다는 함수라고 나와있습니다.

/*

* wake_up_new_task - wake up a newly created task for the first time.

*

* This function will do some initial scheduler statistics housekeeping

* that must be done for every newly created context, then puts the task

* on the runqueue and wakes it.

*/

void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)

{

unsigned long flags;

struct rq *rq;

int cpu __maybe_unused = get_cpu();

 

#ifdef CONFIG_SMP

rq = task_rq_lock(p, &flags);

p->state = TASK_WAKING;

 

/*

* Fork balancing, do it here and not earlier because:

* - cpus_allowed can change in the fork path

* - any previously selected cpu might disappear through hotplug

*

* We set TASK_WAKING so that select_task_rq() can drop rq->lock

* without people poking at ->cpus_allowed.

*/

cpu = select_task_rq(rq, p, SD_BALANCE_FORK, 0);

set_task_cpu(p, cpu);

 

p->state = TASK_RUNNING;

task_rq_unlock(rq, &flags);

#endif

 

rq = task_rq_lock(p, &flags);

activate_task(rq, p, 0);

trace_sched_wakeup_new(p, 1);

check_preempt_curr(rq, p, WF_FORK);

#ifdef CONFIG_SMP

if (p->sched_class->task_woken)

p->sched_class->task_woken(rq, p);

#endif

task_rq_unlock(rq, &flags);

put_cpu();

}

 

 

 

[https://elixir.bootlin.com/linux/v2.6.39.4/source/kernel/sched.c#L2577]

(2) wake_up_process()

주석 설명문을 보면 특정 프로세스를 깨운다고 나와있습니다. 해당 프로세스를 깨우는 것에 성공했다면 1, 이미 실행 중이라면 0을 리턴합니다. 여기서 프로세스를 깨운다는 의미에 대해서 생각을 해 보기로 합시다.

프로세스를 깨운다라는 문장은 프로세스 실행을 스케줄러에게 요청한다는 의미입니다. 이때 런큐에서 실행을 기다리는 프로세스들과 우선순위를 참고해서 프로세스의 실제 실행은 스케줄러가 수행합니다.

/**

* wake_up_process - Wake up a specific process

* @p: The process to be woken up.

*

* Attempt to wake up the nominated process and move it to the set of runnable

* processes. Returns 1 if the process was woken up, 0 if it was already

* running.

*

* It may be assumed that this function implies a write memory barrier before

* changing the task state if and only if any tasks are woken up.

*/

int wake_up_process(struct task_struct *p)

{

return try_to_wake_up(p, TASK_ALL, 0);

}

 

try_to_wake_up() 함수는 나중에 따로 다루어보기로 하겠습니다.

 

 

 

[https://elixir.bootlin.com/linux/v2.6.39.4/source/kernel/sched.c#L5470]

(3) yield()

 

프로세스 실행을 잠시 양보할 때 호출하는 yield() 함수에서도 프로세스의 상태를 실행 대기(TASK_RUNNING)으로 바꿉니다.

 

/**

* yield - yield the current processor to other threads.

*

* This is a shortcut for kernel-space yielding - it marks the

* thread runnable and calls sys_sched_yield().

*/

void __sched yield(void)

{

set_current_state(TASK_RUNNING);

sys_sched_yield();

}

EXPORT_SYMBOL(yield);