Ask Your Question
1

Executing Sage with arguments from C with exec family

asked 2016-06-15 14:16:59 +0100

osr gravatar image

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)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-06-21 01:55:49 +0100

dgulotta gravatar image

The -c needs to be a separate argument from the 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;
edit flag offensive delete link more

Comments

You 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!

osr gravatar imageosr ( 2016-06-21 09:10:51 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-06-15 14:16:59 +0100

Seen: 780 times

Last updated: Jun 21 '16