13 ms·
Scalable C – Writing Large-Scale Distributed C
- signa11 11y agothis seems to be still it is early nascent stage, with a complete toc missing, most likely, in the works. caveat emptor.
- PieterH 11y agoYes, indeed. I've updated the book title on Gitbooks to make this clear. I'm writing and publishing the book piece by piece, to get feedback early on in the process.
- lukaslalinsky 11y agoIt's funny how this centered around ZeroMQ, which is written in C++.
- geocar 11y agoThis might be part of the reason: * http://250bpm.com/blog:4 http://250bpm.com/blog:4 * http://250bpm.com/blog:8 http://250bpm.com/blog:8
- lukaslalinsky 11y agoInteresting, in this case it seems the choice to keep using C++ is just the most practical decision, which is much better than telling people to use C, but developing a library in C++.
- jeremyjh 11y agoWhich are weak arguments that point more to the author's dissatisfaction with the architecture of libzmq than with problems in C++ language. This was discussed previously here: https://news.ycombinator.com/item?id=3953434 https://news.ycombinator.com/item?id=3953434
- unscaled 11y agoIt's worth noting this was written by a different author.
- PieterH 11y agoIt is at heart a guide to using CZMQ, the high level C binding for ZeroMQ. More broadly, how to write scalable C applications like Zyre and Malamute. The fact ZeroMQ is written in C++ is invisible and irrelevant, except that it's easy to wrap in C.
- lukaslalinsky 11y agoThe point is that you are telling people to use C instead of C++ and that the best way to do that is by using a library that is written in C++. I completely understand that it's invisible, but it's not irrelevant -- it's not eating your own dog food (given that you are the author of ZeroMQ).
- PieterH 11y agoZeroMQ is a community of many projects. The old core library is libzmq, written in C++ by hundreds of people, led originally by Martin Sustrik. The library I'm explaining in this book is CZMQ, a high level C binding for libzmq that offers such things as actors, pollers, certificate management, beacons, and containers. CZMQ of course uses libzmq to send messages between processes and threads. In fact libzmq exposes only a C API, not C++ at all.
- _pmf_ 11y ago> While C lends itself to building libraries, it has no consistent API model. What language has? Wouldn't this require first class modules (which few systems have; JS' hacked together solution is obviously not to be considered a true solution)?
- vog 11y agoOCaml has a typesystem in which modules are first-class citizens, just like functions. They have a clear separation between interface (they call it "signatures") and implementation. The compiler enforces that you can only write against the interface, making modules with the same interface really exchangeable. The modules are also parametrizable (they call such modules "functors"). http://caml.inria.fr/pub/docs/manual-ocaml/moduleexamples.html http://caml.inria.fr/pub/docs/manual-ocaml/moduleexamples.ht... https://realworldocaml.org/v1/en/html/functors.html https://realworldocaml.org/v1/en/html/functors.html https://realworldocaml.org/v1/en/html/first-class-modules.html https://realworldocaml.org/v1/en/html/first-class-modules.ht...
- tom_mellior 11y agoThat's all true, but it doesn't mean that OCaml has a "consistent API model", whatever that may mean. Unless "provide a fold and a map for all datatypes", which I guess is consistent across most APIs, is a "model".
- deleted 11y ago[deleted]
- tomjen3 11y agoJava, C#, heck even PHP has classes support for languages these days. So has JS if you don't mind compiling using traceur.
- jasode 11y ago>The more C++ you know the worse you become at working with others. First, because your particular dialects of C++ tend to isolate you. So why is using something non-standard and non-universal like <czmq.h> not perceived as a "dialect" of C?[1] Any non-trivial source codebase beyond "printf("hello world")" will be a "dialect" of the programmer(s). When looking at the C source files of Linux kernel, Redis, and SQLite, etc, the syntax patterns, helper macros, string manipulations, etc do not look the same. Also the author's example of, for (i = List.begin (); i != List.end (); ++i) cout << *i << " "; is not the same semantics as: char *fruit = (char *) zlist_first (list); while (fruit) { printf ("%s ", fruit); fruit = (char *) zlist_next (list); } The C++ loop is multithread safe. The C version is not. For the zlist_next() to work, the "list" data structure needs to maintain mutable state in between subsequent calls. (Think of how something like strtok() works by mutating the string). [1]possibly because Pieter Hintjens is the programmer & CEO behind ZeroMQ and czmq.h. Therefore, it doesn't feel like a dialect to him?
- exDM69 11y ago> The C++ loop is multithread safe. The C version is not. Neither of these loops are multi-thread safe. That's assuming that's an std::list, and even if it isn't you can't implement that C++ API in a fast and thread safe manner. Yes, the ZMQ list is awkward (why did they put the "cursor" or the iterator inside the list object)? But that's not an inherent property of the C language. If you want an example of how to implement a linked list in C elegantly and efficiently, look at the intrusive linked list implementation in the Linux kernel (also available separately in the CCAN project).
- jasode 11y ago>Neither of these loops are multi-thread safe. Yes, as a general rule, all STL containers in C++ are not thread safe. (E.g. Write operations need locks. Read ops also need locks if other threads can write to the container.) However, I was looking at the specific loop example and only saw .begin(), .end(), and a pointer dereference which are all read operations. These should all be thread safe (assuming no other threads modifying the list) and I wasn't aware of any documentation that contradicts that.
- vidarh 11y agoGithub repository: https://github.com/hintjens/scalable-c https://github.com/hintjens/scalable-c
- neikos 11y ago> //Solution: make /usr/local writeable.// > This is a brutal and effective solution, the best kind of solution I... uhm, what? > Solution: grab the latest CZMQ git master from github. No, you do not want to run your software off of master, and the fact that Master doesn't always build (because of errors) should be a fringe occurrence with CI now being free/cheap and highly flexible.
- michaelmior 11y agoI'm not sure about CZMQ, but I assume what you eat is that you don't want to run code from a development branch that's rapidly changing. That's not necessarily what master is in all projects. The master branch is sometimes used as the latest stable release.
- neikos 11y agoTrue, I forgot about that aspect. However in this case that doesn't apply either as a stable branch should always compile.
- Asmod4n 11y agoduring the last years, master wasn't building a couple of times, it usually got fixed in a couple of minutes.
- SFjulie1 11y agoExactly the kind of code I make. I use C for performance.... as an extension for python that have a GC and a GIL. But more than never, I first use numpy (fortran) because it is dazzling fast and has specialized tricks of digital signal processing availables (ifft). And I do C after the profiler says to do so. When needed. If needed. Most of the dynamic data structure (message sent with zmq) the config, the parsing are better handled in python. And since I do as much sloc / day in C and python ... (1 python loc = 6 C loc) I code 6 times faster. And I don't have the headeaches of the dependency management I am totally okay with C, but when doing distributed system more often than never you also multi-threading. And C is not builtin for thread safety, it is harder. So I C some masochisms at work here. And I am pretty sure I am not the only coder thinking this book is full of pedantism and of advices not to be followed and it is empty. How did such a poor news made it to the top?
- vidarh 11y ago> more often than never you also multi-threading This is a Windows-ism that's crept into Unix-likes over the years, and it's not a good trend. Just don't do it. Especially since sharing direct access to state makes it harder to decouple components to scale them further. This book specifically argues to pass state via IPC for a reason. > And I am pretty sure I am not the only coder thinking this book is full of pedantism and of advices not to be followed and it is empty. Maybe, but iMatix has been a successful company for more than two decades, and delivered impressive open source applications (e.g. Xitami web server) and code-generation tools (Libero ec.) as well as cross-plaform utility libraries for C (SFL etc.) already two decades ago, and have gone on to develop large-scale C-based distribued systems and in the process developed things like AMQ and 0MQ that's been incredibly successful, so I for one tend to at least pay careful attention as they actually do have a track record.
- SFjulie1 11y agoI know a lot of successful companies with products that are notoriously poorly coded : - "security software"; - "game industry"; - "car industry"; And if you read correctly I think the biggest issue is TIME is MONEY. What are the advantages of using techniques that: - are expensive (productivity is constant in sloc whatever the language); - are notoriously a systemic risks given their domain of work and is hard to audit; - that can be smoothly achieved by upgrading a faster to build architecture in a scripting language ; They may have bigger balls of steels than me doing it in C and be the best programmers in the world. My question is business oriented: what is the economic rationale of a full C solution from start?
- jheriko 11y agothose three points in the bullet list near the start all seem to miss the mark for me.
- petke 11y agoI'm a cpp programmer who recently spent a week learning zeromq to replace named pipes in a project. By the end I was disappointed by the cpp language bindings as they only cover the low level library. Had I known from the start I probably would have looked elsewhere. Its a shame cpp is ignored in much of the open source community in favor of c. If nothing else Cpp after all is a safer c.
- dschiptsov 11y agohttp://250bpm.com/blog:4 http://250bpm.com/blog:8
- petke 11y agoYes I read those before. I didn't find them convincing. Intrusive lists is an anti pattern that you can also do in cpp if you want. Getting rid of exceptions doesn't mean you get rid of errors. It just means you can more easily ignore errors and continue running a corrupted program. But the big picture though is that cpp I a safer language. A core library might be written in c for whatever reason. But its good to provide a wrapper in a safer language for users to use.
- pjmlp 11y agoI blame the rise of GNU/Linux for it. Given how the market of programming languages looked like in the mid-90's, I bet C would have lost many users if it wasn't for the rise of open source. Remember that the GNU manifesto had for long time C as their main language. In the early days, this entry spoke only about C. http://www.gnu.org/prep/standards/standards.html#Source-Language http://www.gnu.org/prep/standards/standards.html#Source-Lang...
- deleted 11y ago[deleted]
- fizixer 11y agoLove it. I dream of the day when all current system-level fads bite the dust, replaced by new fads, while C is still running as the system layer. (hint: just like it is happening today with the 90s fad called C++, replaced by fads like Go, Rust, D).
- magicmu 11y agoI know the basics of C, but stopped short of getting deep into threading and concurrency since it seems like Go and Rust handle that in a more efficient way (although there's no way I would use Rust in production yet). Are there any advantages to using C/C++ for a new large-scale project?
- Aeolos 11y agoThe main advantage is a huge body of mostly functional code that you can attempt to integrate into your build system. The main disadvantage is that good C++ programmers are very hard to come by.
- hyc_symas 11y agoGood programmers are hard to come by, and that will always be true, regardless of what language you choose.
- Aeolos 11y agoVery true, and that's why newer languages are designed to limit the amount of damage average programmers can cause, while maintaining the same level of performance. See Rust for example. Sorry, small rant incoming. Ignore at will! I would still argue that good C++ programmers are exceptionally rare. I've worked with C++ professionally for roughly a decade (and as a hobby for many years before that), and despite using it and reading about it and fighting it and reasoning with it, I still don't consider myself a good C++ programmer. I've met several self-proclaimed good C++ programmers, but I've only ever met one I'd genuinely call good. He had experience in a dozen languages, half of them functional, and he produced code that was concise, maintainable, fast, documented, with correct pre- and post-conditions. Perhaps the best part was that he created interfaces that were a joy to use and very hard to misuse. He didn't like C++, but he knew how to make it work anyway. It was a joy to work with him. Contrast with your average C++ programmer who will overuse templates (hey, it's generic), generate a deep class hierarchy (hey, it's object oriented), of course with tons of setters and mutable state (parallelism? isn't that like std::thread and stuff?) and then waste time micro-optimizing anyway (hey, C++ is fast). Bonus points for avoiding the STL because "the STL is slow" (== game programmer, although this is becoming less common nowadays). Extra bonus points for arguing that GCs are slow, and then littering the code std::shared_pointer everywhere. These days, I go as far as to say that anyone who only has C++ in his toolbelt is not a particularly good C++ programmer, simply because of the lack of perspective.
- bluejekyll 11y ago> then you know where C stops working, as a language. He actually makes a really strong argument against using C right in the first two paragraphs. C is a dangerous language. Assembly is even more dangerous. There are languages that compile to close the same speed and are systems oriented with 0 overhead. I'm truly curious, if you're working on a new project would you pick C? Or would you reach for something that's going to reduce the bugs that inevitably come from writing even 10 lines of C?
- chris11 11y agoWhat languages would you personally pick over C?
- bluejekyll 11y agoRust, no debate.
- tom_mellior 11y agoI'd be interested in this if it were nearing completion. I think structuring the book around problem-solution pairs is a nice technique. But it would be a much better read if fewer irrelevant statements of opinion were strewn in. Also, a lot of the bizarre statements of irrelevant "fact" should be checked, for example: > In the Old Times, creating a repository was days, weeks of work. I can't begin to comprehend what this may mean. "svn create" (or whatever it was called) was always instantaneous. Setting your project up for network access took longer because you had to read docs and write a config file, but the same is true for Git. > Optimizing compilers (...) may remove assertions. Bullshit. Using NDEBUG removes assertions, and yes, this indeed means that assertions must be side-effect free. But an optimizing compiler? No. If that actually happened for calls to impure functions (and no, it really doesn't happen), it would be a major compiler bug. > a nasty reminder of the old days when computers stored data and code on different kinds of rust, and languages enforced that Code and data do live in different places in memory; nowadays more than ever, for reasons of security. C's original "declarations before statements" rule (the context here) was simply because it makes it much simpler to write a primitive single-pass compiler. > The standard C library often puts destination arguments first, which is a hangover from assembly language. MOV X, Y. ... or maybe it's an analogy with assignment statements, X = Y? Note that three out of these four examples are just irrelevant opinions, so they should be removed from the text even if they weren't factually false.
- PieterH 11y agoCreating an svn repo was fast, yet you could not use it without a dedicated server, DNS configuration, security configuration, firewall configuration, etc. etc. If this was your only job, sure, a few hours' work. For the rest of us, begging a sysadmin or spending days learning the details. Whereas with git it's literally "git init ." or clicking on Github.com and we're ready to roll. I do appreciate the fact checking, and you're welcome to send me more comments. Errors of fact don't survive the editorial process, one hopes. Opinions, that's a different story.
- tom_mellior 11y ago> with git it's literally "git init ." That doesn't magically give you a shared, network-accessible repository with all the correct access controls. > or clicking on Github.com SourceForge has existed since 1999, and after a click you have always been ready to roll. > and you're welcome to send me more comments But I probably won't if your strategy is "spread misinformation first, then make others work to point out mistakes, then defend an indefensible position, then maybe change it". That's not how communities are built. That's what you yourself criticize in the section on merging strategies...
- nickpsecurity 11y agoNice work in progress, Peter. Look forward to seeing more of it given your prior work. I light how you preempt many C-related counterpoints with model-driven development that generates C. Done excellently by iMatix and many others. I'm especially interested in how you'll apply that to distributed C.
- halayli 11y agoThis looks like it's coming from someone who doesn't know C++ well and is just coming up with reasons to fit their bias. The fact that he/she didn't mention any disadvantage to the C code written beside verbosity makes it clear. For one, it's easy to forget to call zlist_destroy. Who owns what in C can get very complicated and you can run into dangling pointers. At least in the C++ version you can manage ownership easier in their case. I am not defending one language over the other, I use them both and have experienced the advantages and disadvantages of each. What's being shown in this book is not how typically you create link-lists. man queue(3) to see how it's generally done. The C++ for-loop is not how you typically iterate over a list , again the author decided to show a bad example to confirm their bias: for (const auto& i : List) cout << i << " ";
- unscaled 11y agoMore likely, someone who hasn't programmed C++ in the last 10 years. Forgetting auto, and using the cumbersome 3-part for loop with iterator boilerplate when you only need value shows age. Initializing the list is also easier now, with initializer lists syntax, so you could just do: list<string> lst = { "tomato", "grape", "apple", "orange"}; and cut another 4 lines, making the C++ line count half of C version. Not a negligible difference, as the author claims.
- doodpants 11y agoSo far I've only read the Preface and part of Chapter 1. What bugs me is this: > * Write portable code that runs on all platforms. Ok, good plan. > * An operating system you are comfortable with. Linux will give you the best results. OS/X [sic] and Windows are usable if you have no choice. So... results vary by platform? And then after the "hello world" example: > And you should see that familiar Hello, World printed on your console. If you are using OS/X [sic] or Windows, it won't be this easy. I'll repeat my advice to install Linux. Funny, this example works just fine for me on OS X. You do realize OS X is a Unix-like system, right? > Having said that, remember this rule: > Linux is the native environment for C development. Gee, I wonder how people like Dennis Ritchie ever managed to write C code before Linux came along?