14 - Linux Threads & Scheduling (1)OutlineScheduling Algorithm CategoriesBasic Scheduling AlgorithmsLinux ThreadsScheduling BasicsAnnouncementsReading:Linux ThreadsScheduling BasicsScheduling Algorithm CategoriesLinux processes and threads are both representedas kernel-space tasks.pthread_create()Clone creates a new task asThe task is assigned both a PID and TIDFundamental question: Which thread runs next?Basic Problem:Scheduling is a class of optimization problems thatcenters on resource allocation.It is a deep topic that spans many fields and wewill be restricting our focus very significantly.We will primarily consider the single-core scenario.A scheduling algorithm makes a scheduling decisionby solving the problem and producing a schedule.We can represent a schedule with a Gantt chart ora timing diagram.Assignment, ordering, and timing of tasks can beaffected by many more factors than those above.In a general-purpose OS, the scheduler does notusually know the tasks' traits beforehand, so thescheduling algorithm must be chosen to performwell in the average case.When can scheduling decisions be made?Any time the kernel runs.Clock interrupts are special hardware interruptsClock interrupts allow for both:Non-Preemptive SchedulingPreemptive SchedulingDifferent computing environments require differentscheduling algorithms that make differentoptimizations.We'll look at 3 contexts:Scheduling algorithms have some shared goals, aswell as context-specific goals:All systems:Batch systems:Batch systems:Interactive systems:Interactive systems:Real-Time systems:Real-Time systems:The scheduler is the part of the OS that makesthe decision, using a scheduling algorithm.Given K tasks that have:and N cores to run on, determine:clone()Tasks combine functionality from bothWe discussed tasks last week in lecture 11.PCB and TCB are combined into task_structUser-space POSIX API for thread creationTasks generally cannot influence the scheduler'sdecisionswhen process/thread:when interrupts occur:allows system to track timeallows scheduling to take place regularlyTasks cannot be interruptedTasks can be rescheduled during executionUsed everywhere nowScheduler only runs on task exit or by requesta thread in current process (shared space)always a new TIDprocessing time (CPU time)release time(maybe) deadline(maybe) weight/priorityassignment of tasks to coresordering of task executiontiming of task executionnew PID only if separate process (fork)PID and TID will be same in a new processchild threads have same PID, different TIDshows PID, PPID, and TID (LWP)TID can be accessed witha new process (new address space)Linux implementation uses clone syscallProcess and thread table => task tablesingle-threaded process => 1 taskmultithreaded process => 1 task per threadPA 3 will be out early next week instead of today.(there was a bug in the reference implementation)Reminder that PA 2 is due next Friday1)2)3)MOS 10.3.1 - 10.3.3, LDD 2MOS 2.4--------------=>=>=>---->>>>>>>>>>>>>>>>>>>----------------->>>------child_id = clone(function, stack_ptr, sharing_flags, arg);gettid()ps -eLfThread 1runningreadyblockedThread 2Thread 3Thread 4timeis created (fork/clone)Clock interrupt (running -> ready)occurs at regular intervals (ticks)blocking syscallhigh priority (must be handled ASAP)prevents a task from keeping CPU foreverclock interrupts do not cause switchingBatch SystemsFairness - each process gets "enough" CPU timeThroughput - maximize total jobs per unit timeFirst-Come, First-Serve (FIFO)Response Time - respond to request quicklyPriorityDeadlines - cannot miss themFixed PriorityPredictability - prevent quality degradationDynamic PriorityProportionality - match users' expectationsMultiple QueuesOthers (briefly)Turnaround Time - minimize mean time to finishShortest Job FirstShortest Remaining Time NextCPU Utilization - keep the CPU busyRound RobinPolicy Enforcement - meet imposed restrictionsBalance - use CPU and I/O efficientlyInteractive SystemsReal-Time Systemslarge servers or mainframes with very high workloadgeneral-purpose computers (e.g. your PC)safety-critical and embedded systems (e.g. a pacemaker)clock interrupts can cause switchingI/O interrupt (blocked -> ready)terminates (exit)block on I/O or eventother non-blocking syscallvoluntary switch (user-space threads)involuntary switch