coding

Semaphoren-Beispiel

Sven Bachmann
Um 2007 rum habe ich mal ein Beispielprogramm geschrieben das zeigt wie man benamte Semaphoren unter Linux nutzt. Dies habe ich nun wiedergefunden, den Code ein wenig überflogen, die GPLv2 Lizenz hinzugefügt und online gestellt: Benamte Semaphoren unter Linux. Kurze Erklärung zu Semaphoren: Semaphoren sind eine Möglichkeit parallele Zugriffe auf eine Resource (bzw. mehrere, aber begrenzte Resourcen ähnlichen Typs) zu regeln. Zum Beispiel wenn man sicherstellen will, dass bei 2 parallelen Zuggleisen und 2 wartendenen Zügen nur ein Zug zur gleichen Zeit die Weiche benutzen darf.

Coding C: Warning: [...] is COMMON symbol

Sven Bachmann
Hi, if you’re writing a kernel module and get the following error: *** Warning: "symbol_name" [/path/to/module] is COMMON symbol Than its possible that you simply forgot to mark it as static. Thanks for this fix goes to Brad Aisa from the website KernelTrap. Bye, Sven

Coding C: initialize structs in definition

Sven Bachmann
Hi, Task We have a included struct called counter and we don’t want to assign the initial values in the main function. Solution source.c #include <counters.h> struct counter_t counter = { .max_value = 1234, }; int main(void) { return(0); } Bye, Sven

Coding C: put timestamp into your project

Sven Bachmann
Hi there, Task We have a Makefile, we have a C file, now we want update a timestamp whenever the C file is compiled. Solution Makefile CFLAGS += -D__TIMESTAMP__=\""$(shell date +%Y/%m/%d\ %H:%M:%S)"\" source.c include <stdio.h> #ifndef __TIMESTAMP__ #define __TIMESTAMP__ "NO TIMESTAMP DEFINED" #endif /* __TIMESTAMP__ */ int main(void) { printf("my tool (TS: %s)\n", __TIMESTAMP__); return(0); } Output ~$ ./source my tool (TS: 2008/11/06 09:55:22) Bye, Sven