Simple/canonical implementations of synchronization primitives? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for a good online reference on typical implementation of synchronization primitives (spinlocks, mutexes, semaphores, read-write locks, conditional variables, ...) either in abstract c+atomics or pseudo-asm (i.e. any reasonable notation of the sequence of atomic operations performed) or x86 asm. Something that starts with the most naive implementations and then addresses their shortcomings and some of the approaches to solving the shortcomings would be great.

Try Tanenbaum's Operating Systems: Design and Implementation.
edit: or Modern Operating Systems. I think the 1st one includes Minix, the 2nd one doesn't. Not sure, sorry =(
It's academically oriented, so it'll get you started on the right path.

Not a complete reference by any means, but the following paper is a classic and essential for understanding the implementation of synchronisation primitives in Linux:
"Fuss, Futexes and Furwocks: Fast Userlevel locking in Linux", Franke Russell & Kirkwod, Proceedings of the Ottawa Linux Symposium 2002 - available (among others) at: http://www.cis.temple.edu/~ingargio/cis307/readings/futex0.pdf
From that and the glibc sources it's possible to learn a lot, but it's not what I'd call easy-going :-)

As a side note, if you just want atomic stuff and you'r using gcc you have some built in functions that you can use instead of asm.
http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html
But for specific locks you always have Wikipedia
http://en.wikipedia.org/wiki/Spinlock
http://en.wikipedia.org/wiki/Semaphore_(programming)
Also worth looking at is
http://en.wikipedia.org/wiki/Lock-free_and_wait-free_algorithms

Related

Is it beneficial to use glibc's strlen()/strcmp() or roll your own based on SSE4.2? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
According to "Schema Validation with Intel® Streaming SIMD Extensions 4 (Intel® SSE4)" (Intel, 2008) [they] added instructions to assist in character searches and comparison on two operands of 16 bytes at a time. I wrote some basic strlen() and strcmp() functions in C, but they seem slower than glibc.
I would like to maybe experiment with using inline assembly to see how my project behaves with inputting/outputting XML.
I've read (on here) that using SMID on things like strlen() is rife with potential problems (memory alignment), so I'm a little concerned about using it in production code.
glibc's implementations will be hard to beat. These functions are carefully optimized and include pieces hand written in assembly. Here is glibc's x86_64 implementation of strcmp, using AVX2 instructions. Be warned, it is 800 lines:
https://github.com/lattera/glibc/blob/master/sysdeps/x86_64/multiarch/strcmp-avx2.S
For more detail, read also Peter Codes' fantastic explanation about glibc's implementation.

