EzDevInfo.com

fiber interview questions

Top fiber frequently asked interview questions

What is the difference between a thread and a fiber?

What is the difference between a thread and a fiber? I've heard of fibers from ruby and I've read heard they're available in other languages, could somebody explain to me in simple terms what is the difference between a thread and a fiber.


Source: (StackOverflow)

Processes, threads, green threads, protothreads, fibers, coroutines: what's the difference?

I'm reading up on concurrency. I've got a bit over my head with terms that have confusingly similar definitions. Namely:

  • Processes
  • Threads
  • "Green threads"
  • Protothreads
  • Fibers
  • Coroutines
  • "Goroutines" in the Go language

My impression is that the distinctions rest on (1) whether truly parallel or multiplexed; (2) whether managed at the CPU, at the OS, or in the program; and (3..5) a few other things I can't identify.

Is there a succinct and unambiguous guide to the differences between these approaches to parallelism?


Source: (StackOverflow)

Advertisements

What is a fiber cookie?

I was browsing through the .NET Marshall class, when I noticed the method GetThreadFromFiberCookie. I tried searching for the term, but only found references to Fibers in the context of threading on Windows. So what is a fiber cookie?


Source: (StackOverflow)

How do I use Ruby1.9 with Shoes?

Shoes wraps it's own Ruby install, right?

I can't use Fiber which is a Ruby1.9 feature. And, I want to use a Fiber for creating a generator.

Here's my code (so you can make sure the problem isn't with my code):

class BrownianGenerator
  def initialize
    @x = 0
    @fiber = Fiber.new do
      loop do 
        @x = @x+rand; 
        Fiber.yield @x
      end
    end
  end
  def next; @fiber.resume end
  def rewind; @x=0 end
end

and if I made a shoes app like this:

Shoes.app do
  @b = BrownianGenerator.new
end

if I pull up the shoes console, I see the error:

uninitialized constant #<class:0xblah>::BrownianGenerator::Fiber

Since, it's saying Fiber is an uninitialized constant, either something is wrong with my code or this Ruby version doesn't know about the Fiber class - the latter should be the case.

I saw this question on determining the version of Ruby (which is 1.8 for my mac install), but I don't know how I would change the version.


Source: (StackOverflow)

How to pass a fiber to a thread?

I am wondering how I do I pass a fiber to a thread?

The only way that I managed to do it was by casting to and from shared.

auto fiber = new Fiber((){
});
auto t = spawn((){
    auto fib = cast(Fiber)receiveOnly!(shared(Fiber));
    writeln("fib");
    fib.call();

});
send(t, cast(shared(Fiber))fiber);

But that doesn't seem right. I think I don't fully understand the implications of shared.

I had to cast the fiber to shared because I wasn't allowed to send it to the thread without it. I had to cast it back to a fiber because I couldn't call Fiber.call on a shared fiber.

What is the correct way of passing a Fiber to a thread?


Source: (StackOverflow)

Lightweight, portable C++ fibers, MIT license

I would like to get ahold of a lightweight, portable fiber lib with MIT license (or looser). Boost.Coroutine does not qualify (not lightweight), neither do Portable Coroutine Library nor Kent C++CSP (both GPL).

Edit: could you help me find one? :)


Source: (StackOverflow)

What are Erlang processes behind the scenes?

I've got very limited knowledge about Erlang, but as far as I understand, it can spawn "processes" with a very low cost.

So I wonder, what are those "processes" behind the scenes?

Are they Fibers? Threads? Continuations?


Source: (StackOverflow)

Making multiple HTTP requests asynchronously

require 'net/http'

urls = [
  {'link' => 'http://www.google.com/'},
  {'link' => 'http://www.yandex.ru/'},
  {'link' => 'http://www.baidu.com/'}
]

urls.each do |u|
  u['content'] = Net::HTTP.get( URI.parse(u['link']) )
end

print urls

This code works in synchronous style. First request, second, third. I would like to send all requests asynchronously and print urls after all of them is done.

What the best way to do it? Is Fiber suited for that?


Source: (StackOverflow)

Do fibers have priorities?

Fibers are defined as lightweight threads, and threads have priorities because they are preemptively scheduled. However, since fibers are cooperative do they too have priorities?


Source: (StackOverflow)

"Meteor code must always run within a Fiber" when using Meteor.runAsync

Using cassandra with meteor.

let client = new cassandra.Client({contactPoints: [cassandraHost]});

