ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 21 Jun 2016 09:10:51 +0200Executing Sage with arguments from C with exec familyhttps://ask.sagemath.org/question/33795/executing-sage-with-arguments-from-c-with-exec-family/Hi, I'm writing a piece of software that lets me distribute executions of the type *same program-multiple data* in a computing server through PVM. This software has a module for Maple executions, one for C, one for Python and one for Pari. Now I'm trying to write the code for a Sage module.
The idea is that i have a master who sends tasks to several slaves, each task consists on executing the same Sage script but using different values for the variables `taskId` and `taskArgs`. The slave forks a process and executes a chunk such as this one:
// NULL-terminated array of strings
char **args;
int nargs=2;
args = (char**)malloc((nargs+1)*sizeof(char*));
// Do not malloc for NULL
for (i=0;i<nargs;i++)
args[i] = malloc(BUFFER_SIZE);
// Fill up the array with strings
sprintf(args[0],"sage");
sprintf(args[1],"-c \"taskId=%d;taskArgs=[%s];load('%s');\"",taskNumber,arguments,inp_programFile);
args[2] = NULL;
// Call the execution and check for errors
err = execvp(args[0],args);
perror("ERROR:: child Sage process");
exit(err);
For example, an instance of this code would be executing
`sage -c "taskId=71;taskArgs=[0,73,74,0];load('test.sage')"`,
where *test.sage* prints the arguments or whatever.
Now comes the question. If i execute the previous command from the command line i get the expected result (the two prints). However, if i execute my C program (which works corectly for Maple, C, Python and Pari using a very similar approach) i get this error:
`sage-run received unknown option: -c "taskId=71;taskArgs:=[0,73,74,0];load('test.sage')"`
I have already tried changing the double quotes `"` to single quotes `'`, using only a single, very simple argument from C (e.g. `sage -c "print(1);"`), all to no avail.
Am I missing something? How can the same command work from the command line but not when i use `execvp` from within C?
Thanks in advanced for any help!
----------
(I know I could adapt my Python module to read Sage scripts, but i want a *standalone* module for Sage because the Python one uses `sys.args` and I'd like to keep the Sage scripts as simple as possible for the users of the software)Wed, 15 Jun 2016 14:16:59 +0200https://ask.sagemath.org/question/33795/executing-sage-with-arguments-from-c-with-exec-family/Answer by dgulotta for <p>Hi, I'm writing a piece of software that lets me distribute executions of the type <em>same program-multiple data</em> in a computing server through PVM. This software has a module for Maple executions, one for C, one for Python and one for Pari. Now I'm trying to write the code for a Sage module.</p>
<p>The idea is that i have a master who sends tasks to several slaves, each task consists on executing the same Sage script but using different values for the variables <code>taskId</code> and <code>taskArgs</code>. The slave forks a process and executes a chunk such as this one:</p>
<pre><code> // NULL-terminated array of strings
char **args;
int nargs=2;
args = (char**)malloc((nargs+1)*sizeof(char*));
// Do not malloc for NULL
for (i=0;i<nargs;i++)
args[i] = malloc(BUFFER_SIZE);
// Fill up the array with strings
sprintf(args[0],"sage");
sprintf(args[1],"-c \"taskId=%d;taskArgs=[%s];load('%s');\"",taskNumber,arguments,inp_programFile);
args[2] = NULL;
// Call the execution and check for errors
err = execvp(args[0],args);
perror("ERROR:: child Sage process");
exit(err);
</code></pre>
<p>For example, an instance of this code would be executing </p>
<p><code>sage -c "taskId=71;taskArgs=[0,73,74,0];load('test.sage')"</code>,</p>
<p>where <em>test.sage</em> prints the arguments or whatever.</p>
<p>Now comes the question. If i execute the previous command from the command line i get the expected result (the two prints). However, if i execute my C program (which works corectly for Maple, C, Python and Pari using a very similar approach) i get this error:</p>
<p><code>sage-run received unknown option: -c "taskId=71;taskArgs:=[0,73,74,0];load('test.sage')"</code></p>
<p>I have already tried changing the double quotes <code>"</code> to single quotes <code>'</code>, using only a single, very simple argument from C (e.g. <code>sage -c "print(1);"</code>), all to no avail.</p>
<p>Am I missing something? How can the same command work from the command line but not when i use <code>execvp</code> from within C?</p>
<p>Thanks in advanced for any help!</p>
<hr/>
<p>(I know I could adapt my Python module to read Sage scripts, but i want a <em>standalone</em> module for Sage because the Python one uses <code>sys.args</code> and I'd like to keep the Sage scripts as simple as possible for the users of the software)</p>
https://ask.sagemath.org/question/33795/executing-sage-with-arguments-from-c-with-exec-family/?answer=33866#post-id-33866The <code>-c</code> needs to be a separate argument from the code:
<pre><code>
sprintf(args[0],"sage");
sprintf(args[1],"-c");
sprintf(args[2],"\"taskId=%d;taskArgs=[%s];load('%s');\"",taskNumber,arguments,inp_programFile);
args[3] = NULL;
</code></pre>Tue, 21 Jun 2016 01:55:49 +0200https://ask.sagemath.org/question/33795/executing-sage-with-arguments-from-c-with-exec-family/?answer=33866#post-id-33866Comment by osr for <p>The <code>-c</code> needs to be a separate argument from the code:</p>
<pre><code>
sprintf(args[0],"sage");
sprintf(args[1],"-c");
sprintf(args[2],"\"taskId=%d;taskArgs=[%s];load('%s');\"",taskNumber,arguments,inp_programFile);
args[3] = NULL;
</code></pre>
https://ask.sagemath.org/question/33795/executing-sage-with-arguments-from-c-with-exec-family/?comment=33868#post-id-33868You must be right, because the error no longer appears. But I've run into another problem doing it this way, which is not related to Sage afaik (my guess is PVM i/o). I will accept your answer anyway, thank you!Tue, 21 Jun 2016 09:10:51 +0200https://ask.sagemath.org/question/33795/executing-sage-with-arguments-from-c-with-exec-family/?comment=33868#post-id-33868