1

update preemption demo

This commit is contained in:
2022-07-11 19:22:23 +02:00
parent 60e36011e3
commit 25da4b8596
4 changed files with 31 additions and 45 deletions

View File

@ -1,17 +0,0 @@
#include "user/PreemptiveLoopThread.h"
void PreemptiveLoopThread::run() {
/* Hier muss Code eingefuegt werden */
int cnt = 0;
while (true) {
// Basic synchronization by semaphore
sem->p();
kout.setpos(55, this->id);
kout << this->id << ": " << dec << cnt++ << endl;
sem->v();
}
}

View File

@ -1,21 +0,0 @@
#ifndef __preemptive_thread_include__
#define __preemptive_thread_include__
#include "kernel/Globals.h"
#include "kernel/threads/Thread.h"
#include "user/PreemptiveLoopThread.h"
class PreemptiveThreadDemo : public Thread {
private:
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
public:
PreemptiveThreadDemo() {
kout << "Initialized PreemptiveThreadDemo" << endl;
}
// Thread-Startmethode
void run() override;
};
#endif

View File

@ -1,11 +1,23 @@
#include "user/PreemptiveThreadDemo.h"
#include "lib/Semaphore.h"
#include "user/demo/PreemptiveThreadDemo.h"
void PreemptiveLoopThread::run() {
int cnt = 0;
while (true) {
// Basic synchronization by semaphore
sem->p();
kout.setpos(55, this->id);
kout << this->id << ": " << dec << cnt++ << endl;
sem->v();
}
}
void PreemptiveThreadDemo::run() {
kout << "PreemptiveThreadDemo ====================================" << endl;
kout << "Initializing Semaphore" << endl;
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
// so stack allocated objects will be lost
Semaphore* sem = new Semaphore(1); // Create this semaphore on the heap as this thread exits itself,
// so stack allocated objects will be lost
kout << "Allocating LoopThread A" << endl;
Thread* cntA = new PreemptiveLoopThread(0, sem);

View File

@ -1,12 +1,11 @@
#ifndef __pre_loopthread_include__
#define __pre_loopthread_include__
#ifndef __preemptive_thread_include__
#define __preemptive_thread_include__
#include "kernel/Globals.h"
#include "kernel/threads/Thread.h"
#include "lib/Semaphore.h"
class PreemptiveLoopThread : public Thread {
private:
int id;
Semaphore* sem;
@ -21,4 +20,17 @@ public:
void run() override;
};
class PreemptiveThreadDemo : public Thread {
private:
PreemptiveThreadDemo(const PreemptiveThreadDemo& copy) = delete; // Verhindere Kopieren
public:
PreemptiveThreadDemo() {
kout << "Initialized PreemptiveThreadDemo" << endl;
}
// Thread-Startmethode
void run() override;
};
#endif