I see why VMs are so popular…
At least for functional-flavored languages. C’s stack and the lack of any explicit way to manage it really makes coroutines/fibers a pain in the ass, and turns any attempt at implementing tail calls into a horrific nightmare. At least if you care about performance /and/ portability. It does however excel at implementing state machines, which gets you about one step away from a VM. But to do them (TCO/fibers) directly you’ve really gotta jump through some hoops. The easy solution would be to use goto… but C won’t let you jump to labels across functions (and for good reason, given they didn’t have its use as a /target/ language in mind).
So let’s see some options I’ve come across. We’ll start with fibers/coroutines as anyone bothering to read this will probably find them more useful (and many of the benefits of TCO can be had with them anyway)…
Protothreads. This is probably the single coolest bit of C I’ve ever seen. In a nutshell, it’s a set of macros which turn a function into an instance of Duff’s device where calls to yield form the boundries of the case statements.
Pros:
ANSI C, fast, and ridiculously lightweight with a simple implementation (if you grok duff’s device). Includes a timer mechanism which can be leveraged for some semblance of preemtiveness.
Cons:
Totally loses local variables between yeilds/timeouts. Its idea of “thread” is limited to a function, blocking calls further down the chain need extra work to deal with. Implementation details make use of switch dangerous.
Other:
Employment of static declarations where nessesary were succesful but known problems with volatile makes some other possibilites dangerous. Blocking issue wasn’t particularly investigated, the code generated was treating functions as actors — they were /all/ protothreads with timeouts around the few I/O related functions exposed to the HLL. Performance as such was surprisingly decent. The cons are trivial to deal with in the small code bases of its intend use — embedded devices.
pthreads
Heavyweight OS-backed threads.
Pros:
Has the obvious advantage of being preemptive and able to utilize multiple cores across multiple processors without any additional work.
Cons:
Has the obvious disadvantage of being preemptive in a world where people are sadly often too fucking stupid to use something like STM and/or message passing, preferring instead to fool themselves into believing that they can handle manual locking while walking along the edge of non determinism bolted into a serial language rather than reserving it for cases where profiling actually shows it to be nessesary, which frankly for “typical desktop applications”, it probably isn’t.
Probably a bad idea on typical computers if you intend to spawn half a million threads but obviously useful for distributing fibers across cores.
Not ANSI C, but there are many libraries available that can ease portability in various ways such as Pthreads-w32 or a host of libs that simply abstract the native threading capabilities (if any) of whatever OS they’re running on.
setjmp/longjmp
Not a whole lot to say, essentially just saves/restores the stack pointer and registers for later restoration. ANSI C, fast, but takes more work to build coroutines out of since you can only jump /down/ the /current/ call stack, provided the matching setjmp is still /below/ it. This bites a lot of first attempts at such, as well as a few attempts at using it for exception handling. Chicken Scheme uses this when nessesary (stack is almost full) to jump back down to a trampoline after moving any live stack data to the heap in its implementation of TCO, folowing the ideas of Cheney on the M.T.A., from which it is free to generate the C code in continuation passing style, and thus provide call/cc, which gets you to whatever sort of threading or exception handling you want to build out of it.
ucontext.h
What a lot of people wish setjmp/longjmp were (and many would despise…). Basically gives you one shot continutations by saving the entire stack (rather than just the stack pointer) to heap for later restoration. Unfortunately you need POSIX to get this. But if you have it fibers become nearly trivial. A lot of wrapping libs seem to happily use it where available with or instead of pthreads such as Gnu Pth etc…
Most others attempts I’ve seen at getting coop threading or fibers in C boil down to various wrappers around or reimplementations of setjmp.h or ucontext.h with their own various performance and portability characteristics, many of which were facinating. While not about threads per se, Implementation Strategies for First-Class Continuations is a great overview of differrent methods with more detail than I could ever be bothered with, where even just simpler one shot continuations gets you a stone’s throw from fibers and exceptions and a lot of the problems are the same.
Unfortunately most of this doesn’t really help me to get here, which is where I really want to be (and from which I get the rest practically for free). At least in any intuitive fashion. Surely I can fake it (as mentioned with protothreads) but it just feels… dirty. Of the other possibilities trampolining is common and simple to implement, and Cheney on the M.T.A.’s/Chicken’s way of doing it while also using the stack as part of the GC system I think is quite slick, but they also just don’t seem right. Alas, most the other attempts I’ve seen involve inline assembly, relying on compiler specific extentions (GCC and LLVM will do TCO at least on intel and ppc), or editing the resulting asm to replace calls with jumps before the final assembler/linking passes, which is comical at best, and none of which I’ll attempt. So it seems to me that my only real options are to by whatever means make each function a thread/actor at the C level (as done) or adopt some form of trampolining.
Or… quit worrying about any of it and just implement a VM, which C excells at. Or use any of a number of existing VMs (Neko looks particularly nice for my uses, other than its being under the lgpl which has a tendency to make my brain segfault whenever I attempt to parse it).
Really though, I wish C would just give me a fucking nonlocal goto to use when generating code. Or for someone to throw money at the C– project. And while I’m wishing, being able to depend upon the presence of ucontext.h when I’m writing C by hand would be fucking awesome. I think the C1x people should do that rather than a specific threading implementation, though for many uses the effect would be the same (assuming it retains state as I’d expect, anyway), and just as braindead for embedded devices.
Eh, ok, enough half incoherant rambling. I’ve been awake far too many hours and my stomach is threatening to start digesting my tongue if I don’t provide it with real food soon.
Two days to fustration in #C
The first day I took some flak to be sure. Especially after mentioning getting back into C after time with Scheme. E.g. being told by three different people that I couldn’t do something, and that if I want a language that has feature X then I should use a language that has feature X. All
I was looking at was retaining some scoped state between function calls to write a generator without having to get into coroutines or breaking it into comically short files. In this particular case, adding some peculiarities to rand() rather than calling it multiple times for different ranges. Did it scant moments later with a ‘static’ declaration and hadn’t posed it as a question in the first fucking place. But like I said, I had mentioned being into Scheme. I expected some flak.
On to today, amacleod had actually suggested that iterating over an array (or in this case, a string) simply to copy its contents was “quite fast”, asking “how do you think the C libraries do it?” when I mentioned it’d be piss slow. I guess in amacleod’s world there’s only one implementation of the std C libs, and it’s a clone of the examples in K&R. For the rest of us in
the real world there’s http://bytes.com/topic/c/answers/217848-memcpy-vs-performance or http://cboard.cprogramming.com/c-programming/92339-memcpy-vs-memmove.html or any of a dozen
others all saying the same thing. Or god forbid, actually banging out the code and doing some profiling…
But hey, whatever. I fucked off and made some bacon and eggs. Nom. But when I came back it was to see /this/ bullshit in my buffer.
16:21 < amacleod> However, if you can put it in there you can’t use it as a sentinel because it’s just the number 0.
16:22 -!- para7 [~para@port-83-236-166-89.static.qsc.de] has joined ##C
16:23 -!- zommi [~andreas@nat/ibm/x-lhegmcnivhijoqxu] has quit [Read error: Connection reset by peer]
16:23 -!- arvind_khadri [~arvind@unaffiliated/arvind-khadri/x-2237230] has joined ##C
16:23 < amacleod> cc7gir, it’s really unclear to me what your code is trying to do.
16:23 < amacleod> Helsinkiii, so, tell me what “starting from the beginning” looks like in C code.
16:24 < cc7gir> amacleod: I want to find the mode of the the list of numbers
16:24 < cc7gir> the most frequently occuring number
16:24 < Helsinkiii> amacloed: probably string[i] where i=0
16:25 < Helsinkiii> i’m 2 days into c and im coming from Java, so yeah. i can’t afford our class textbook
16:25 < amacleod> Helsinkiii, yeah.. int i; char string[] = “hello”; for (i = 0; ; ) { } sounds like a good starting point.
16:25 < PoppaVic> Nice.. Well, accept the fail and off to the next class
I won’t expect the guy who lives in a strange monopoly libs world to try just answering the question (which was along the lines of copying until in a loop… which has a one line solution in the K&R book he thinks all libs are based on) but that ray of sunshine from PV was just fucking priceless.
16:26 < Helsinkiii> no
16:26 < Helsinkiii> our teacher said it’s optional
16:26 < Helsinkiii> amacleod: what i did is make 2 do whiles, nested
16:27 < Helsinkiii> the first gets the string
16:27 < Helsinkiii> the nested one copies it into an array
16:27 < Helsinkiii> then at the end, increments, and then the outer loop runs again
16:27 < amacleod> Ok.. the outer loop is because you need to get more than one string from input?
16:27 < Helsinkiii> yes
16:27 < Helsinkiii> as many as the user wants, unitl he presses enter on a blank line
16:27 < Helsinkiii> aka he enters null
16:27 -!- Xorlev [~xorlev@wl-dhcp181-207.Mines.EDU] has joined ##C
16:27 -!- Xorlev [~xorlev@wl-dhcp181-207.Mines.EDU] has quit [Changing host]
16:27 -!- Xorlev [~xorlev@unaffiliated/xorlev] has joined ##C
16:27 < amacleod> so, as nadder pointed out about half an hour ago, you can just use the address of the first element of the array as the target of fscanf
16:28 < amacleod> er.. fgets, I mean.
Which he may have, if he hadn’t been told to use a loop instead simply for the sake of disagreeing with me. But even better, now the alpha asshole steps in, who amacleod immediately gives lead to… and who, to be fair, a concesending prick though he may be, at least has the virtue of actually being familiar with C.
16:28 < Zhivago> You appear to be confused as to what “null” means.
16:28 -!- deadlock [~no_uid@unaffiliated/deadlock] has quit [Ping timeout: 272 seconds]
16:28 < cc7gir> me?
16:28 < Helsinkiii> no me
16:28 < amacleod> Heh.
16:28 < amacleod> Both a yaz, actually.
16:29 < Helsinkiii> so, how would i wrote that. whats the syntax like for doing such a feat
16:29 < amacleod> Helsinkiii was most recently speaking of the empty string (“”) as though it were “null”.
16:29 < Zhivago> I suggest reading the documentation for fgets.
16:29 < amacleod> I concur.
16:30 < Zhivago> If you cannot read then it is time to consider another profession.
16:30 < Zhivago> Perhaps landscape gardening.
16:30 < Prael> Man, I’m glad I didn’t learn C here.
That “empty string” would be a newline which is far from “empty” in C (really amacleod, leave it to Zhivago…) but other than that, ‘nuf said. If other #C’s on other networks are anything like Freenode’s, I’m hardly surprised that C’s popularity is constantly declining.
Still, it’s been serving my purposes quite admirably. One of my ‘toy interpreters’ is now a (mostly) self compiling (to C99) actors language, somewhat like an Objective C with implicit green/lightweight threading and sexps. *grins*.
PayPal just fucked me.
Been busy the last couple months, working on houses, getting some cash together. Finally did, and got a few books regarding web design. Just made a simple site, it came out quite nicely. Got a PalPal account so they could pay, and they did. Went to add my bank acct and verify it… whoops, “You are currently not allowed to add a bank account because your account access has been limited.”. Everything in PayPal’s help area regarding such, with the steps for fixing it, don’t work. There’s nothing in the “Resolution Center” most of them reference. I just confirmed my cell phone, upgraded it to Premier, and still the same deal.
What. The. Fuck?
Yes, I’ve tried to call them. It’s a bunch of voice prompts, all the options being things already on their site.
Yes, I’ve tried to email them, it says 2-4 days for a reply which will probably be canned anyway.
Update: The resolution seems to have been to upgrade to a business acct. Can’t imagine why… bank acct in my name, I wasn’t planning on switching to a business PayPal acct until I got one as such from my bank. Go figure. Waiting for the deposits to verify the bank acct now. Any more problems and I’m going to just drop PayPal and get a merchant account.
Update: Woke up, checked to see if the two little deposits paypal makes to verify account had been made, and they hadn’t been. Nothing unusual, it can take 3-4 days and I started it on a weekend anyway. Checked paypal acct, and I see this:
We recently reviewed your account, and we need more information about your business to allow us to provide uninterrupted service. Until we can collect this information, your access to sensitive account features will be limited. We would like to restore your access as soon as possible. We apologize for the inconvenience.
Why is my account access limited?
Your account access has been limited for the following reason(s):
Sep. 22, 2008: We have detected suspicious activity regarding the receipt or withdrawal of funds.
Apparently, there were two refunds. Except my one customer swears they didn’t request one, and the 2nd one is… a whole 10 dollars, which was from my self when I set up the acct to make sure I could accept $ with it. I fucking know I didn’t request a refund from my self. Again: What. The. Fuck?
Needless to say, I’m not fucking amused at all. I will attempt to get my fucking money out of this situation, but considering their well known history of this crazy shit, I’m not expecting much. Damned thing is I knew about this going in. It was just the only real way I could accept a payment at the moment. I was so happy, too. Was going to use the cash to get a business license, open a new bank acct for it, get a proper domain and hosting, and buy more books to study, particularly regarding PHP5, as most of my PHP knowledge is from when PHP4 was new. Now I have nothing.
I’ll just have to borrow the cash I need, and work my way from there. Which is precisely what I was trying to avoid having to do. Needless to say, no more of my earnings will be trusted to PayPal.
No more Facebook.
Logged In or Out, Facebook Is Watching You
Facebook A Black Hole For Personal Info
Facebook Sharing Too Much Personal Data With Application Developers
Facebook Widget Installs Zango Spyware
Facebook Caves To Privacy Protests Over Beacon
Facebook Beacon Privacy Issues Worse Than Previously Thought?
Facebook Retreats on Online Tracking
Facebook Users Complain of New Ad-Based Tracking
The New Facebook Ads – Another Privacy Debacle?
From Facebook’s Terms of Service:
When you post User Content to the Site, you authorize and direct us to make such copies thereof as we deem necessary in order to facilitate the posting and storage of the User Content on the Site. By posting User Content to any part of the Site, you automatically grant, and you represent and warrant that you have the right to grant, to the Company an irrevocable, perpetual, non-exclusive, transferable, fully paid, worldwide license (with the right to sublicense) to use, copy, publicly perform, publicly display, reformat, translate, excerpt (in whole or in part) and distribute such User Content for any purpose, commercial, advertising, or otherwise, on or in connection with the Site or the promotion thereof, to prepare derivative works of, or incorporate into other works, such User Content, and to grant and authorize sublicenses of the foregoing. You may remove your User Content from the Site at any time. If you choose to remove your User Content, the license granted above will automatically expire, however you acknowledge that the Company may retain archived copies of your User Content. Facebook does not assert any ownership over your User Content; rather, as between us and you, subject to the rights granted to us in these Terms, you retain full ownership of all of your User Content and any intellectual property rights or other proprietary rights associated with your User Content.
Sandvine, Linux, Windows, BSD, and an opinion.
So, Cat in the Red Hat has firewall rulesets for Linux and FreeBSD/Windows for ignoring Sandvine’s forged RST packets.
Now, I’ve always preferred BSD-style firewall configs. But I’ve never seen before such a painfully clear example of *why* I prefer them. If you’re at all familiar with how TCP/IP works, “add drop tcp from any to me 6883 tcpflags rst”, isn’t exactly brain surgery to understand. Now, does “-A INPUT -p tcp –dport 6883 –tcp-flags RST RST -j DROP” really add any relevant information so far as the action you’re trying to perform? Why is RST stated twice?
But really, that’s minor. What’s the real difference here? BSD firewalls use a Domain Specific Language, whereas Linux uses flags to an application. Either way has its pros and cons.
It’s a matter of taste, really. I prefer the one that doesn’t look like line noise, and doesn’t involve an extra process to get to use things like variables (as in the script for Ubuntu/etc…). What if I want to make 6883 a variable, holding a list of subnets? Well, the solution in the linked post was to wrap the rulesets in a shell script. Would iptables support, say, a list of subnets instead of a number as the value of that variable? Well first I’d have to read the documentation for iptables, and either way read up on a separate scripting language (Case 1: It does, and I only need to know how to set a variable in the script language. Case 2: it does not, and I need to also learn how to do a loop in the scripting language). Having a DSL for the rulesets, however, all 2 (or 3) steps above are covered within the same document, within the same section. Indeed, within the very same text. As it explains how to set a variable to multiple values, it can be inferred that rules will accept them, and that explicit looping is unnecessary. Speaking specifically of OpenBSD’s PF here, it’s simply the most elegant firewall system I’ve ever dealt with (and the only one I’d describe as “elegant”, for that matter).
Of course it could be argued that iptables doesn’t make you learn a new language, and that you can just use any scripting language you’re already familiar with (assuming you’re using a *nix because you like *nix, and not just because you hate Windows and can’t afford OSX, you probably know some script language or another). But then I’d say, look at the shear complexity of those flags… you’re still essentially learning a DSL anyway, and if you just really, really want to use your scripting language of choice? The ‘ipfw’ command is available to any OS using IPF as its firewall, and performs the same function as ‘ipfilter’ does for Linux’s firewall. And IPF has been ported to FreeBSD, NetBSD, OpenBSD, SunOS, HP/UX, OpenSolaris, Linux, QNX, OSX, Windows… being the default on at least the first two (maybe Solaris as well, I forget). Who didn’t have to learn what, now?
As an aside, the fact that there’s two configs for what is at its core the same OS, and one script for two entirely different OS’s (and all the others IPF supports, at that) just seems hilariously ironic to me. I would assume that the ipfilter script for Ubuntu/etc… would work on RedHat &co as well, but I’m still laughing.
[update] How odd. The IPTables/IPF postings in Cat in the Red Hat’s blog have disappeared. There’s a story on an IPTables-only version today on Slashdot though.
Why I hate YIM.
A while ago a friend of mine wanted me to access their webcam over Yahoo Instant Messenger (no, not for amateur porn). What the hell ever happened to web-based camera feeds? You know, the “web” in “webcam”? Does anyone here remember JenniCam? Oh well, anyway. I happened to have a Win2K install at the time, Pidgin lacks webcam support, and so I reluctantly installed YIM’s own “official” client.
————————————–
Hate:
————————————–
Forced, entirely unmentioned install of Adobe Flash. Adobe can’t even make a PDF reader not take an agonizing amount of time to load and display even the simplest of PDF’s. Xpdf and gv in Unix start, as far as practical, *instantly*. Foxit does the same for Windows boxen. And the shear size of it! Ugh. You’d think the company that invented the format could produce a decent viewer for it? Apparently not. So why should I want anything else of theirs on my system? Especially given Adobe’s history in recent memory: Adobe Quietly Monitoring Software Use? — Adobe Flash Zero-Day Attack Underway. Yeah. I totally want their shit on my computer. Riiight. To be fair though, at least they’re finally opening up the specs to the Flash format. Not that you couldn’t get them before, but now you’ll be allowed to use them to actually write a player, not just a producer, and projects like Gnash have begun to suck quite a bit less since then. Anyone willing to bet this wasn’t simply in response to Microsoft’s Silverlight? Everyone’s familiar with the phrase Embrace, Extend, and Extinguish. Just sayin’. Regardless, I was trying to install a damn IM client, not a browser plug in. The only plausible excuse I can think of for this is if it’s using Flash for its webcam support… but then why can’t I just point any old flash-capable media player at the stream?
Oh, I know why! Ads in the webcam window, unless it’s fullscreened. Want to multitask? Say, to write to the person who’s webcam you’re viewing to request a different view angle on the problematic component they’re trying to show you? And face it, for many, many users, tits? They know their userbase, and sex sells.
But that’s not the only time you see ads. They’re in your friend’s list. Constantly. Are we ad-ware yet?
During install, must opt out of installing a “toolbar”, and some other browser crap. I want an IM client, not my browser hijacked.
Weather and music crap. A VOIP phone balance telling me I have no minutes either way, which are separately billed, of course. Irrelevant plugins. What the fuck? I even told it during the install that I didn’t give a shit about VOIP. And if I wanted to stream music, or use VOIP, why the hell would it be with an IM client, from Yahoo no less? If they can be turned off, it wasn’t very obvious how to do so. Stop advertising shit I’ll never use. It makes me cringe, it makes me desperate to slam the reset button and get back into BSD, where do one thing, do it well reigns supreme. IM client. I want IM. I have a back yard. I have music. I have a cell phone, kthx?
It starts with windows, without ever having asked to do so. It’s not in the start up folder. Any software which does this is FUCKING EVIL. YIM is not a device driver, the only remotely plausible excuse for this behavior. Again, if this can be corrected, it wasn’t obvious how to do so. IM client, not a device driver.
Yahoo Messenger Insider. What The Fuck? No, I don’t want what you call “news”. I don’t want to see it every goddamn time I log into Yahoo, especially considering how piss fucking slowly it (and YIM itself) loads. I want an IM client, not a goddamn web browser.
Quit checking my fucking email account. I don’t care what’s in it, anyone fucking stupid enough to email me there doesn’t know me well enough for me to give a shit about in the first place. IM client. Not mail client. Why the hell is YIM trying to be the center of my fucking life?
————————————–
Annoyances:
————————————–
GUI. It stands for “Graphical User Interface”, not “Candyland User Interface”. Using it made me feel like I was in preschool.
No tabbed windows. An entire separate window for each person is simply… primitive. Can we get out of the fucking 1990′s, please? Maybe it wouldn’t be so bad if Windows supported tabbed window management, but it doesn’t. Unless you replace the shell, of course.
Webcam only support 1x, 2x, 3x, and fullscreen zoom levels. I can’t remember the last time I saw anything related to the display of media with such limitations. Maybe this is normal in Windows?
————————————–
The aftermath:
————————————–
Bad lightning storm, some bits of kit were fried (along with a tree, a few power line transformers…), the computer I was using is now an NFS server for my laptop, which lacking a (working) HDD is acting as a diskeless client. Because you can do that kind of shit in Unix, without a “pro” or “corporate” version of your OS and yearly Terminal Services licenses. For obvious reasons, I nolonger have a Windows install, and with it no YIM. I’ve also come to find out that Kopete supports the viewing of YIM webcams perfectly well, and have it installed for such cases where it may be nessessary again. I’d just use it exclusively instead of Pidgin, but like I said… I’m a fan of do one thing, do it well, and I don’t appreciate the resources it sucks up loading all of its required support daemons (artsd, dbus, a whole slew of shit under kdeinit…) every time it starts. Granted KDE at least has a nice modular design, I still just don’t want all of that to run a simple IM client, and I’m not interested in the Windows/OSX-cloning desktop-”integration” whoreing behaviors of KDE/Gnome/etc… in general. But that’s for a different post, at a different time.
-
Recent
-
Links
-
Archives
- October 2011 (1)
- January 2011 (2)
- August 2010 (5)
- July 2010 (5)
- May 2010 (3)
- March 2010 (10)
- February 2010 (7)
- November 2009 (1)
- October 2009 (1)
- August 2009 (1)
- July 2009 (3)
- April 2009 (2)
-
Categories
-
RSS
Entries RSS
Comments RSS