LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2003, 03:01 PM   #1
GoboFraggle
LQ Newbie
 
Registered: Jan 2003
Posts: 10

Rep: Reputation: 0
Want to run a program from a program


I'm writing a C/C++ shell program using KDev on a Mandrake 9.0 system. I want to be able to launch programs using fork() and execvp() or something similar, but I have had no success getting anything to work. Can any anyone help me out. Here's my program so far. All the extra #include statements go along with things I've tried. I plan to weed out the unneeded ones.
What I've written so far sets up a continuous loop and parses the command line. Any help will be appreciated.
Gobo Fraggle

#ifdef HAVE_CONFIG_H
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <config.h>
#endif
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;



int main(int argc, char *argv[])
{
struct myShell_struct {
int argc;
char *name;
char *argv[];
char *command_line[];
}myShell;

myShell.argc = 0;

int i;
fputs("DASH:/ ",stdout);
gets(myShell.argv[0]);

//Initiate shell loop
while(strcmp(myShell.argv[0],"exit"))
{
i=0;
myShell.argc = 0;

//Parse command line
myShell.command_line[0] = strtok(myShell.argv[0]," ");
i=1;
while ( (myShell.command_line[i]=strtok(NULL, " ")) != NULL)
{
// Calculate myShell.argc = # of command line arguments -1
i++;
myShell.argc++;
}

fputs("DASH:/ ",stdout);
gets(myShell.argv[0]);
}

return 0;
}
 
Old 04-21-2003, 03:19 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I don't know what you'd like to do exactly, but it should be something like this:
Code:
pid_t child_pid;
child_pid=fork();
if(child_pid!=0) {
         /* parent process */
} else {
        execvp(program, arglist);
}
where arglist is char** (list of chars, can be created using
Code:
char* arglist[]= { "arg1", "arg2","arg3",NULL};
(NULL is needed at the end).
 
Old 04-21-2003, 03:51 PM   #3
pythagras
LQ Newbie
 
Registered: Mar 2003
Posts: 6

Rep: Reputation: 0
Exclamation BE CAREFUL!

BE CAREFUL!

Putting a fork in a loop is a recipe for disaster. Fork is like hitting the "New Window" button on a web browser - it creates a copy of the process with which you can execute other programs on top of. A loop that goes screwy with a fork inside can be pretty nasty.
 
Old 04-21-2003, 08:30 PM   #4
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Re: BE CAREFUL!

Quote:
Originally posted by pythagras
Putting a fork in a loop is a recipe for disaster. Fork is like hitting the "New Window" button on a web browser - it creates a copy of the process with which you can execute other programs on top of. A loop that goes screwy with a fork inside can be pretty nasty.
It's even worse than that, since the number of procs doubles each time. After about 20 loop iterations (which would take a fraction of a microsecond on modern processors) you'd have about a million copies of your program running!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to run mfo6463 Linux - Newbie 2 04-21-2004 07:28 PM
run another program in a program becky_starr Programming 7 02-15-2004 11:46 AM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM
please help me run this program joe blow Linux - Newbie 4 08-08-2003 10:07 AM
How do I run a program? rtstar Linux - Newbie 2 07-25-2003 09:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration