I’ll be honest: this is one of the cooler things I’ve seen recently:
/* * Create separate threads for the main protagonists involved in the inception */ static void *inception(void *unused) { struct sched_param param = {.sched_priority = 99 }; int policy = SCHED_OTHER; if(!getuid() || !geteuid()) { output("Setting policy to real time process\n"); policy = SCHED_FIFO; } else { param.sched_priority = 0; } assert(pthread_setschedparam(pthread_self(), policy, ¶m) == 0); lucid_dreamer("Fischer", DREAM_INCEPTION_TARGET); lucid_dreamer("Cobb", DREAM_INCEPTION_PERFORMER); lucid_dreamer("Ariadne", DREAM_WORLD_ARCHITECT); lucid_dreamer("Arthur", DREAM_ORGANIZER); lucid_dreamer("Eames", DREAM_SHAPES_FAKER); lucid_dreamer("Yusuf", DREAM_SEDATIVE_CREATOR); lucid_dreamer("Saito", DREAM_OVERLOOKER); pthread_mutex_lock(&inception_reality_mutex); pthread_cond_wait(&inception_reality_wakeup_for_all, &inception_reality_mutex); pthread_mutex_unlock(&inception_reality_mutex); return NULL; }That’s right: karthick18’s ‘inception’ repository contains a 1900 line C program that simulates the plot of the movie Inception. How, you ask?
Have you heard of the Dining Philosopher’s problem? Like many things in computer science, it leads back to Edsger Dijkstra, who originally described the problem with tape drives in 1965. Here’s a summary:
Five philosophers sit around a table, with five bowls of rice in front of them. In between each bowl of rice is a chopstick. A philosopher can do two things: think and eat. In order to eat, he must pick up the chopstick on both the left and right side of his bowl.
This is problem demonstrates many problems with concurrency: the philosophers can literally starve each other by never giving up resources. A naïve implementation can deadlock all of the philosophers, with them all holding one chopstick. Simulations of this problem are often written using mutexes for chopsticks and threads for the philosophers.
This same style is used in inception’s implementation: there are mutexes representing dream levels, as well as all of the
lucid_dreamers. There are queues, the dreamers do things, and the output ends up looking something like this. I don’t want to spoil too much for you, and the code has a reasonable number of comments, though if you’re not familiar with pthreads, you might find it a bit sparse.Totally cool.
The code on GitHub.
-
pregnantbrutaluniforms liked this
-
bdsmsuperpornstars liked this
-
shortlysearc liked this
-
newspropriet liked this
-
reparatiifrigidere liked this
-
cloud62621 liked this
-
138151337 liked this
-
pariuri--sportive liked this
-
console-wii-u liked this
-
stanjenkins1128 liked this
-
entertech liked this
-
crazygeeksonline liked this
-
streamates liked this
-
texas-hold-em liked this
-
hardcoredrunkfeet liked this
-
last--minute liked this
-
cazucito reblogged this from thechangelog
-
svnlto reblogged this from thechangelog
-
soliman reblogged this from digitalyn
-
floatingegg reblogged this from thechangelog
-
amalfi-coast reblogged this from cab1729
-
cab1729 reblogged this from thechangelog
-
lostandstumbling reblogged this from thechangelog
-
734g33k reblogged this from thechangelog
-
astucioso reblogged this from thechangelog
-
purplehuman reblogged this from movielove
-
markador reblogged this from thechangelog
-
ilvillaggio reblogged this from thechangelog
