How to call two C programs from within one C program?

How can I call two C applications from within another C application?

e.g. :

pg1.c can be run as ./a.out pg1_args

pg2.c can be run as ./a.out pg2_args

I would like to write a program that can be run as:

./a.out pg1_args pg2_args

With the result being equivalent to :

./a.out pg1_args

./a.out pg2_args

./a.out pg1_args

./a.out pg2_args

the pg1 here is svm_scale and pg2 here is svm_predict , both taken from libsvm : http://www.csie.ntu.edu.tw/~cjlin/libsvm/

[ edit ]

@Jonathan ,

I wrote these programs for trying out this concept..

pg1.c

#include <stdio.h>
#include <string.h>

int main(int argc,char **argv)
{
    FILE *fin;
    fin=fopen("pg1file.txt","a");
    fprintf(fin,"%s",argv[1]);
    fflush(fin);
    fclose(fin);
}

pg2.c

#include <stdio.h>
#include <string.h>

int main(int argc,char **argv)
{
    FILE *fin;
    fin=fopen("pg2file.txt","a");
    fprintf(fin,"%s",argv[1]);
    fflush(fin);
    fclose(fin);
}

pg3.c :

#include<stdio.h>
#include<string.h>
int main(int argc,char **argv)
{
int i;
const char *cmd1 = strcat("./pg1 ",argv[1]);
const char *cmd2 = strcat("./pg2 ",argv[2]);
for(i=0;i<4;i++)
{
if (system(cmd1) != 0)
    printf("\n error executing pg 1"); 
if (system(cmd2) != 0)
    printf("\n error executing pg 2");
}
}

[root@localhost trinity]# ./a.out first second
Segmentation fault (core dumped)
[root@localhost trinity]# 

Could somebody explain what I've done wrong?

Answers


From your latest code, here is your problem:

const char *cmd1 = strcat("./pg1 ",argv[1]);
const char *cmd2 = strcat("./pg2 ",argv[2]);

That's bad for a couple of reasons (string literals are usually put in read only memory and they to do not have memory allocated for concatenating new data at the end).

Change that to:

size_t len = snprintf(NULL, 0, "./pg1 %s", argv[1]);
char *cmd1 = malloc(len + 1);
snprintf(cmd1, len, "./pg1 %s", argv[1]);

size_t len = snprintf(NULL, 0, "./pg2 %s", argv[2]);
char *cmd2 = malloc(len + 1);
snprintf(cmd2, len, "./pg2 %s", argv[2]);

A very simple solution is to use the system() function. Pass a program's command line to it to run the program.


Need Your Help

Can we check the queue depth using a scripting language?

java perl bash shell websphere-mq

Is it feasible to check queue depth(MQ) using any scripts? [No restrictions on the language]. The plan is to look at non-Java solutions.

Can't get a CALayer to update its drawLayer: DURING a bounds animation

ios objective-c animation core-animation core-graphics

I'm trying to animate a custom UIView's bounds while also keeping its layer the same size as its parent view. To do that, I'm trying to animate the layers bounds alongside its parent view. I need the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.