Skip to content

Commit

Permalink
chapter/compute: Restructure compute chapter
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Miga <[email protected]>
  • Loading branch information
AndreiMiga77 committed Sep 15, 2024
1 parent eb0056b commit eb27e7b
Show file tree
Hide file tree
Showing 433 changed files with 12,102 additions and 933 deletions.
34 changes: 34 additions & 0 deletions chapters/compute/Makefile
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)
50 changes: 50 additions & 0 deletions chapters/compute/arena/drills/tasks/CLIST/support/Makefile
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)

Check failure on line 41 in chapters/compute/arena/drills/tasks/CLIST/support/Makefile

View workflow job for this annotation

GitHub Actions / Checkpatch

ERROR:TRAILING_WHITESPACE: trailing whitespace

$(TARGET): $(TEST_OBJECTS)
$(CC) $^ -o $@ $(LDLIBS)

$(TARGET_PARALLEL): $(TEST_OBJECTS_PARALLEL)
$(CC) $^ -o $@ $(LDLIBS)

Check failure on line 48 in chapters/compute/arena/drills/tasks/CLIST/support/Makefile

View workflow job for this annotation

GitHub Actions / Checkpatch

ERROR:TRAILING_WHITESPACE: trailing whitespace
clean::
-rm -f $(LIBRARY) $(TARGET) $(TARGET_PARALLEL)
40 changes: 40 additions & 0 deletions chapters/compute/arena/drills/tasks/page-faults/support/Makefile
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 > $@
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

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:SPDX_LICENSE_TAG: Improper SPDX comment style for 'chapters/compute/arena/drills/tasks/shared-memory/solution/shared_memory.c', please use '//' instead

Check failure on line 1 in chapters/compute/arena/drills/tasks/shared-memory/solution/shared_memory.c

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1

#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

Check failure on line 48 in chapters/compute/arena/drills/tasks/shared-memory/solution/shared_memory.c

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:BLOCK_COMMENT_STYLE: Block comments should align the * on each line
* 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shared_memory
39 changes: 39 additions & 0 deletions chapters/compute/arena/drills/tasks/shared-memory/support/Makefile
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
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

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:SPDX_LICENSE_TAG: Improper SPDX comment style for 'chapters/compute/arena/drills/tasks/sleepy/solution/sleepy_creator.c', please use '//' instead

Check failure on line 1 in chapters/compute/arena/drills/tasks/sleepy/solution/sleepy_creator.c

View workflow job for this annotation

GitHub Actions / Checkpatch

WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1

#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;
}
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 chapters/compute/arena/drills/tasks/sleepy/support/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sleepy_creator
syscalls.*
41 changes: 41 additions & 0 deletions chapters/compute/arena/drills/tasks/sleepy/support/Makefile
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*
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())
Loading

0 comments on commit eb27e7b

Please sign in to comment.