Where is the problem with my code? Trying to cooperatively multitask using setjmp.

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all, thanks for your time.

 

There's some code on a wikipage which I'm trying to run, but there's some problem with.. Something. Please check the link to wikipedia.

http://en.wikipedia.org/wiki/Set...

 

I can't really tell if it's incomplete or not (some of the comments in the code would imply this though) but for some reason it won't function on my W8.1Pro x64, I'm using gcc version rubenvb 4.6.3 if that's any help. The output from the program is (within quotes):

"Child loop begins

Parent"

then it crashes. One output string is missing, it crashes before "Child loop end" is written.

 

The weird thing is though, that the code, modified for the Atmel ATmega328p (on the Arduino Uno) to blink, works. It context switches without a glitch, at least from what I can tell. I'm trying to extend the two task to three, to begin with (I know you can do everything with a queueing system for an arbitray amount of threads but I'm not used to C so for now I'll do it caveman style), and I do get the correct output when running it, except it crashes as usual. But unlike before, this does not work on the AVR, so for now I wan't to get to the bottom of this simpler wikipedia example, and that's where YOU come in and save the day!

 

Basically it's four thirty in the morning and I should sleep, I'll run the example code on my other computer tomorrow but I thought that if you can directly say that it's not my computer's fault but my own, please do tell.

sol i sinne - brun inne

Last Edited: Wed. Dec 31, 2014 - 03:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you think it's possible that the AVR-program crashes exactly like the one on the computer, but then resets the board and starts over again? Because that would yield the same result as the actual meaning of the program, unfortunately..

sol i sinne - brun inne

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote a tutorial on co-operative multitasking.

https://www.avrfreaks.net/forum/t...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Alright, I don't know why it doesn't work on my comp (but it's C so who knows, I'm used to Haskell so imagine the leap...) but it works on the board. It swithes between three different tasks in this case.

Here's the code for it. I removed everything which is specific to my solution, this is the general version which should work on all boards. What you will have to add is probably some setup method, and methods which actually do work. The context switching works in a round-robin fashion.

 

#include <util/delay.h>
#include <avr/io.h>
#include <setjmp.h>
#include <stdio.h>

 

jmp_buf mainTask, childTask, childTask2;

 

void child(void);
void child2(void);

 

void call_with_cushion (void) {
    volatile char space[1000]; /* Reserve enough space for main to run */    //------- added "volatile"
    //space[999] = 1; /* Do not optimize array out of existence */            // ------ uncommented
    child();
}

void call_with_cushion2 (void) {
    volatile char space[1000]; /* Reserve enough space for main to run */    //------- added "volatile"
    //space[999] = 1; /* Do not optimize array out of existence */            // ------ uncommented
    child2();
}
 
int main(void) {

    // run setup method here
    
    if (!setjmp(mainTask)) {
        call_with_cushion();
    }
    for (;;) {
        /* THIS IS WHERE YOU EXECUTE YOUR TERMINATING CODE, FIRST TASK */
        if (!setjmp(mainTask)) longjmp(childTask, 1);
    }
}

 

void child (void) {
    if (!setjmp(childTask)) {
        call_with_cushion2();
    }
    for (;;) {
        /* THIS IS WHERE YOU EXECUTE YOUR TERMINATING CODE, SECOND TASK */
        if (!setjmp(childTask)) longjmp(childTask2, 1);
    }
}

 

void child2 (void) {
    for (;;) {
        /* THIS IS WHERE YOU EXECUTE YOUR TERMINATING CODE, THIRD TASK */
        if (!setjmp(childTask2)) longjmp(mainTask, 1);
    }
}

 

It seems to work fine even without "reserving space", but I'm no C expert so I'll leave it like it was (what about malloc?).

sol i sinne - brun inne

Last Edited: Wed. Dec 31, 2014 - 02:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"That's a lovely dress Mrs Cleaver". Eddie Haskell, circa '58

 

HaHa. Now for the stupid question... I selected the wiki example and tried to save it in a .c file called setjmptest.c, but its all one line in wordpad, which usually does lf to crlf sort of interlligently. Can one of you unixy guys tell me how to cutnpaste a c example from wikipedia? Very frustrating.

 

 

Imagecraft compiler user

Last Edited: Wed. Dec 31, 2014 - 06:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bobgardner wrote:
Can one of you unixy guys tell me how to cutnpaste a c example from wikipedia? Very frustrating.
I can't reproduce the problem.

 

Mind you, all I have to try with is a Win2KPro VM with IE 6.0 and FF 3.6.16.  I drag-selected some code from the OP's wikipedia link, and pasted into notepad.  No problem with line breaks.

 

Are you pasting into notepad?   Or some other editor?

 

I'd guess it's some muckup with CR and LF.  I would try search/replace for CR (i.e. '\r') or LF (i.e. '\n') in your editor (after pasting), and replace with CR/LF, or some such.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bob, why didn't you just paste it into Notepad, it worked there.  I recommend that you get and use Notepad++ as a replacement for Notepad.  Try it, you'll like it!wink

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Wed. Dec 31, 2014 - 07:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, you guys are right. It cutnpasted right into notepad. Both examples from the wiki page run exactly as advertised. Prints out just like in the commented prediction. (maybe its the excellent compiler I use)

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
but its all one line in wordpad
Ah, missed that.

 

However, it still doesn't break.  Perhaps the Win2KPro/IE6/FF3.6.16 combo doesn't suffer the same fate as your Windows 8 (7?) machine...

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hah yeah that was an eye opener for me too, I'm using this massive OS (windows) and I can't even create a file?! But from the command window, or terminal or whatever you may call it you can type for instance "NUL > whatever.youwant" and it will create a file in the current directory, a blank file if you don't specify an extension I think..

sol i sinne - brun inne

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You're correct, I tried it on my linux machine (god damnit I hate linux, I'm off to the drug store to buy some ulcer meds) and it works, the wiki example code that is, also without "reserving space". It must be my windows that's not doing what it's supposed to. Anyhow, the code works both on a real computer and on the Arduino so who gives a darn.

sol i sinne - brun inne