26.19Useful variables. 4tH 4tH Forth compiler
Reklaam
Reklaam
CHAPTER 26. USING THE 4TH API 511
}
/* set all slots to NULL */ for (x = 0; x < MAX_TASK; x++) Processes [x] = NULL;
/* now load two processes */ printf ("Process %ld installed\n", task_4th (Processes, load_4th ("1.hx"))); printf ("Process %ld installed\n", task_4th (Processes, load_4th ("2.hx"))); multi_4th (Processes); return (EXIT_SUCCESS);
/* start the multitasker */
/* return success */
Since load_4th() returns a NULL pointer when it doesn’t succeed, it is safe to pass it to task_4th(). Even if programs aren’t suited to do any multitasking, you can use this program: they will just be executed consecutively. task_4th() will add a program to the process list. wake_4th() will try to wake up a program beginning with the PID which is passed to it. Note that the PID is passed to the 4tH program, so it can be queried. If ’OUT’ contains a valid PID, it will be the next process that the program will try to awaken. When a process terminates, it is taken from the process list. When all processes have terminated, wake_4th() returns CELL_MIN. Note that a program can terminate the multitasker by returning a non-zero value. That’s as fancy as it gets, folks!
The String Segment and the Code Segment are read-only for a reason. Although you can access them, we advise you not to attempt it. The same goes for the Stack Area and the
System Area. The interfaces we’ve provided here are a relatively safe way to exchange information between 4tH and C, but if you make any errors your program might crash.
Therefore, it is a good idea to add exception handlers to critical sections of your 4tH program.
26.19
Useful variables
We’ve already seen that dump_4th() can provide you with a lot of information about
Hcode. If you need this information, you don’t have to call dump_4th(). The dump_4th() function simply uses the information that is already available. This small program shows you how to obtain it:
Hcode *Program;
Program = comp_4th (strdup (".\" Hello world\" cr")); if (Program == NULL) else { printf ("Not enough memory\n"); printf ("Error# : %u\n", Program->ErrNo); printf ("Error at word: %d\n", Program->ErrLine); printf ("Object size : %d\n", Program->CodeSiz); printf ("String size : %u\n", Program->StringSiz); printf ("Var. offset : %u\n", Program->Offset); printf ("Variables : %u\n", Program->Variables); printf ("Strings printf ("Reliable
: %u\n", Program->Strings);
: %s\n", Program->Reliable ? "Yes" : "No");
}
The labels are kept the same as in dump_4th(), so if you need more information, read that section again.
Laadi alla
Reklaam