MOS, C & tasks. Programming help, please!
  • Order of the Butterfly
    Order of the Butterfly
    maurensen
    Posts: 358 from 2003/10/3
    From: Padova - Italy
    Aloha!
    Now in my self-made Amiga C programming course I've some problem with creating a Task.
    This example is cut'n'pasted from Amiga RKM 2.1 (sorry, I've only this version!):

    #include <exec/types.h>
    #include <exec/memory.h>
    #include <exec/tasks.h>
    #include <libraries/dos.h>
    #include <clib/exec_protos.h>
    #include <clib/alib_protos.h>
    #include <stdlib.h>
    #include <stdio.h>
    #define STACK_SIZE 1000L
    struct Task *task = NULL;
    char *simpletaskname = "SimpleTask";
    ULONG sharedvar;
    void simpletask(void);
    void cleanexit(UBYTE *,LONG);
    int main(int argc,char **argv)
    {
    sharedvar = 0L;
    task = CreateTask(simpletaskname,0,simpletask,STACK_SIZE);
    if(!task) cleanexit("Can't create task",RETURN_FAIL);
    printf("This program initialized a variable to zero, then started a\n");
    printf("separate task which is incrementing that variable right now,\n");
    printf("while this program waits for you to press RETURN.\n");
    printf("Press RETURN now: ");
    getchar();
    printf("The shared variable now equals %ld\n",sharedvar);
    /* We can simply remove the task we added because our simpletask does not make */
    /* any system calls which could cause it to be awakened or signalled later. */
    Forbid();
    DeleteTask(task);
    Permit();
    cleanexit("",RETURN_OK);
    }
    void simpletask()
    {
    while(sharedvar < 0x8000000) sharedvar++;
    /* Wait forever because main() is going to RemTask() us */
    Wait(0L);
    }
    void cleanexit(UBYTE *s, LONG e)
    {
    if(*s) printf("%s\n",s);
    exit(e);
    }

    I think the little proggy is self explanatory. It should run a task that
    increases a global dummy variable and when the user hits return the main task kill
    the subtask and return the value of the variable.

    The problem is that MOS SDK compilation is ok but when I run the proggy
    after waiting some second before pressing RETRUN i get the value of
    "sharedvar" always equal 0!
    It seems the subtask never starts...
    Could you help me please? It's a sort of incompatibility between MOS and AOS?
    And yes how can start a task in MOS? I'm not able to find an example in the
    SDK...
    Thanx in advance and ciao!
    P.s: as I'm posting, I'd like to thanx the equipe @
    GURU-MEDITATION.net for their beautiful MUI tutorial!
    It's in french but I think it's very understandable
    for all!!! Than U very much for your support guys!
    -------------------
  • »06.10.04 - 18:59
    Profile
  • MorphOS Developer
    itix
    Posts: 1520 from 2003/2/24
    From: Finland
    Not sure, but I think your subtask crashed right after starting... this because AddTask(), CreateNewProc() and others default to 68k.

    Try this:

    CreateNewProc(
    NP_Entry, simpletask,
    NP_Name, simpletaskname,
    NP_PPCStackSize, STACK_SIZE,
    NP_CodeType, CODETYPE_PPC,
    TAG_DONE);

    Explanations:

    NP_PPCStackSize is a stack size for PPC programs (defaults to 16K).
    NP_CodeType is used to tell your code is in PPC (defaults to 68k type).
    1 + 1 = 3 with very large values of 1
  • »06.10.04 - 19:53
    Profile
  • Order of the Butterfly
    Order of the Butterfly
    maurensen
    Posts: 358 from 2003/10/3
    From: Padova - Italy
    Thanx 4 the reply Itix but I'm sorry I have to say that also calling
    newproc as you suggested still doesn't work. I get always a value of
    0.
    :-(
    Damn MOS dokumentation !!!!
    -------------------
  • »08.10.04 - 19:24
    Profile
  • HAK
  • Order of the Butterfly
    Order of the Butterfly
    Posts: 225 from 2003/2/24
    From: Austria, Vienna
    Hi maurensen,

    Quote:

    And yes how can start a task in MOS? I'm not able to find an example in the SDK...


    Maybe I understood you wrong, but if you have installed the MOS-SDK there is a drawer Documentation/Examples/Multithreading which might help you in this case.


    Bye HAK
  • »09.10.04 - 00:21
    Profile
  • MorphOS Developer
    itix
    Posts: 1520 from 2003/2/24
    From: Finland
    Ok. Try this:

    change

    ULONG sharedvar;

    to

    volatile ULONG sharedvar;

    (Btw you can check from Scout if your subtask is running or it crashed/freezed.)
    1 + 1 = 3 with very large values of 1
  • »09.10.04 - 02:43
    Profile