• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • A function that returns twice: fork()

    December 6th, 2012
    tech  [html]
    Generally a function returns once:
      random_boolean() {
        // seed our random number generator with the current time
        srand(time(0));
        // return true half the time, false half the time
        return rand() % 2;
      }
    
      main() {
        if (random_boolean())
          printf("true");
        else
          printf("false");
      }
    
    This will print either "true" or "false", depending on what random_boolean() returns. [1] But it doesn't have to be this way:

      random_boolean() {
        return fork();
      }
    
      main() {
        if (random_boolean())
          printf("true");
        else
          printf("false");
      }
    
    This will print both "true" and "false": fork() returns twice. What's going on? When a program calls fork() the operating system clones it, returning true to the original and false to the clone [2]. The flow of control splits. You might wonder: what if I ran a program like:
      main() {
        while (1)
          fork();
      }
    
    Roughly, each time through the loop we double the number of processes. That should exponentially create processes, which might be bad. As it happens, several years ago I did wonder about this. To find out, I wrote a program, and discovered that it is in fact quite bad, crashing my machine extremely quickly. After which I moved on until one evening about a year later when everyone in my operating systems class was trying to finish a project due at midnight. Working remotely, ssh'ed from my edge-of-campus dorm into the cs department's login server (which was also the primary file server), I ran ~/a.out when I meant to run ./a.out. This took down every computer in the department:
      From: Jeff Kaufman
      To: Swarthmore CS Department Sysadmin
      Date: Sun, 18 Sep 2005 21:00:59 -0400 (EDT)
      Subject: Allspice accidental downtime
    
      I accidentally ran a fork bomb on Allspice, making it was unusable
      at user level for about 25 minutes.  The fork bomb was sitting (very
      stupidly) in my home directory as a.out, left over from messing
      around with it almost a year ago.  I then ran ~/a.out instead of
      ./a.out.  Much badness ensued.  Mary had keys to the server room, so
      we came down to the science center and rebooted allspice.
    
      Everything seems to be ok now.
    
      Multitudinous apologies,
         Jeff
    
      PS: perhaps this makes a good case for ulimits?
    
    Arriving at the science center to fix the problem I had created, my classmates were pretty unhappy with me.

    After this we shifted remote logins to go to random lab machines via round-robin dns instead of to the central server.


    [1] While gcc will compile this code as written, you shouldn't actually write C like this. I'm writing tersely to keep the important stuff from drowning in the verbosity of C.

    [2] Technically it returns 0 to the clone and the process id of the clone to the original. Which are usually called parent and child. Minor details.

    Comment via: google plus, facebook

    Recent posts on blogs I like:

    Austerity is Inefficient

    Working on an emergency timetable for regional rail has made it clear how an environment of austerity requires tradeoffs that reduce efficiency. I already talked about how the Swiss electronics before concrete slogan is not about not spending money but ab…

    via Pedestrian Observations February 27, 2021

    Fireside Friday, February 26, 2021

    Fireside this week, but next week we are diving into our long awaited series on pre-modern textile production, though we will be particularly focused on the most important clothing fibers in the Mediterranean world, wool and linen (rather than, say, silk …

    via A Collection of Unmitigated Pedantry February 26, 2021

    The Troubling Ethics of Writing (A Speech from Ancient Sumer)

    (Translated from a transcript of an ancient Sumerian speech by Uruk's most well-respected Scriptological Ethicist) Writing is a profoundly dangerous technology: Access to writing was initially, and still remains, uneven. What's worse, the rich are m…

    via BLOG - Cullen O'Keefe February 15, 2021

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact