#Task and Msgs
4 messages in this thread
I have this little program. I'm trying to get two tasks to talk to each
other. I've tried every variation I can think of to fix this, and none of
them work. It manages to create the task and then guru's.
The listing for the program is:
MODULE t1;
FROM SYSTEM IMPORT
ADR, ADDRESS, BYTE, NULL, TSIZE, WORD;
FROM Ports IMPORT
Message, MessagePtr, MsgPortPtr, FindPort, WaitPort, GetMsg,
PutMsg, ReplyMsg;
FROM PortUtils IMPORT
CreatePort, DeletePort, CreateExtIO, DeleteExtIO;
FROM Tasks IMPORT
Task, TaskPtr, AddTask, FindTask, RemTask;
FROM Storage IMPORT
ALLOCATE, DEALLOCATE, CreateHeap, DestroyHeap;
FROM Memory IMPORT
AllocMem, FreeMem, MemReqSet, MemPublic, MemClear;
FROM Nodes IMPORT
NodeType;
FROM InOut IMPORT
WriteString, WriteLn;
FROM IO IMPORT
IORequest, IORequestPtr;
IMPORT Trapper;
TYPE
StackAreaType = ARRAY[0..2048] OF WORD;
taskproctype = PROCEDURE;
MyRequest = RECORD
mrReq : IORequest;
mrData : ARRAY[0..10] OF CHAR;
END;
MyRequestPtr = POINTER TO MyRequest;
VAR
Stack1, Stack2 : StackAreaType;
Task1, Task2 : TaskPtr;
i : LONGCARD;
pv1, pv2 : taskproctype;
Intercom : MsgPortPtr;
memreqs : MemReqSet;
MyData : ARRAY[0..20] OF CHAR;
MyMsg : MyRequestPtr;
quits : CARDINAL;
(*$P-*) PROCEDURE p1;
VAR
i : CARDINAL;
m : MyRequestPtr;
n : MessagePtr;
p : MsgPortPtr;
r : MsgPortPtr;
PROCEDURE WaitReply();
BEGIN
WHILE (GetMsg(r) = MessagePtr(0)) DO
n := WaitPort(r);
END;
END WaitReply;
<Continued next message>
Continued from last msg
BEGIN
p := FindPort("Intercom");
r := CreatePort("Mailbox1",0);
m := MyRequestPtr(CreateExtIO(r, TSIZE(MyRequest)));
FOR i := 1 TO 10 DO
m^.mrData := "p1 – OK.";
PutMsg(p,MessagePtr(m));
WaitReply();
END;
m^.mrData := "quit";
PutMsg(p,MessagePtr(m));
WaitReply();
DeleteExtIO(IORequestPtr(n),SIZE(n^));
DeletePort(r); END p1;
BEGIN
WriteString("Entering program"); WriteLn;
Intercom := CreatePort("Intercom", 0);
IF CreateHeap(32768) THEN
WriteString("Heap created…");
memreqs := MemReqSet{MemPublic, MemClear};
Task1 := TaskPtr(AllocMem(LONGCARD(TSIZE(Task)), memreqs));
WriteString("Task1 structure allocated…");
WITH Task1^ DO
WITH tcNode DO
lnType := BYTE(NTTask);
lnName := NULL;
END;
tcSPLower := ADR(Stack1);
tcSPUpper := ADR(Stack1[2048]);
tcSPReg := tcSPUpper – 4;
END;
pv1 := p1;
WriteString("Task1 structure initialized…");
AddTask(Task1, ADDRESS(pv1), NULL);
WriteString("Task1 added"); WriteLn;
FOR i := 1 TO 100000 DO END;
quits := 1;
REPEAT
MyMsg := MyRequestPtr(GetMsg(Intercom));
IF (MyMsg = MyRequestPtr(0)) THEN
MyMsg := MyRequestPtr(WaitPort(Intercom));
END;
WriteString("Received Msg");WriteLn;
WriteString(MyMsg^.mrData); WriteLn;
IF (MyMsg^.mrData[0] = "q") THEN INC(quits); END;
ReplyMsg(MessagePtr(MyMsg));
UNTIL (quits = 2);
FOR i := 1 TO 100000 DO END;
FreeMem(Task1, LONGCARD(TSIZE(Task)));
WriteString("Task1 structure freed…");
DestroyHeap;
WriteString("Heap destroyed"); WriteLn;
DeletePort(Intercom);
END; END t1.
Any help will be appreciated.
MCR – San Jose, CA
Rather than have me try to run it to see what it does (or does NOT) do,
why don't you tell me what you think is wrong. BTW, at first glance I don't
like your waitreply() routine at all.
I'm having a little trouble following the code "online" but a couple things
possibly: Why did you eliminate the additional procedure code using the
(*$P-*)? It should work fine with it. Also, make sure your task doesn't try
to do any AmigaDOS. That causes a crah (er, crash) for sure. I've got a few
working task examples around (I keep meaning to write something up showing
how to add tasks) and they all work fine. It really takes more coding to get
a task going then a process. AmigaDOS/Exec take care of some oe overhead for
processes.