Download presentation
Presentation is loading. Please wait.
Published byArthur Simmons Modified over 8 years ago
1
Computer Science, Texas A&M University 1 Machine Problem 2
2
Computer Science, Texas A&M University 2 Machine Problem 2 Step 1 ━ Create a library (Task class) which creates threads ━ Due on Nov 28 th Step 2 ━ Implement threads scheduling ━ Due on Dec 6 th
3
Computer Science, Texas A&M University 3 Step 1 Create a thread library ━ task.h, utils.h and task_test_step1.cpp are given to you ━ Implement task.cpp
4
Computer Science, Texas A&M University 4 Class Task class Task { protected: static const intMAX_NAME_LEN = 15; charname[MAX_NAME_LEN]; pthread_t thread_id; public: Task(const char _name[]); ~Task(); char*Name(); virtual voidStart(); virtual void Run() = 0; (pure virtual function) static void GracefullyExitMainThread(); };
5
Computer Science, Texas A&M University 5 Class Task Each task object is a separate thread The action of each thread is defined in Run() Task is a base class, other classes can inherit it and define their own actions (override Run()) ━ Examples in task_test_step1.cpp
6
Computer Science, Texas A&M University 6 Task.cppTask.cpp char* Task::Name() { return name; } void Task::Start() { // create thread // pthread_create() // use tfunc to create thread, check handout } // we don’t need to implement Task::Run() void Task::GracefullyExitMainThread() { // exit // pthread_exit() }
7
Computer Science, Texas A&M University 7 Sample Result starting task rudder control starting task avionics task in tfunc in tfunc rudder control Rudder Controller [rudder controlin tfunc ] running in tfunc avionics task Avionics System [avionics task] running avionics task waiting for next refresh interval rudder control waiting for next sensor input avionics task refreshing avionics screen avionics task waiting for next refresh interval rudder control issueing rudder control command rudder control waiting for next sensor input avionics task refreshing avionics screen avionics task waiting for next refresh interval rudder control issueing rudder control command …… rudder control issueing rudder control command rudder control waiting for next sensor input rudder control issueing rudder control command rudder control waiting for next sensor input rudder control issueing rudder control command
8
Computer Science, Texas A&M University 8 Step 2 Implement threads scheduling ━ Suspend a thread ━ Resume a thread Each thread is an object of class Schedulable (derived from class Task) Class Scheduler is responsible for threads scheduling ━ Maintain a queue of schedulable threads Class Semaphore for synchronization
9
Computer Science, Texas A&M University 9 Step 2 Structure ━ One scheduler ━ Multiple schedulable objects which are associated with the scheduler To do: ━ Schedulable.cpp, Scheduler.cpp, Semaphore.cpp
10
Computer Science, Texas A&M University 10 Scheduler.cppScheduler.cpp int Scheduler::enqueue(Schedulable * _task) { // lock mutex // push task into ready queue // unlock mutex } Schedulable* Scheduler::dequeue() { // lock mutex // pop a schedulable thread // unlock mutex } Schedulable* Scheduler::Current_Task() { // get current running task } int Scheduler::Kick_off() { // start scheduling (after all schedulable threads are in ready queue) // take a thread from the queue and unblock it }
11
Computer Science, Texas A&M University 11 Scheduler.cppScheduler.cpp int Scheduler::Start(Schedulable* _task) { // suspend this task using Block() // push this task into ready queue } int Scheduler::Yield() { // block the thread current thread // unblock next thread in the ready queue and make it running } int Scheduler::Resume(Schedulable * _task) { // push the task into ready queue // Resume does not trigger the task to continue running }
12
Computer Science, Texas A&M University 12 Schedulable.cppSchedulable.cpp int Schedulable::Start() { // create thread (it’s already implemented in Task::Start(), so you // don’t need to do anything here } int Scheduler::Block() { // P() } int Scheduler::Unblock() { // V() } void CarrierForRun() { // call this->sched->Start(this) to add it into ready queue // Run() // yeild after finishing }
13
Computer Science, Texas A&M University 13 Semaphore.cppSemaphore.cpp Semaphore::P() { // lock mutex value-- if (value < 0) block thread // unlock mutex } Semaphore::V() { // lock mutex value++ if (value <= 0) release thread // unlock mutex }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.