You are on page 1of 6

Stack Exchange log in | careers | chat | meta | about | faq Stack Overflow * * * * * Questions Tags Users Badges Unanswered

* Ask Question How to call two C programs from within one C program? up vote 1 down vote favorite 1 Please login or register to vote for this post. (click on this box to dismiss) 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? c libsvm link|improve this question edited Mar 29 '10 at 12:13 voyager 11.3k22650 asked Jan 22 '10 at 6:13 trinity 6102622 65% accept rate 2 Why use C for the wrapper? Surely a simple shell script would do fine: while tru e; do pg1 pg1_args; pg2 pg2_args; done William Pursell Jan 22 '10 at 6:39

strcat is writing off the end of your strings, the first argument to strcat need s to point to a space allocated with enough room for the string you're adding to the end. pib Jan 22 '10 at 6:49 feedback 4 Answers active oldest votes up vote 4 down vote accepted 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 t he 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]); link|improve this answer answered Jan 22 '10 at 6:46 R Samuel Klatchko 34.2k43380 feedback up vote 4 down vote A very simple solution is to use the system() function. Pass a program's command line to it to run the program. link|improve this answer answered Jan 22 '10 at 6:15 Max Shawabkeh 12.7k11839 feedback up vote 1 down vote Probably the simplest technique is to build the two calls (are the two 'differen t' programs really both called a.out?) as a string and then use the system() fun ction: const char *cmd1 = "./a.out pg1_args"; const char *cmd2 = "./a.out pg2_args";

if (system(cmd1) != 0) ...report trouble... if (system(cmd2) != 0) ...report trouble... Clearly, you would normally build those command lines from the arguments passed to your program, rather than hard-wiring them as shown. Just be wary of buffer o verflows and unexpected characters when you build the command lines. link|improve this answer answered Jan 22 '10 at 6:15 Jonathan Leffler 154k14124287 okay , i'll try this , thanks. trinity Jan 22 '10 at 6:25 @trinity: R Samuel Klatchko shows what I had in mind; I was flying at the wrong time and couldn't provide that answer. In C, it is always crucial to understand where string data is stored. Your code was trying to attach an argument after a constant string - which was in read-only memory, hence leading to the core dump. Jonathan Leffler Jan 22 '10 at 19:09 feedback up vote 1 down vote const char *cmd1 = strcat("./pg1 ",argv[1]); const char *cmd2 = strcat("./pg2 ",argv[2]); is wrong. "./pg1 " is a read-only string. You can't append anything to it. You h aven't got access to the memory after the string "./pg1 " ends anyway. So, you n eed to get access to memory where you can write your strings. Try: char *cmd1 = malloc(strlen("./pg1 ") + strlen(argv[1]) + 1); char *cmd2 = malloc(strlen("./pg2 ") + strlen(argv[2]) + 1); if (cmd1 == NULL || cmd2 == NULL) { /* deal with error */ } sprintf(cmd1, "./pg1 %s", argv[1]); sprintf(cmd1, "./pg1 %s", argv[1]); and then remember to free the memory when you're done. Or you can declare them a s arrays with a big enough size: char cmd1[32] = "./pg1 "; char cmd1[32] = "./pg2 "; strcat(cmd1, argv[1]); strcat(cmd1, argv[2]); but the above will be bad if there is not enough space in cmd1 or cmd2. link|improve this answer answered Jan 22 '10 at 6:50 Alok 19.1k32766 thanks , it s working. trinity Jan 22 '10 at 7:18 feedback

Your Answer * * * * * * * * * * * * * * * draft saved draft discarded log in or Name Email required, but never shown Home Page discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged c libsvm or ask your own question. Hello World! This is a collaboratively edited question and answer site for professional and e nthusiast programmers. It's 100% free, no registration required. about faq

tagged c 63927 libsvm 175 asked 2 years ago viewed 506 times active 2 years ago 91 People Chatting LOUNGE(C); yesterday - Domagoj Panda

[Domagoj Panda: yesterday] Related Categorical Feature traning data sets using libsvm on windows How to get support vectors and rho values from svm_model (in Python)? LIbsvm error rate How to use libsvm for text classification? Java-ML(LibSVM) How can I get the classes probabilities? Strict class labels in SVM SVM equations from e1071 R package? How to extract info from scikits.learn classifier to then use in C code How to start SVM training on MATLAB How to implement ten-fold cross validation in LibSVM How to determine a decision boundary calculated by libsvm Suppress C warning messages in R How do I interpret an incorrect result? How to create mex files for libsvm C files Implementation of One vs Rest Multi-class classifier using LIBSVM LibSVM training error python program to export numpy/lists in svmlight format libsvm, how to classify data that doesn't have enough features how can i use grid.py for parameter selection? Libsvm-anyone has used it? prediction using libsvm in java libsvm input data? LIBSVM (svmpredict) How to tell if OpenMP is working? question feed lang-c about | faq | blog | chat | data | shop | legal | privacy policy | advertising i nfo | mobile | contact us | feedback stackoverflow.com api/apps careers serverfault.com superuser.com meta are 51 webapps gaming ubuntu webmasters cooking game development math ph graphy stats tex english theoretical cs programmers unix apple wordpr physics home improvement gis electronics android security bicycles d drupal sharepoint scifi & fantasy user experience skeptics rpg judaism rev 2012.7.6.3243 site design / logo 2012 stack exchange inc; user contributions licensed under cc -wiki with attribution required

You might also like