forked from open-education-hub/operating-systems
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chapter/compute: Restructure compute chapter
Signed-off-by: Andrei Miga <[email protected]>
- Loading branch information
1 parent
eb0056b
commit eb27e7b
Showing
433 changed files
with
12,102 additions
and
933 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
TARGETS = round-robin race-condition race-condition-toctou race-condition-lock | ||
RVMD = reveal-md | ||
MDPP = markdown-pp | ||
FFMPEG = ffmpeg | ||
|
||
SLIDES ?= slides.mdpp | ||
SLIDES_OUT ?= slides.md | ||
MEDIA_DIR ?= media | ||
SITE ?= _site | ||
OPEN ?= xdg-open | ||
|
||
.PHONY: all html clean videos | ||
|
||
all: videos html | ||
|
||
html: $(SITE) | ||
|
||
$(SITE): $(SLIDES) | ||
$(MDPP) $< -o $(SLIDES_OUT) | ||
$(RVMD) $(SLIDES_OUT) --static $@ | ||
|
||
videos: | ||
for TARGET in $(TARGETS); do \ | ||
$(FFMPEG) -framerate 0.5 -f image2 -y \ | ||
-i "$(MEDIA_DIR)/$$TARGET/$$TARGET-%d.svg" -vf format=yuv420p $(MEDIA_DIR)/$$TARGET-generated.gif; \ | ||
done | ||
|
||
open: $(SITE) | ||
$(OPEN) $</index.html | ||
|
||
clean: | ||
-rm -f $(MEDIA_DIR)/*-generated.gif | ||
-rm -f *~ | ||
-rm -fr $(SITE) $(SLIDES_OUT) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
50 changes: 50 additions & 0 deletions
50
chapters/compute/arena/drills/tasks/CLIST/support/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
TARGET = test | ||
TARGET_PARALLEL = test_parallel | ||
|
||
all: $(TARGET) $(TARGET_PARALLEL) | ||
|
||
CC = gcc | ||
|
||
# Get the relative path to the directory of the current makefile. | ||
MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST))) | ||
INCLUDES_DIR := $(MAKEFILE_DIR).. | ||
UTILS_DIR := $(MAKEFILE_DIR)/utils | ||
LOGGER_DIR := $(UTILS_DIR)/log | ||
|
||
CPPFLAGS += -I$(INCLUDES_DIR) | ||
CFLAGS += -g -Wall -Wextra | ||
LDFLAGS += -z lazy | ||
LOGGER_OBJ = log.o | ||
LOGGER = $(LOGGER_DIR)/$(LOGGER_OBJ) | ||
|
||
SRCS = $(wildcard *.c) | ||
OBJS = $(SRCS:.c=.o) | ||
|
||
$(LOGGER_OBJ): $(LOGGER_DIR)/log.c | ||
@make -C $(LOGGER_DIR) $(LOGGER_OBJ) | ||
|
||
$(OBJS): %.o: %.c | ||
|
||
clean:: | ||
-rm -f $(OBJS) $(LOGGER) | ||
|
||
.PHONY: clean | ||
|
||
LIB_OBJECTS = clist.o | ||
LIBRARY = libclist.a | ||
TEST_OBJECTS = test.o $(LIBRARY) | ||
TEST_OBJECTS_PARALLEL = test_parallel.o $(LIBRARY) | ||
|
||
LDLIBS = -lpthread | ||
|
||
$(LIBRARY): $(LIB_OBJECTS) | ||
ar rcs $(LIBRARY) $(LIB_OBJECTS) | ||
|
||
$(TARGET): $(TEST_OBJECTS) | ||
$(CC) $^ -o $@ $(LDLIBS) | ||
|
||
$(TARGET_PARALLEL): $(TEST_OBJECTS_PARALLEL) | ||
$(CC) $^ -o $@ $(LDLIBS) | ||
|
||
clean:: | ||
-rm -f $(LIBRARY) $(TARGET) $(TARGET_PARALLEL) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
chapters/compute/arena/drills/tasks/page-faults/support/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
BINARIES = page_faults file.txt | ||
|
||
all: $(BINARIES) | ||
|
||
CC = gcc | ||
|
||
# Get the relative path to the directory of the current makefile. | ||
MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST))) | ||
INCLUDES_DIR := $(MAKEFILE_DIR).. | ||
UTILS_DIR := $(MAKEFILE_DIR)/utils | ||
LOGGER_DIR := $(UTILS_DIR)/log | ||
|
||
CPPFLAGS += -I$(INCLUDES_DIR) | ||
CFLAGS += -g -Wall -Wextra | ||
LDFLAGS += -z lazy | ||
LOGGER_OBJ = log.o | ||
LOGGER = $(LOGGER_DIR)/$(LOGGER_OBJ) | ||
|
||
SRCS = $(wildcard *.c) | ||
OBJS = $(SRCS:.c=.o) | ||
|
||
$(LOGGER_OBJ): $(LOGGER_DIR)/log.c | ||
@make -C $(LOGGER_DIR) $(LOGGER_OBJ) | ||
|
||
$(OBJS): %.o: %.c | ||
|
||
clean:: | ||
-rm -f $(OBJS) $(LOGGER) | ||
|
||
.PHONY: clean | ||
|
||
$(BINARIES): $(LOGGER) | ||
|
||
clean:: | ||
-rm -f $(BINARIES) | ||
|
||
.PHONY: all clean | ||
|
||
file.txt: | ||
curl metaphorpsum.com/paragraphs/20/50 > $@ |
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
chapters/compute/arena/drills/tasks/shared-memory/solution/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../support/shared-memory/Makefile |
79 changes: 79 additions & 0 deletions
79
chapters/compute/arena/drills/tasks/shared-memory/solution/shared_memory.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
Check failure on line 1 in chapters/compute/arena/drills/tasks/shared-memory/solution/shared_memory.c GitHub Actions / Checkpatch
|
||
|
||
#include <stdio.h> | ||
#include <sys/mman.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include <sys/wait.h> | ||
#include <semaphore.h> | ||
|
||
#include "utils/utils.h" | ||
|
||
int main(void) | ||
{ | ||
pid_t ret_pid; | ||
pid_t pid; | ||
int *p; | ||
int rc; | ||
sem_t *sem; | ||
|
||
/* TODO 1: Change the flags to disable copy-on-write. */ | ||
p = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, | ||
MAP_SHARED | MAP_ANONYMOUS, -1, 0); | ||
DIE(p == MAP_FAILED, "mmap"); | ||
|
||
*p = 42; | ||
|
||
/** | ||
* TODO 2.1: Create a semaphore in the shared page and use it to signal | ||
* the child process to end. | ||
*/ | ||
sem = (sem_t *)(p + 10); | ||
rc = sem_init(sem, 1, 0); | ||
DIE(rc < 0, "sem_init"); | ||
|
||
pid = fork(); | ||
switch (pid) { | ||
case -1: | ||
/* `fork()` has encountered an error. */ | ||
DIE(1, "fork"); | ||
break; | ||
|
||
case 0: | ||
/* Child process */ | ||
*p = 69; | ||
printf("[child] Wrote value %d at address %p\n", *p, p); | ||
|
||
/** | ||
* TODO 2.2: Wait for the semaphore to be signalled by the | ||
* parent. | ||
*/ | ||
rc = sem_wait(sem); | ||
DIE(rc < 0, "sem_wait"); | ||
|
||
break; | ||
|
||
default: | ||
/* Parent process */ | ||
/** | ||
* TODO 2.3: Sleep for a few seconds before signalling the child | ||
* process to end. | ||
*/ | ||
sleep(2); | ||
rc = sem_post(sem); | ||
DIE(rc < 0, "sem_post"); | ||
|
||
ret_pid = waitpid(pid, NULL, 0); | ||
DIE(ret_pid < 0, "waitpid parent"); | ||
|
||
/** | ||
* TODO 2.4: Make sure the value read by the parent is still the | ||
* one written by the child. | ||
*/ | ||
printf("[parent] Child process exited. Data at address %p = %d\n", | ||
p, *p); | ||
break; | ||
} | ||
|
||
return 0; | ||
} |
1 change: 1 addition & 0 deletions
1
chapters/compute/arena/drills/tasks/shared-memory/support/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
shared_memory |
39 changes: 39 additions & 0 deletions
39
chapters/compute/arena/drills/tasks/shared-memory/support/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
LDLIBS = -lpthread | ||
BINARY = shared_memory | ||
|
||
all: $(BINARY) | ||
|
||
CC = gcc | ||
|
||
# Get the relative path to the directory of the current makefile. | ||
MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST))) | ||
INCLUDES_DIR := $(MAKEFILE_DIR).. | ||
UTILS_DIR := $(MAKEFILE_DIR)/utils | ||
LOGGER_DIR := $(UTILS_DIR)/log | ||
|
||
CPPFLAGS += -I$(INCLUDES_DIR) | ||
CFLAGS += -g -Wall -Wextra | ||
LDFLAGS += -z lazy | ||
LOGGER_OBJ = log.o | ||
LOGGER = $(LOGGER_DIR)/$(LOGGER_OBJ) | ||
|
||
SRCS = $(wildcard *.c) | ||
OBJS = $(SRCS:.c=.o) | ||
|
||
$(LOGGER_OBJ): $(LOGGER_DIR)/log.c | ||
@make -C $(LOGGER_DIR) $(LOGGER_OBJ) | ||
|
||
$(OBJS): %.o: %.c | ||
|
||
clean:: | ||
-rm -f $(OBJS) $(LOGGER) | ||
|
||
.PHONY: clean | ||
|
||
$(BINARY): $(OBJS) $(LOGGER) | ||
$(CC) $^ $(LDFLAGS) -o $@ $(LDLIBS) | ||
|
||
clean:: | ||
-rm -f $(BINARIES) | ||
|
||
.PHONY: all clean |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../support/sleepy/Makefile |
12 changes: 12 additions & 0 deletions
12
chapters/compute/arena/drills/tasks/sleepy/solution/sleepy_creator.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause */ | ||
Check failure on line 1 in chapters/compute/arena/drills/tasks/sleepy/solution/sleepy_creator.c GitHub Actions / Checkpatch
|
||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main(void) | ||
{ | ||
/* TODO 1: Create one `sleep 1000` process using `system`. */ | ||
system("sleep 1000"); | ||
puts("Parent process ending."); | ||
return 0; | ||
} |
24 changes: 24 additions & 0 deletions
24
chapters/compute/arena/drills/tasks/sleepy/solution/sleepy_creator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import subprocess | ||
from sys import exit | ||
|
||
NUM_SLEEPS = 10 | ||
|
||
|
||
def main(): | ||
# TODO 1: Create 10 `sleep 1000` processes using `subprocess.Popen` | ||
# Use the documentation: https://docs.python.org/3/library/subprocess.html#subprocess.Popen | ||
procs = [] | ||
for _ in range(NUM_SLEEPS): | ||
# Create new process and add it to the list of processes. | ||
p = subprocess.Popen(["sleep", "1000"]) | ||
procs.append(p) | ||
|
||
# TODO 2: Make the current process wait for its child processes. | ||
for p in procs: | ||
p.wait() | ||
|
||
|
||
if __name__ == "__main__": | ||
exit(main()) |
2 changes: 2 additions & 0 deletions
2
chapters/compute/arena/drills/tasks/sleepy/support/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
sleepy_creator | ||
syscalls.* |
41 changes: 41 additions & 0 deletions
41
chapters/compute/arena/drills/tasks/sleepy/support/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
BINARY = sleepy_creator | ||
|
||
all: $(BINARY) | ||
|
||
CC = gcc | ||
|
||
# Get the relative path to the directory of the current makefile. | ||
MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST))) | ||
INCLUDES_DIR := $(MAKEFILE_DIR).. | ||
UTILS_DIR := $(MAKEFILE_DIR)/utils | ||
LOGGER_DIR := $(UTILS_DIR)/log | ||
|
||
CPPFLAGS += -I$(INCLUDES_DIR) | ||
CFLAGS += -g -Wall -Wextra | ||
LDFLAGS += -z lazy | ||
LOGGER_OBJ = log.o | ||
LOGGER = $(LOGGER_DIR)/$(LOGGER_OBJ) | ||
|
||
SRCS = $(wildcard *.c) | ||
OBJS = $(SRCS:.c=.o) | ||
|
||
$(LOGGER_OBJ): $(LOGGER_DIR)/log.c | ||
@make -C $(LOGGER_DIR) $(LOGGER_OBJ) | ||
|
||
$(OBJS): %.o: %.c | ||
|
||
clean:: | ||
-rm -f $(OBJS) $(LOGGER) | ||
|
||
.PHONY: clean | ||
|
||
$(BINARY): $(OBJS) $(LOGGER) | ||
$(CC) $^ $(LDFLAGS) -o $@ $(LDLIBS) | ||
|
||
clean:: | ||
-rm -f $(BINARIES) | ||
|
||
.PHONY: all clean | ||
|
||
clean:: | ||
-rm -f syscalls* |
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions
55
chapters/compute/arena/drills/tasks/sum-array/solution/python/sum_array_processes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
from math import ceil | ||
from multiprocessing import Manager, Process | ||
from random import randint | ||
from sys import argv | ||
from time import time_ns | ||
|
||
ARR_LEN = 10_000_000 | ||
|
||
|
||
def generate_random_array(length=ARR_LEN): | ||
return [randint(1, 100) for _ in range(length)] | ||
|
||
|
||
def calculate_array_part_sum(arr, start, end, id, sums): | ||
sum_arr = 0 | ||
for i in range(start, end): | ||
sum_arr += arr[i] | ||
sums[id] = sum_arr | ||
|
||
|
||
def main(): | ||
if len(argv) < 2: | ||
print(f"Usage: {argv[0]} <num_processes>") | ||
return 1 | ||
|
||
num_procs = int(argv[1]) | ||
arr = generate_random_array() | ||
sum_arr = 0 | ||
procs = [None] * num_procs | ||
sums = Manager().dict() | ||
|
||
time_start = time_ns() | ||
|
||
for i in range(num_procs): | ||
start = i * ceil(ARR_LEN / num_procs) | ||
end = min(ARR_LEN, (i + 1) * ceil(ARR_LEN / num_procs)) | ||
procs[i] = Process( | ||
target=calculate_array_part_sum, args=(arr, start, end, i, sums) | ||
) | ||
procs[i].start() | ||
|
||
for i in range(num_procs): | ||
procs[i].join() | ||
sum_arr += sums[i] | ||
|
||
time_end = time_ns() | ||
time_spent = (time_end - time_start) / 1000_000 | ||
|
||
print(f"Array sum is: {sum_arr}; time spent: {time_spent}ms") | ||
|
||
|
||
if __name__ == "__main__": | ||
exit(main()) |
Oops, something went wrong.