[comp.sys.amiga] Processes and the Like.

derick@garfield.UUCP (Derick Linegar) (03/07/88)

[	Is  comp.sys.amiga.tech official or what ?? 	]


Hi,
	I would like to know how to set up mutliple processes within a program 
	under MANX. All the code examples I have seen so far in the literature
	uses LATTICE. (Nice example, Rob Peck .. ) And since I do not own
	lattice, can anyone supply me an example???

	E-Mail me if not appropiate for Usenet..!!

derick

UUCP:	derick@garfield.UUCP
CDN:	derick@garfield.mun.cdn
EDU:	derick@garfield.mun.edu
CANADA GHOST:	Derick Linegar, 12A Fleming Str. St John's Nfld. Canada A1C 3A2
	

shimoda@rmi.UUCP (Markus Schmidt) (03/09/88)

In article <4561@garfield.UUCP> derick@garfield.UUCP (Derick Linegar) writes:
: 	I would like to know how to set up mutliple processes within a program 
: 	under MANX. All the code examples I have seen so far in the literature
: 	uses LATTICE. (Nice example, Rob Peck .. ) And since I do not own
: 	lattice, can anyone supply me an example???
: 
Here u are:

main()
{
     void AnotherTask();

     .... some code ...

     CreateTask("This is the Name", 0L, AnotherTask, 4000L);
     /*           name            prior., code,       stacksize */

    .... other code
 
    while (FindTask("This is the Name"))
          Delay(50L);
}

void AnotherTask()
{
    int this, that;
     char foo;

     geta4();    /* THAT"S IMPORTANT IF YOU HAVE SMALL CODE ADDRESSING */

     ... taskstuff ...
}



It is also described somewhere in the Manx manuel chapter 
"tech.info".

Hope to have helped you!

Markus
(shimoda@rmi.uucp)

Ahh: I nearly forgot: The last lines in main() are to make sure,
that the task has completet before your main program is finished.
If you would finish without doin  that, the task would run in 
unallocated memory, and that is a really sure way to meditation!

conn@stratus.UUCP (Avery Shealey) (03/11/88)

In article <913@rmi.UUCP> shimoda@rmi.UUCP (Markus Schmidt) writes:
>In article <4561@garfield.UUCP> derick@garfield.UUCP (Derick Linegar) writes:
>: 	I would like to know how to set up mutliple processes within a program 
>: 	under MANX. All the code examples I have seen so far in the literature
>: 	uses LATTICE. (Nice example, Rob Peck .. ) And since I do not own
>: 	lattice, can anyone supply me an example???
>: 
>Here u are:
>
>main()
>{

Deleted code for creating tasks

>}
>

I think he meant processes, not tasks. I have been working on this for
several weeks (in between classes). 

I have had some luck. To set it up, I faked a CommandLineInterface
and set up any fields in the process structure that looked like they
would not be set by CreateProc().

I was able to load a process and get I/O but there are
a couple of quirks. The process that was loaded closes its output window
even though the startup code for Manx says that it will not.

If the process that you want to load is not linked with +a (or -a)
option to align on longword boundaries - hello mister guru.

Also, I cannot figure out how to detect that the process has ended. There
is no reply message since the process thinks it was a CLI, and the state
information in the task structure is set to wait when it ends.

The other quirk was that if I tried to call DupLock() on the variables
  myproc->pr_CurrentDir or  mycli->cli_CommandDir
I always get a guru at that line.

If any of you gurus out there see anything, please let me know.

Thanks, 
Avery
======================================================
code for pr.c
======================================================
#include "exec/ports.h"
#include "exec/tasks.h"
#include "exec/memory.h"
#include "intuition/intuition.h"
#include "libraries/dosextens.h"
#include "stdio.h"
#include "workbench/startup.h"

#define BTOC(ptr) ((void *)((long)(ptr) << 2))
#define CTOB(ptr) ((void *)((long)(ptr) >> 2))

extern char *btocstr();
extern BSTR ctobstr();

main()
{
  BPTR window;
  BPTR Open(), Lock(), DupLock();
  BPTR newseg, LoadSeg();
  int i, j;
  LONG myargs[8], nargs, res1;
  struct CommandLineInterface *cli, *mycli;
  struct FileHandle *con;
  struct InfoData *id;
  struct Message *reply, *GetMsg();
  struct MsgPort *proc;
  struct MsgPort *replyport;
  struct MsgPort *CreateProc(), *CreatePort();
  struct Process *myproc, *newproc;
  struct Task *FindTask();
  struct WBStartup *startup;
  void *AllocMem();


  replyport = CreatePort(NULL, 0);

  if ((newseg = LoadSeg("test")) == 0) {
    printf("unable to load segment\n");
    exit(10);
  }

  if ((proc = CreateProc("test", 0, newseg, 5000)) == 0) {
    printf("unable to create process\n");
    UnLoadSeg(newseg);
    exit(20);
  }

  myproc = (struct Process *) FindTask(0);
  mycli = (struct CommandLineInterface *) BTOC(myproc->pr_CLI);
  newproc = (struct Process *) ((int)proc - sizeof(struct Task));

  window = Open("CON:0/0/400/100/Test", MODE_NEWFILE);

  cli = AllocMem(sizeof(struct CommandLineInterface),MEMF_PUBLIC|MEMF_CLEAR);
  cli->cli_SetName = mycli->cli_SetName;
  cli->cli_CommandDir = Lock("C:", ACCESS_READ);
  cli->cli_CommandName = ctobstr("test");
  cli->cli_FailLevel = mycli->cli_FailLevel;
  cli->cli_StandardInput = cli->cli_StandardOutput = window;
  cli->cli_CurrentInput = cli->cli_CurrentOutput = window;
  cli->cli_Background = FALSE;
  cli->cli_Interactive = FALSE;
  cli->cli_CommandFile = cli->cli_Prompt = (BPTR) NULL;
  cli->cli_Module = newseg;

  newproc->pr_CLI = (BPTR) CTOB(cli);
  newproc->pr_CIS = newproc->pr_COS = window;
  newproc->pr_TaskNum = (long) 1;
  newproc->pr_CurrentDir = Lock("vdk:", ACCESS_READ);
  newproc->pr_FileSystemTask = myproc->pr_FileSystemTask;

  con = (struct FileHandle *) BTOC(window);
  newproc->pr_ConsoleTask = (APTR) con->fh_Type;

  id = AllocMem(sizeof(struct InfoData), MEMF_CLEAR|MEMF_PUBLIC);
  myargs[0] = ((ULONG) id) >> 2;
  nargs = 1;
  res1 = (LONG) sendpkt (con->fh_Type, ACTION_DISK_INFO, myargs, nargs);

  newproc->pr_WindowPtr = (APTR) id->id_VolumeNode;

  startup = AllocMem(sizeof(struct WBStartup), MEMF_CLEAR|MEMF_PUBLIC);
  startup->sm_Message.mn_ReplyPort = replyport;
  startup->sm_Message.mn_Length = sizeof(struct WBStartup);
  startup->sm_Message.mn_Node.ln_Name = "startup";
  startup->sm_NumArgs = 0;
  startup->sm_ArgList = NULL;
  startup->sm_ToolWindow = NULL;

  PutMsg(proc, startup);

  while (FindTask("test") != NULL)
    Delay(50);

  FreeMem(cli, sizeof(struct CommandLineInterface));
  FreeMem(startup, sizeof(struct WBStartup));
  FreeMem(id, sizeof(struct InfoData));
  UnLoadSeg(newseg);
  DeletePort(replyport);
  Close(window);
}

LONG sendpkt(pid, action, args, nargs)
struct MsgPort *pid;
LONG action, args[], nargs;
{
  LONG count, *pargs, res1;
  struct MsgPort *replyport;
  struct MsgPort *CreatePort();
  struct StandardPacket *packet;

  replyport = CreatePort(NULL, 0);

  packet = AllocMem(sizeof(struct StandardPacket), MEMF_PUBLIC|MEMF_CLEAR);

  packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
  packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
  packet->sp_Pkt.dp_Port = replyport;
  packet->sp_Pkt.dp_Type = action;

  pargs = &(packet->sp_Pkt.dp_Arg1);
  for (count = 0; count < nargs; count++)
    pargs[count] = args[count];

  PutMsg(pid, packet);

  WaitPort(replyport);
  GetMsg(replyport);

  res1 = packet->sp_Pkt.dp_Res1;

  FreeMem(packet, sizeof(struct StandardPacket));
  DeletePort(replyport);

  return(res1);
}
======================================================
code for btoc.c
======================================================
#include <exec/types.h>
#include <exec/memory.h>

typedef long BPTR;
typedef long BSTR;

#define BTOC(ptr) ((void *)((long)(ptr) << 2))
#define CTOB(ptr) ((void *)((long)(ptr) >> 2))

char *btocstr(bstr)
BSTR bstr;
{
  char *t;
  int len;

  t = (char *) BTOC(bstr);
  len = (int) *(t++);
  *(t + len) = '\0';
  return (t);
}

BSTR ctobstr(cstr)
char *cstr;
{
  char *t;
  void *AllocMem();

  t = AllocMem(strlen(cstr), MEMF_CLEAR | MEMF_PUBLIC);
  t[0] = strlen(cstr);
  strcpy(t+1, cstr);
  return((BSTR) CTOB(t));
}
======================================================
code for test.c
======================================================
#include "stdio.h"

main()
{
   printf("test of processes\n");
   Delay(250);
}
======================================================

Avery Shealey
School of Information & Computer Science, Georgia Tech, Atlanta GA 30332
Internet:  conn@stratus.gatech.edu	 CSNet:  conn%stratus@gatech	
UUCP:  ...!{akgua,allegra,amd,hplabs,seismo,ihnp4}!gatech!stratus!conn