Welcome, Guest. Please login or register.

Author Topic: Execute, System or RunCommand ?  (Read 2687 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Execute, System or RunCommand ?
« on: June 11, 2005, 01:00:49 PM »
Hi. Seems that I'm hijacking this forum all the way:-D

Here's the deal, I'm using CreateNewProc to set up a separate process that will handle an input buffer asychronously for better efficiency and multitasking. For now I haven't done any GUI so I was hoping to use some printf's from the new created process to be sure things are going ok. The darn thing refuses to printf to the Shell eventhough the Execute function I have inside it prints the output of a command correctly.
The code I'm using to start it is:

Code: [Select]
BOOL LoadBufferProcess (BOOL Ini_or_CleanUp)
 {
 BPTR bufferprocessSeglist = 0;
 static struct TagItem TagsforCreateNewProc[6];
 
 if (Ini_or_CleanUp)
   {if (!(bufferprocessSeglist = LoadSeg ("MDBuffer")))
     {printf("Couldn't load MD Buffer..\n");
      return (FALSE);}
    TagsforCreateNewProc[0].ti_Tag = NP_Seglist;
    TagsforCreateNewProc[0].ti_Data = bufferprocessSeglist;
    TagsforCreateNewProc[1].ti_Tag = NP_Name;
    TagsforCreateNewProc[1].ti_Data = (ULONG)"MDBuffer";
    TagsforCreateNewProc[2].ti_Tag = NP_Cli;
    TagsforCreateNewProc[2].ti_Data = TRUE;
  /*TagsforCreateNewProc[3].ti_Tag = NP_Output;*/
  /*TagsforCreateNewProc[3].ti_Data = Output();*/
    TagsforCreateNewProc[3].ti_Tag = NP_CloseOutput;
    TagsforCreateNewProc[3].ti_Data = FALSE;
    TagsforCreateNewProc[4].ti_Tag = TAG_DONE;
    if(!CreateNewProc (TagsforCreateNewProc))
      printf("Couldn't create MDBuffer Process..\n");
    else
      {printf ("MDBuffer now active...\n");
       return (TRUE);
      }
    }
 return (FALSE);
 }



Here another one:
I'm a bit undecided as to wheter go for Execute() or RunCommand() or even System().
From the DevCD: the first one doesn't give any comnmand output. System is similar but doesn't read input from a filehandle and Creates a new process (does the first one also does that?)
And finally there's RunCommand wich uses the current process to run the command, but the docs say nothing about output.
The last 2 have the big advantage of returning the commands return value so error checking can be done.

Questions:
- What's the point in having the command executed with CreateNewProc if it only returns when it finishes anyway ?

- I'd go for RunCommand but how can I know or define how it's output is handled ? I suppose since it uses the current process it uses it's output window, console process, etc?

- Final is the Cli structure really necessary (on the above code) ?

\\"We made Amiga, they {bleep}ed it up\\"
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
Dammit, I must refrain from posting so quickly, I have some commented lines on the code and that's the prblem :oops:

But the last buch of questions still holds... :-)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Using printf from two processes simultanously can be dangerous (stdio isn't necessarily re-entrant). I'd recommend using kprintf and debug.lib (sas/c) or libdebug.a (gcc).

Quote
- What's the point in having the command executed with CreateNewProc if it only returns when it finishes anyway ?

CreateNewProc creates new process that runs 100% asyncronous from the parent. CreateNewProc returns as soon as the subproc has been launched, it does not wait for it to finish. Be careful to sync the start and exit or you will have random problems.

Here's an example of subprocess and IPC:
http://www.iki.fi/sintonen/src/ipc/
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2871
    • Show only replies by Jose
@Piru
Thx :-)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Thomas

Re: Execute, System or RunCommand ?
« Reply #4 on: June 11, 2005, 02:43:54 PM »

You don't need the CLI structure.

If you remove NP_Output you should remove NP_CloseOutput, too.

You can use Printf (with a capital "P") for debug output into your Shell window. You shouldn't use ANSI functions at all when working with sub processes. AmigaOS is full of functions you can use safely instead.

Bye,
Thomas

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Execute, System or RunCommand ?
« Reply #5 on: June 12, 2005, 01:50:57 AM »
@Thomas
Quote
You can use Printf (with a capital "P") for debug output into your Shell window. You shouldn't use ANSI functions at all when working with sub processes. AmigaOS is full of functions you can use safely instead.

I have to correct you here: dos.library I/O buffering isn't thread safe either (including Printf).

Obviously if you open new console and Printf to it, it will be safe.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: Execute, System or RunCommand ?
« Reply #6 on: June 12, 2005, 11:20:56 AM »
@Piru

Aye, I can confirm that what with all the mucking about creating my C++ Threadable service class. I used a Process as the basis since you cannot guarentee in advance what objects may need to do some IO.

dos.library was no different to stdio when it came to concurrent accesses. Still, this is why I also have Lockable...
int p; // A