From 7073d04f313625b583a9631a9dba06e3aa3353f7 Mon Sep 17 00:00:00 2001
From: Peter Gerwinski <peter.gerwinski@hs-bochum.de>
Date: Thu, 2 Dec 2021 16:17:54 +0100
Subject: [PATCH] Beispiele 2.12.2021

---
 20211202/linux-scheduler-0.c | 48 ++++++++++++++++++++++++++++++++++++
 20211202/linux-scheduler-1.c | 40 ++++++++++++++++++++++++++++++
 20211202/linux-scheduler-2.c | 42 +++++++++++++++++++++++++++++++
 20211202/task-modes.h        |  5 ++++
 4 files changed, 135 insertions(+)
 create mode 100644 20211202/linux-scheduler-0.c
 create mode 100644 20211202/linux-scheduler-1.c
 create mode 100644 20211202/linux-scheduler-2.c
 create mode 100644 20211202/task-modes.h

diff --git a/20211202/linux-scheduler-0.c b/20211202/linux-scheduler-0.c
new file mode 100644
index 0000000..e97d32b
--- /dev/null
+++ b/20211202/linux-scheduler-0.c
@@ -0,0 +1,48 @@
+/*
+ *  'schedule()' is the scheduler function. This is GOOD CODE! There
+ * probably won't be any reason to change this, as it should work well
+ * in all circumstances (ie gives IO-bound processes good response etc).
+ * The one thing you might take a look at is the signal-handler code here.
+ *
+ *   NOTE!!  Task 0 is the 'idle' task, which gets called when no other
+ * tasks can run. It can not be killed, and it cannot sleep. The 'state'
+ * information in task[0] is never used.
+ */
+void schedule(void)
+{
+	int i,next,c;
+	struct task_struct ** p;
+
+/* check alarm, wake up any interruptible tasks that have got a signal */
+
+	for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
+		if (*p) {
+			if ((*p)->alarm && (*p)->alarm < jiffies) {
+					(*p)->signal |= (1<<(SIGALRM-1));
+					(*p)->alarm = 0;
+				}
+			if ((*p)->signal && (*p)->state==TASK_INTERRUPTIBLE)
+				(*p)->state=TASK_RUNNING;
+		}
+
+/* this is the scheduler proper: */
+
+	while (1) {
+		c = -1;
+		next = 0;
+		i = NR_TASKS;
+		p = &task[NR_TASKS];
+		while (--i) {
+			if (!*--p)
+				continue;
+			if ((*p)->state == TASK_RUNNING && (*p)->counter > c)
+				c = (*p)->counter, next = i;
+		}
+		if (c) break;
+		for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
+			if (*p)
+				(*p)->counter = ((*p)->counter >> 1) +
+						(*p)->priority;
+	}
+	switch_to(next);
+}
diff --git a/20211202/linux-scheduler-1.c b/20211202/linux-scheduler-1.c
new file mode 100644
index 0000000..0b36564
--- /dev/null
+++ b/20211202/linux-scheduler-1.c
@@ -0,0 +1,40 @@
+void schedule(void)
+{
+	int i,next,c;
+	struct task_struct ** p;
+
+/* check alarm, wake up any interruptible tasks that have got a signal */
+
+	for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
+		if (*p) {
+			if ((*p)->alarm && (*p)->alarm < jiffies) {
+					(*p)->signal |= (1<<(SIGALRM-1));
+					(*p)->alarm = 0;
+				}
+			if ((*p)->signal && (*p)->state==TASK_INTERRUPTIBLE)
+				(*p)->state=TASK_RUNNING;
+		}
+
+/* this is the scheduler proper: */
+
+	while (1) {
+		c = -1;
+		next = 0;
+		i = NR_TASKS - 1;
+		p = &task[NR_TASKS - 1];
+		while (i) {
+			if (*p) {
+                                if ((*p)->state == TASK_RUNNING && (*p)->counter > c)
+			        	c = (*p)->counter, next = i;
+                        }
+                        i--;
+                        p--;
+		}
+		if (c) break;
+		for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
+			if (*p)
+				(*p)->counter = ((*p)->counter >> 1) +
+						(*p)->priority;
+	}
+	switch_to(next);
+}
diff --git a/20211202/linux-scheduler-2.c b/20211202/linux-scheduler-2.c
new file mode 100644
index 0000000..1ccce7f
--- /dev/null
+++ b/20211202/linux-scheduler-2.c
@@ -0,0 +1,42 @@
+void schedule(void)
+{
+	int i,next,c;
+	struct task_struct ** p;
+
+/* check alarm, wake up any interruptible tasks that have got a signal */
+
+	for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
+		if (*p) {
+			if ((*p)->alarm && (*p)->alarm < jiffies) {
+					(*p)->signal |= (1<<(SIGALRM-1));
+					(*p)->alarm = 0;
+				}
+			if ((*p)->signal && (*p)->state==TASK_INTERRUPTIBLE)
+				(*p)->state=TASK_RUNNING;
+		}
+
+/* this is the scheduler proper: */
+
+	while (1) {
+		c = -1;
+		next = 0;
+		i = NR_TASKS - 1;
+		p = &task[NR_TASKS - 1];
+		while (i) {
+			if (*p) {
+                                if ((*p)->state == TASK_RUNNING && (*p)->counter > c) {
+			        	c = (*p)->counter;
+                                        next = i;
+                                }
+                        }
+                        i--;
+                        p--;
+		}
+		if (c) break;
+		for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
+			if (*p)
+				(*p)->counter = ((*p)->counter >> 1) +
+						(*p)->priority;
+	}
+	switch_to(next);
+}
diff --git a/20211202/task-modes.h b/20211202/task-modes.h
new file mode 100644
index 0000000..47f5cba
--- /dev/null
+++ b/20211202/task-modes.h
@@ -0,0 +1,5 @@
+#define TASK_RUNNING		0
+#define TASK_INTERRUPTIBLE	1
+#define TASK_UNINTERRUPTIBLE	2
+#define TASK_ZOMBIE		3
+#define TASK_STOPPED		4
-- 
GitLab