emulating context-switch

Discussion in 'C++' started by refigh, Aug 10, 2011.

  1. refigh

    refigh Guest

    hi every body,
    I have a embedded board with a processor without OS on it. there are
    some independent tasks which should be run always. I put them into a
    while(true) loop and call them one after another i.g.

    while(true){
    task(1);
    task(2);
    task(3);
    }

    by above code it seems they are run concurrently.

    sometimes one of these task takes too much time and then causes
    problem for other tasks. I want to emulate context switch in c++. the
    method I consider to implement is:
    I divide each task to some reasonable parts and after reach any of
    these checkpoints, a task should return and a task_manager in body of
    main function select another task to continue( for example by a
    priority mechanism ).
    but I think pausing a function is not easy? am I right? I think I must
    define many variables as static and also save the inputs of function
    for call a paused function again,
    is there a simpler method in assembly? does anybody has a better
    suggestion?
    ( I should mention that it is not possible to use interrupts for me
    also. )
    thanks.
    refigh, Aug 10, 2011
    #1
    1. Advertising

  2. refigh

    Bo Lorentsen Guest

    On 08/10/2011 02:31 PM, refigh wrote:

    > I divide each task to some reasonable parts and after reach any of
    > these checkpoints, a task should return and a task_manager in body of
    > main function select another task to continue( for example by a
    > priority mechanism ).
    > but I think pausing a function is not easy?

    No, you need to use something like async IO and longjmp to manage this,
    and you need to manage an individual stack for each task, in order to
    make all local values work.

    And yes this is an cooperative model, and one task that does hand in a
    long running loop will stop all other tasks while looping (thats why we
    used "yield()" in win16 :)

    > am I right? I think I must
    > define many variables as static and also save the inputs of function
    > for call a paused function again,
    > is there a simpler method in assembly?

    The local value problem are fixed by setting up a stack per thread, and
    this goes for function arguments too, as these are all stack based i C
    and C++.

    > does anybody has a better
    > suggestion?
    > ( I should mention that it is not possible to use interrupts for me
    > also. )

    This would be the best way to task switch, by giving time slices to each
    task (using an interrupt for this), but .... depending on the platform
    you are writing for/on, it may be more sensible to use your energy on
    porting a custom Linux kernel to the platform, instead of reinventing
    the wheel.

    Else take a look at http://en.wikipedia.org/wiki/List_of_operating_systems

    Lastly, I am not sure this it the prober news group for this kind of
    question (nor answer) :)

    /BL
    Bo Lorentsen, Aug 10, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VS_NET_DEV
    Replies:
    2
    Views:
    3,799
    jenny
    May 25, 2004
  2. =?Utf-8?B?U3VuU21pbGU=?=
    Replies:
    0
    Views:
    704
    =?Utf-8?B?U3VuU21pbGU=?=
    Jan 10, 2006
  3. Flip Rayner
    Replies:
    1
    Views:
    652
    bruce barker
    Jan 23, 2007
  4. asd
    Replies:
    1
    Views:
    433
    www.pulpjava.com
    Nov 9, 2006
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    106
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page