var cassandraExecSync = Meteor.wrapAsync(client.execute, client);
MyProject.Feed.CassandraMeteorWrap = {
insertNewPost: function (userId, postContentJson, relevance) {
       var insertCommand = insert(userId, postContentJson, relevance);
       try {
           return cassandraExecSync(insertCommand);
       } catch (err) {
           console.log("error inserting: " + insertCommand);
           console.log(err);
       }
   }

So I wrapped Cassandra.client.execute (which has last arg as callback) with Meteor.wrapAsync

The first few inserts work, but after a few inserts (the insert is being called periodically) I get a:

[Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.]

Update: Debug meteor showed the stack trace and the exception starts from the npm package I use "cassandra-driver" on the .onTimeout():

function listOnTimeout() {
  var msecs = this.msecs;
  var list = this;

  debug('timeout callback ' + msecs);

  var now = Timer.now();
  debug('now: %d', now);

  var first;
  while (first = L.peek(list)) {
    // If the previous iteration caused a timer to be added,
    // update the value of "now" so that timing computations are
    // done correctly. See test/simple/test-timers-blocking-callback.js
    // for more information.
    if (now < first._monotonicStartTime) {
      now = Timer.now();
      debug('now: %d', now);
    }

    var diff = now - first._monotonicStartTime;
    if (diff < msecs) {
      list.start(msecs - diff, 0);
      debug(msecs + ' list wait because diff is ' + diff);
      return;
    } else {
      L.remove(first);
      assert(first !== L.peek(list));

      if (!first._onTimeout) continue;

      // v0.4 compatibility: if the timer callback throws and the
      // domain or uncaughtException handler ignore the exception,
      // other timers that expire on this tick should still run.
      //
      // https://github.com/joyent/node/issues/2631
      var domain = first.domain;
      if (domain && domain._disposed) continue;
      try {
        if (domain)
          domain.enter();
        var threw = true;
        **first._onTimeout();**
        if (domain)
          domain.exit();
        threw = false;
      } finally {
        if (threw) {
          // We need to continue processing after domain error handling
          // is complete, but not by using whatever domain was left over
          // when the timeout threw its exception.
          var oldDomain = process.domain;
          process.domain = null;
          process.nextTick(function() {
            list.ontimeout();
          });
          process.domain = oldDomain;
        }
      }
    }
  }

  debug(msecs + ' list empty');
  assert(L.isEmpty(list));
  list.close();
  delete lists[msecs];
}

Source: (StackOverflow)

Does D fiber has stack size limitation?

In C/C++, coroutines are implemented with stack-exchange hack, so stack-size is usually limited, doesn't grow automatically.

Does D Fiber has these limitations? Or does it grow automatically?


Source: (StackOverflow)

What to do after quitting in the middle of a fiber

Once I am done in the middle of a Fiber instance fiber, i.e., I yielded from it without completing it, and I am not using fiber any more, what should I do with it? Should I explicitly destroy it, or is there something like kill for a Fiber, or will it be garbage collected properly? If it is, then how does Ruby know whether I am going to fiber.resume in the future or not?


Source: (StackOverflow)

SEH setup for fibers with exception chain validation (SEHOP) active

I'm working on a native fiber/coroutine implementation – fairly standard, for each fiber, a separate stack is allocated, and to switch contexts, registers are pushed onto the source context stack and popped from the target stack. It works well, but now I hit a little problem:

I need SEH to work within a fiber (it's okay if the program terminates or strange things start to happen when an exception goes unhandled until the fiber's last stack frame, it won't). Just saving/restoring FS:[0] (along with FS:[4] and FS:[8], obviously) during the context switch and initially setting FS:[0] for newly allocated fibers to 0xFFFFFFFF (so that the exception handler set after the context switch will be the root of the chain) almost works.

To be precise, it works on all non-server Windows OSes I tested – the problem is that Windows Server 2008 and 2008 R2 have the exception chain validation (SEHOP, SEH overwrite protection) feature enabled by default, which makes RaiseException check if the original handler (somewhere in ntdll.dll) is still the root of the chain, and immediately terminates the program as if no handlers were installed otherwise.

Thus, I'm facing the problem of constructing an appropriate root frame on the stack to keep the validation code happy. Are there any (hidden?) API functions I can call to do that, or do I have to figure out what is needed to keep RtlDispatchException and friends happy and construct the appropriate _EXCEPTION_REGISTRATION entry myself? I can't just reuse the Windows-supplied one from the creating thread because it would be at the wrong address (the SEH implementation also checks if the handler address is in the boundaries given by FS:[4] and FS:[8], and possibly also if the address order is consistent).

Oh, and I'd strongly prefer not to resort to the CreateFiber WinAPI family of functions.


Source: (StackOverflow)

Image-processing basics

enter image description here

I have to do some image processing but I don't know where to start. My problem is as follows :-

I have a 2D fiber image (attached with this post), in which the fiber edges are denoted by white color and the inside of the fiber is black. I want to choose any black pixel inside the fiber, and travel from it along the length of the fiber. This will involve comparing the contrast with the surrounding pixels and then travelling in the desired direction. My main aim is to find the length of the fiber

So can someone please tell me atleast where to start? I have made a rough algorithm in my mind on how to approach my problem but I don't know even which software/library to use.

Regards Adi

EDIT1 - Instead of OpenCV, I started using MATLAB since I found it much easier. I applied the Hough Transform and then Houghpeaks function with max no. of peaks = 100 so that all fibers are included. After that I got the following image. How do I find the length now?

EDIT2 - I found a research article on how to calculate length using Hough Transform but I'm not able to implement it in MATLAB. Someone please help


Source: (StackOverflow)

Please explain the logic behind this ruby fiber example

The example code is from here:

def http_get(url)
  f = Fiber.current
  http = EventMachine::HttpRequest.new(url).get

  # resume fiber once http call is done
  http.callback { f.resume(http) }
  http.errback  { f.resume(http) }

  return Fiber.yield
end

EventMachine.run do
  Fiber.new{
    page = http_get('http://www.google.com/')
    puts "Fetched page: #{page.response_header.status}"

    if page
      page = http_get('http://www.google.com/search?q=eventmachine')
      puts "Fetched page 2: #{page.response_header.status}"
    end
  }.resume
end

So, in the context of the EM run block, the author's creating a fiber and running it immediately with resume. But, I don't understand why the http_get logic is structured in that way. I mean, it's taking the current fiber ( which in this case should be the one created in the EM run block ), it starts a http request which may fail or succeed, and it resumes the current fiber. Afterwards it just calls yield on the fiber. What exactly will be running since he is calling yield? Can someone please explain why http_get is written the way it is?


Source: (StackOverflow)