Good sources for reading Operating Systems [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I am looking for some good online sources which explain process sychronization,process and threads with practical examples.
i have gone through a lot of theoritical stuff but always face confusion and difficulties when actually trying implement them.
This is a link where you can find the code very useful for thread synchronization.
I started on Tanenbaum's (Wikipedia) "Modern Operating Systems" book then moved on to Linux From Scratch. For practical examples OSDev here and here is a mine of information when you become really hands on. Otherwise, for Windows specifics there are no shortage of books or MSDN literature.
I found Tanenbaum a very entertaining read, he invented the Minix OS which he uses for real world examples. Minix is from around the same era as Linux. Have a look at some of the very early Linux source code release (eg 0.1 or therabouts if you don't want to lose a year of your life studying pure code).
Wikipedia Is Always The Best Online Tutorial Guide. However u can also Check This Link.. If you would like to go with a Pdf Book then u can click Here.. Its really Good Book
this link for NACHOS may be a good help for you. In our undergraduate class we did some good assignments in OS course.

Existing threadpool C implementation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
What open-source implementation(s) in C for a pthreads thread pool would you recommend ?
Additional points if this implementation is :
Light-weight: glib, APR, NSPR and others come with a big buy-in, I'd rather have just 2 files (header and implementation).
Tested on several platforms (Linux, BSD, Mac OS X, etc.).
Still maintained.
I worked on making something I'd be able to use and I've published it on github: it's unimaginably called threadpool.
If your goal is light-weight, then the last thing you want is a prewritten, super-general-purpose, high-level-abstraction-based implementation. Implementing a thread pool yourself, suited to your particular task, is fairly trivial, but you might also question whether you actually need a thread pool or whether you'd be fine just creating and destroying threads as needed.
Without knowing more details about your application, I can't give much more specific advice. But the tools you might find useful are:
Condition variables
Semaphores
A job queue protected by a mutex
POSIX message queues
Here is an implementation with these features:
ANSI C and POSIX compliant
Minimal but powerful API
Synchronisation from the user
Full documentation
I once used this, which isn't actually an official implementation per se. It does use pthreads as you requested, and should give you some ideas of what you need to do. (See threadpool.h, threadpool.c, threadpool_test.c, and the Makefile for instructions on how to compile.) You'll obviously have to do some refactoring as it's original intention is probably different than yours. It's commented rather well actually.
Even though this deviates from the original question, I'd also like to mention that the newest C standard, unofficially C1X (see wikipedia, hyperlink limit), has planned support for threads N1570 (google it, hyperlink limit again!) (7.31.15).
Some personal advice from my experience would be to make sure that your application can actually be run in parallel, and if the overhead of creating a new thread is so high that you can't live without a thread pool. Personally I've blundered on both these parts and I've actually ended up with implementations slower than my single threaded application. Also, you might want to be aware of different problems, including cache-lockouts and misses, which would actually degrade the performance of your application.
I'm probably blabbering on by now, but best of luck.

Open-Source Trigonometric Equation Simplifiers (preferably C-based)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have done some searching around the internet trying to find some form of example code that will allow me to simplify trigonometric equations. In my mathematics course-work this year I am required to simplify trigonometric equations, and I intend on adapting the code to work with my TI-89 Titanium CAS, to use in replacement of the tCollect and tExpand functions (that can't seem to get the job done quite as nicely as I'd like it to).
As I can't really find all too much information on the matter I'd like to know if there is any code (preferably C-based) that will help me to simplify these kinds of equations. If not, what kind of algorithm would be best looking into to code myself?
Thanks.
A very powerful system that seems to have the functionality you need is Maxima, an open source computer algebra system. The following manpage describes trigexapand and trigreduce, which would seem to be roughly what you're looking for.
http://maxima.sourceforge.net/docs/manual/en/maxima_15.html
It's in Lisp rather than C, but consider that an adventure :)
Eigenmath is a handy little computer algebra system.
You might find something relevant in Eigenmath's source code.
The HP49/50 series calculators have a variety of trig manipulation functions that may do a better job than the TI-89. The CAS for the HP calculators is open source, but written in an obscure language. You'd be better off checking out the next CAS written by that guy: Giac/Xcas, written in C++.

Boost like libraries in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Can you recommend peer reviewed libraries that I can use in C environment (something like Boost for C++) ? Something that provides hash, thread, interprocess communications, lists, smart memory management...
The environment is embedded system, not a very minimal system, but also not a PC!
+1 for GLib from me, too. Plus, it has its own threading API too, so you don't have to learn pthreads if you don't want to.
Not sure if there exists such a thing as "smart memory management" in C, it's not very easy when you don't have the safety nets of destructors and control over operators. But, again, GLib has plenty of memory-oriented data structures and stuff that really makes life easier.
And no, I'm not on the GLib team, but I really do like it. :)
Check out the Apache Portable Runtime (APR) project.
Some of it's features:
memory management API
threads, mutexes
file I/O
atomic operations
hash tables, arrays
network sockets and protocol
shared memory, mmap
Not to mention it's portable.
I'm not sure if you'll find a single library that covers all of that... but you can check out glib and pthreads to cover a good bit of that.
Look at Boehm GC a widely used conservative garbage collector for C (or C++) that might serve your needs as far as smart memory management is concerned.
I'll jump on the GLib bandwagon too. Remember that C doesn't provide any syntactic sugar for complex data structures, so there are lots of casts and long function names in GLib, but it really does a great and efficient job with a little added verbosity!!
About the Glib use.
You probably can take what you need and cross-compile it. So if you just need the thread package - just compile that and don't take everything.
I'm doing the same thing with the Python VM. PyMite fits on a microcontroller and doesn't use all the functionality.

Resources