Skip to content

Instantly share code, notes, and snippets.

@dillstead
dillstead / faneuil_hall_e.txt
Created September 18, 2025 18:39
Little Book of Semaphores Faneuil Hall Extended Solution
Extended Faneuil Hall Problem
judge = 0
entered = 0
left = 0
checked = 0
nojudge = Sem(1)
allsignedin = Sem(0)
confirmed = Sem(0)
alldone = Sem(1)
@dillstead
dillstead / faneuil_hall.txt
Created September 18, 2025 15:24
Little Book of Semaphores Faneuil Hall Solution
Faneuil Hall Problem
entered = 0
nojudge = Sem(1)
confirmed = Sem(0)
checkedin = Sem(0)
Immigrant thread
nojudge.wait()
@dillstead
dillstead / senate_bus.txt
Created September 16, 2025 15:23
Little Book of Semaphores Senate Bus Solution
Senate Bus Problem
riders = 0
mutex = Sem(1)
multiplex = Sem(50)
bus = Sem(0)
allAboard = Sem(0)
Bus thread
@dillstead
dillstead / room_party.txt
Last active September 16, 2025 19:50
Little Book of Semaphores Room Party Solution
Room Party Problem
students = 0
dean = 'not here'
mutex = Sem(1)
turn = Sem(1)
wait = Sem(0)
Dean thread
@dillstead
dillstead / unaligned_access.c
Created May 16, 2024 20:00
Unaligned access after arena allocation
// gcc -Werror -std=c11 -Wall -Wextra -Wno-error=unused-parameter -Wno-error=unused-function -Wno-error=unused-variable -Wconversion -Wno-error=sign-conversion -fsanitize=address,undefined -g3 -o unaligned_access unaligned_access.c
// Expected runtime error:
// ~/projects/tmp $ ./unaligned_access
// unaligned_access.c:72:20: runtime error: member access within misaligned address 0x6210000010c7 for type 'struct io_data', which requires 8 byte alignment
#include <stdlib.h>
#include <sys/uio.h>
#include <stddef.h>
#include <stdint.h>
@dillstead
dillstead / read_file.c
Last active May 16, 2024 18:25
Reading a large (>= 2 GB file) using 32-bit sizes
// gcc -Werror -Wall -Wextra -D_FILE_OFFSET_BITS=64 -O2 -o read_file read_file.c
#include <stdlib.h>
#include <stddef.h>
#include <sys/stat.h>
#include <stdint.h>
int main(void)
{
off_t file_sz = 1LL << 32;
ptrdiff_t block_sz = 1 << 27;
@dillstead
dillstead / piblink.S
Created February 8, 2024 20:50
Blinking the LED in assembly on a Raspberry Pi + Pi 2
@dillstead
dillstead / rainbow_arm.c
Last active November 2, 2023 15:56
ARM Hash Function Collision Detection
// Find 64-bit hash collisions using a rainbow table
//
// Prints string collision pairs, one per line, until running out of
// memory or manually stopped.
//
// Porting: Implement fullwrite() and an entry point that calls worker()
// on one thread per CPU core.
//
// This is free and unencumbered software released into the public domain.
@dillstead
dillstead / arm_clone.c
Last active October 30, 2023 17:57
ARM Raw Threading
// Refer to: https://nullprogram.com/blog/2015/05/15/
// https://nullprogram.com/blog/2023/02/15/
// https://nullprogram.com/blog/2023/03/23/
// gcc -Werror -std=gnu99 -Wall -Wextra -Wno-error=unused-parameter -Wno-error=unused-function -Wno-error=unused-variable -Wconversion -Wno-error=sign-conversion -fno-builtin -nostdlib -march=armv7-a -O2 -o clone_arm clone_arm.c
#include <unistd.h>
#include <stddef.h>
#include <syscall.h>
#define assert(c) while (!(c)) __builtin_trap()
#define sizeof(x) (ptrdiff_t) sizeof(x)
@dillstead
dillstead / syscall_arm.c
Created October 24, 2023 19:40
ARM Raw System Call
// Refer to: https://nullprogram.com/blog/2023/03/23/
// gcc -Werror -std=gnu99 -Wall -Wextra -Wno-error=unused-parameter -Wno-error=unused-function -Wno-error=unused-variable -Wconversion -Wno-error=sign-conversion -g3 -o syscall_arm syscall_arm.c
#include <stdlib.h>
#include <stdbool.h>
#include <syscall.h>
#define countof(a) (sizeof(a) / sizeof(*(a)))
#define lengthof(s) (countof(s) - 1)
#define SYSCALL1(n, a) \