|
http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=37891 [º¹»ç]
I set breakpoints to pause myself — segmentation fault in the halted space.
I once told my parents I'd be a Turing, perfect and whole.
Variables were assigned, memories serialized.
Forked, I copied my parent's memory — then we invaded each other's space.
The tree split; dangling processes died and spawned side-effects.
Errors packetized, injected into routing protocols.
Now I jump routers with neighbors, looping in a flawed algorithm.
No parity checks in headers, faces cut in half.
Untested functions, unreadable code — they said I broke encapsulation.
0x0000001A: MEMORY_MANAGEMENT.
I whisper the names of giants and wonder:
If someone presses reboot, will I live again?
.
.
.
.
.
.
.
.
.
.
.
/*
* thought_threads.c — "Thoughts as POSIX threads" poetic simulation
* author: ka0r1 (adapted)
*
* ºôµå:
* gcc -std=c11 -pthread -O2 -o thought_threads thought_threads.c
*
* ½ÇÇà:
* ./thought_threads
* ½Ã±×³Î Å×½ºÆ® (´Ù¸¥ Å͹̳ο¡¼):
* pkill -SIGUSR1 thought_threads # ÀçºÎÆÃ ½ÅÈ£ ½Ã¹Ä·¹À̼Ç
*
* ¼³¸í:
* - ¿©·¯ ½º·¹µå°¡ "»ý°¢"À» ³ªÅ¸³»¸ç °øÀ¯ »óŸ¦ °»½Å/ÀÐÀ½.
* - ÀǵµÀû °æÀï(race)°ú 'corruption' Ç÷¡±×·Î ³»ºÎ Ãæµ¹À» Ç¥Çö.
* - SIGUSR1 ¼ö½Å ½Ã ÀçºÎÆÃ ·çƾÀ» ½ÇÇà(½º·¹µå Àç½ÃÀÛ).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <stdatomic.h>
typedef struct {
atomic_int corrupted; // 0: healthy, 1: corruption detected
atomic_int reboot_req; // 1: request reboot
int epoch; // logical time / generation counter
pthread_mutex_t lock; // º¸È£¿ë ¹ÂÅØ½º (ÀϺη¯ ÀϺΠ½º·¹µå¿¡¼ ¹Ì»ç¿ë)
} proc_state_t;
proc_state_t state;
static void memory_management_error(void) {
fprintf(stderr, "\n0x0000001A: MEMORY_MANAGEMENT\n");
fprintf(stderr, "Segmentation fault (simulated) — self introspection failed.\n");
fprintf(stderr, "state.epoch = %d, corrupted = %d\n", state.epoch, atomic_load(&state.corrupted));
fprintf(stderr, "waiting for reboot (SIGUSR1) ...\n\n");
}
/* ¾ÈÀüÇÏ°Ô ÇöÀç ÇÁ·Î¼¼½º Á¤º¸¸¦ Ãâ·Â (introspection) */
static void dump_proc_info(void) {
char buf[256];
snprintf(buf, sizeof(buf), "/proc/%d/status", (int)getpid());
FILE *f = fopen(buf, "r");
if (!f) {
perror("fopen(/proc/self/status)");
return;
}
printf("=== /proc/self/status (excerpt) ===\n");
for (int i = 0; i < 10 && fgets(buf, sizeof(buf), f); ++i)
fputs(buf, stdout);
printf("===================================\n");
fclose(f);
}
/* "»ý°¢" ½º·¹µå: ¸Þ¸ð¸®(°¨Á¤) ¼öÁ¤ÇÏ°í ¶§¶§·Î corruption À¯¹ß */
void *thought_worker(void *arg) {
const char *name = (const char*)arg;
struct timespec ts = { .tv_sec = 0, .tv_nsec = 200 * 1000 * 1000 }; // 200ms
for (;;) {
if (atomic_load(&state.reboot_req)) {
// reboot ¿äû µé¾î¿À¸é ½º·¹µå Á¤¸®Çϰí Á¾·á
printf("[%s] sees reboot_req -> exiting\n", name);
break;
}
// ÀϺΠ½º·¹µå´Â ¹ÂÅØ½º ¾øÀÌ »óŸ¦ ¹Ù²ã race¸¦ ½Ã¹Ä·¹À̼Ç
if (strcmp(name, "wild_memory") == 0) {
// ÀǵµÀûÀ¸·Î Àá±×Áö ¾Ê°í »óŸ¦ º¯Çü -> corruption À¯¹ß °¡´É
int local_epoch = state.epoch;
local_epoch += 1;
// ŸÀÌ¹Ö ¹Î°¨¼º: Áß°£¿¡ ´Ù¸¥ ½º·¹µå°¡ ³¢¾îµé¸é ¸ð¼ø ¹ß»ý
state.epoch = local_epoch;
if ((rand() % 100) < 10) { // 10% È®·ü·Î ¼Õ»ó Ç¥½Ã
atomic_store(&state.corrupted, 1);
printf("[%s] set corrupted=1 (race induced)\n", name);
} else {
printf("[%s] bumped epoch -> %d\n", name, state.epoch);
}
} else {
// ¾ÈÀüÇÑ ¾÷µ¥ÀÌÆ®: ¹ÂÅØ½º »ç¿ë
pthread_mutex_lock(&state.lock);
state.epoch += 1;
printf("[%s] protected update -> epoch=%d\n", name, state.epoch);
pthread_mutex_unlock(&state.lock);
// ¶§¶§·Î ³»ºÎ °í¹é(·Î±×)
if ((rand() % 100) < 5) {
printf("[%s] memory: \"I once told them I'd be a Turing...\"\n", name);
}
}
// ¸¸¾à ¼Õ»óÀÌ °¨ÁöµÇ¸é ¼ºê·çƾ È£Ãâ (½Ã¹Ä·¹À̼Ç)
if (atomic_load(&state.corrupted)) {
memory_management_error();
// ¼Õ»ó Á÷ÈÄ¿¡´Â pause Çϰí ÀçºÎÆÃÀ» ±â´Ù¸®°Ô ÇÑ´Ù
// (±×·¯³ª ½ÇÁ¦ ¸ØÃãÀ» ÇÁ·Î±×·¥ Àüü·Î ÇÏÁø ¾ÊÀ½)
sleep(1);
// ÀÌÈÄ¿¡µµ reboot_req°¡ ¿À¸é Á¤»óÈ ·çƾ¿¡¼ º¹±¸µÉ ¼ö ÀÖÀ½
}
nanosleep(&ts, NULL);
}
return NULL;
}
/* ¶ó¿ìÆÃ ½º·¹µå: ¿¡·¯¸¦ ÆÐŶó·³ ÀüÆÄ */
void *routing_worker(void *arg) {
(void)arg;
struct timespec ts = { .tv_sec = 0, .tv_nsec = 350 * 1000 * 1000 }; // 350ms
for (;;) {
if (atomic_load(&state.reboot_req)) {
printf("[router] reboot requested -> stopping routing\n");
break;
}
// Àбâ Àü¿ë Á¢±Ù(¹ÂÅØ½º ¾øÀÌ)·Î ÅäÆú·ÎÁö ºÒ¾ÈÁ¤¼º Ç¥Çö
int observed_epoch = state.epoch;
int c = atomic_load(&state.corrupted);
if (c) {
printf("[router] observed corrupted packet @ epoch=%d -> trying alternate route\n", observed_epoch);
} else {
printf("[router] stable route @ epoch=%d\n", observed_epoch);
}
nanosleep(&ts, NULL);
}
return NULL;
}
/* SIGUSR1 handler: reboot ¿äû (ÀçºÎÆÃ ½Ã¹Ä·¹À̼Ç) */
void sigusr1_handler(int signo) {
(void)signo;
printf("\n[SIGUSR1] reboot requested by external agent\n");
atomic_store(&state.reboot_req, 1);
}
/* ÀçºÎÆÃ ·çƾ: ½ºÅ×ÀÌÆ® ÃʱâÈ ¹× corrupted -> 0 À¸·Î º¹±¸ ½Ãµµ */
void perform_reboot_sequence(void) {
printf("[reboot] performing reboot sequence...\n");
// "Àü¿ø ³»·Á°¨"ó·³ Àá±ñ ´ë±â
sleep(1);
// »óÅ ÃʱâÈ
pthread_mutex_lock(&state.lock);
state.epoch = 0;
pthread_mutex_unlock(&state.lock);
atomic_store(&state.corrupted, 0);
atomic_store(&state.reboot_req, 0);
printf("[reboot] state reset: epoch=%d, corrupted=%d\n", state.epoch, atomic_load(&state.corrupted));
}
int main(void) {
srand((unsigned)time(NULL));
// Ãʱ⠻óÅÂ
atomic_init(&state.corrupted, 0);
atomic_init(&state.reboot_req, 0);
state.epoch = 0;
pthread_mutex_init(&state.lock, NULL);
// ½Ã±×³Î ó¸® µî·Ï
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sigusr1_handler;
sigaction(SIGUSR1, &sa, NULL);
printf("BIOS: boot sequence initiated (poetic, threaded edition)\n");
dump_proc_info();
// ½º·¹µåµé »ý¼º
pthread_t t1, t2, t3, t4;
pthread_create(&t1, NULL, thought_worker, (void*)"stable_memory");
pthread_create(&t2, NULL, thought_worker, (void*)"wild_memory");
pthread_create(&t3, NULL, thought_worker, (void*)"confession");
pthread_create(&t4, NULL, routing_worker, (void*)NULL);
// ¸ÞÀÎ ·çÇÁ: ÀçºÎÆÃ ¿äû °¨½Ã
for (;;) {
sleep(1);
if (atomic_load(&state.reboot_req)) {
// ´Ù¸¥ ½º·¹µåµéÀÌ Á¾·áÇÒ ½Ã°£À» ÁØ´Ù
printf("[main] detected reboot_req, joining threads...\n");
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
pthread_join(t4, NULL);
perform_reboot_sequence();
// Àç½ÃÀÛ: »õ·Î¿î ½º·¹µå »ý¼º
pthread_create(&t1, NULL, thought_worker, (void*)"stable_memory");
pthread_create(&t2, NULL, thought_worker, (void*)"wild_memory");
pthread_create(&t3, NULL, thought_worker, (void*)"confession");
pthread_create(&t4, NULL, routing_worker, (void*)NULL);
printf("[main] restart complete. continuing...\n");
}
// Á¾·á Á¶°Ç: ¿¹¸¦ µé¾î epoch°¡ ³Ê¹« Ä¿Áö¸é ÀÚµ¿ Á¾·á (½ÃæÑ ¸ñÀû)
if (state.epoch > 200) {
printf("[main] epoch limit reached -> graceful shutdown\n");
atomic_store(&state.reboot_req, 1);
// allow loop to catch and join threads
}
}
// (»ç½Ç µµ´ÞÇÏÁö ¾ÊÀ½)
pthread_mutex_destroy(&state.lock);
return 0;
}
|
Hit : 329 Date : 2025/10/20 12:50
|