sliver__

5.4 Thread Scheduling 본문

CS/운영체제

5.4 Thread Scheduling

sliver__ 2023. 11. 18. 10:19
728x90

User-level threads are managed by a thread library, and the kernel is unaware of them. To run on a CPU, user-level threads must ulti- mately be mapped to an associated kernel-level thread, although this mapping may be indirect and may use a lightweight process (LWP). In this section, we explore scheduling issues involving user-level and kernel-level threads and offer specific examples of scheduling for Pthreads.

 

Contention Scope

The thread library schedules user- level threads to run on an available LWP. This scheme is known as process- contention scope (PCS), since competition for the CPU takes place among threads belonging to the same process.

To decide which kernel-level thread to schedule onto a CPU, the kernel uses system-contention scope (SCS).

 

 

Pthread Scheduling

 

PTHREAD SCOPE PROCESS schedules threads using PCS scheduling.

PTHREAD SCOPE SYSTEM schedules threads using SCS scheduling.

 

On systems implementing the many-to-many model, the PTHREAD SCOPE PROCESS policy schedules user-level threads onto available LWPs. The number of LWPs is maintained by the thread library, perhaps using scheduler activations (Section 4.6.5). The PTHREAD SCOPE SYSTEM scheduling policy will create and bind an LWP for each user-level thread on many-to-many systems, effectively mapping threads using the one-to-one policy.

 

The Pthread IPC (Interprocess Communication) provides two functions for setting—and getting—the contention scope policy:

pthread attr setscope(pthread attr t *attr, int scope)

pthread attr getscope(pthread attr t *attr, int *scope)

 

#include <pthread.h>
#include <stdio.h> #define NUM THREADS 5
int main(int argc, char *argv[])
{
    int i, scope;
    pthread t tid[NUM THREADS]; pthread attr t attr;
    /* get the default attributes */ pthread attr init(&attr);
    /* first inquire on the current scope */
    if (pthread attr getscope(&attr, &scope) != 0)
        fprintf(stderr, "Unable to get scheduling scope∖n"); else {
            if (scope == PTHREAD SCOPE PROCESS) printf("PTHREAD SCOPE PROCESS");
            else if (scope == PTHREAD SCOPE SYSTEM) printf("PTHREAD SCOPE SYSTEM");
            else
                fprintf(stderr, "Illegal scope value.∖n");
            5.4 Thread Scheduling 219
        }
    /* set the scheduling algorithm to PCS or SCS */ pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);
    /* create the threads */
    for (i = 0; i < NUM THREADS; i++)
        pthread create(&tid[i],&attr,runner,NULL);
    /* now join on each thread */
    for (i = 0; i < NUM THREADS; i++)
        pthread join(tid[i], NULL);
}
/* Each thread will begin control in this function */ void *runner(void *param)
{
    /* do some work ... */ pthread exit(0);
}
728x90

'CS > 운영체제' 카테고리의 다른 글

5.5 Multi-Processor Scheduling  (0) 2023.11.18
5.3 Scheduling Algorithms  (1) 2023.11.18
5.2 Scheduling Criteria  (0) 2023.11.18
5.1 Basic Concept of CPU Scheduling  (0) 2023.11.18
4.7 Operating-System Examples  (0) 2023.11.13
Comments