Article View: comp.sys.amiga.tech
Article #12781need help with tasks
From: /
Date: Fri, 19 May 1989 04:23
Date: Fri, 19 May 1989 04:23
111 lines
3031 bytes
3031 bytes
Help! When I run the code below I meet the Guru! Can anyone help me out?
I am trying to write a program that starts up and creates several tasks.
The C program has all of the code for each of the tasks. After the
main prcedure starts the tasks I want it to wait for all of the tasks to
finish. After all tasks have finished the main program should remove them
and exit. My first attempt is shown below. I wrote a simple semaphore
server process so that the task can signal the main program when it is
done. Everything seems to work fine until I run AddTask. At that point
my system gurus. What am I doing wrong? Do I need to use something other
than the address of the procedure in AddTask? I am using the MANX C compiler
with sdb. Any ideas would be appreciated!
I also have a question on how a task can wait to be deleted. I would like
to just put it to sleep. The only way I could think of to do this is to
wait on a signal that will never happen. Is there a better way?
#include <exec/memory.h>
#include <exec/tasks.h>
#include <functions.h>
#include <stdio.h>
#include "semaphore.h"
#define STACK_SIZE ((long) 1000)
long uid = 0;
void task()
{
long signal;
signal = AllocSignal((long) -1);
semaphore_V(uid);
if (signal != -1) {
Wait(signal);
} else {
/* this is bad, how else can we wait to be removed? */
while (TRUE);
}
}
struct Task *start_task()
{
struct Task *p_task;
APTR p_stack;
p_stack = AllocMem(STACK_SIZE, MEMF_CLEAR);
if (! p_stack) {
/* error: not enough memory */
return NULL;
}
p_task = AllocMem((long) sizeof(struct Task), MEMF_CLEAR |
MEMF_PUBLIC);
if (! p_task) {
/* error: not enought memory */
FreeMem(p_stack, STACK_SIZE);
return NULL;
}
p_task->tc_Node.ln_Type = NT_TASK;
p_task->tc_Node.ln_Name = "task";
p_task->tc_SPLower = p_stack;
p_task->tc_SPUpper = p_stack + STACK_SIZE;
p_task->tc_SPReg = p_task->tc_SPUpper;
AddTask(p_task, task, NULL);
return p_task;
}
void stop_task(p_task)
struct Task *p_task;
{
RemTask(p_task);
/*
FreeMem(p_task->tc_SPLower, STACK_SIZE);
FreeMem(p_task, (long) sizeof(struct Task));
*/
}
main()
{
struct Task *p_task;
long uid;
if (! semaphore_init()) {
fprintf(stderr, "can't init semaphores...\n");
exit(1);
}
uid = semaphore_new(0L);
if (! uid) {
fprintf(stderr, "can't create a new semaphore...\n");
exit(1);
}
p_task = start_task();
semaphore_P(uid);
stop_task(p_task);
semaphore_delete(uid);
semaphore_term();
}
--
Denis Bohm (usenet: decwrl!vlsisj!denis)
Technical Details
Message-ID:
<15230@vlsisj.VLSI.COM>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!ames!amdahl!pyramid!leadsv!practic!vlsisj!denisShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!ames!amdahl!pyramid!leadsv!practic!vlsisj!denis From: denis@vlsisj.VLSI.COM (/) Newsgroups: comp.sys.amiga.tech Subject: need help with tasks Message-ID: <15230@vlsisj.VLSI.COM> Date: 19 May 89 04:23:25 GMT Reply-To: denis@vlsisj.UUCP () Organization: VLSI Technology Inc., San Jose, CA Lines: 111