LinuxQuestions.org
Visit Jeremy's Blog.
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 02-07-2010, 05:14 PM   #1
fpsasm
LQ Newbie
 
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20

Rep: Reputation: 1
C programming, run a command line or program


Hello,

I am wanting to write a program that runs a program or command-line. Is there are way of making a program that activates a command-line (for example executing 'ps -a -f' or '/home/shared/fah').

In addition to that, I want the program to do a 'ps -a -f' and put the results in a buff, how could I do this.

Cheers, thanks for the help in advanced,

Matthew
 
Old 02-07-2010, 07:45 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
You can call a program using either "system" or an "exec" function. You also need to know how fork and IPC (interprocess communication) work, so read up on those first if you dont already know. A simple method is with standard files, another method is with "pipes" (named or unnamed).

An example of how to do this might be
Code:
//open file/pipe

// fork

// child closes read end of fd/pipe, and later will write to it
// parent closes write end of fd/pipe, and later will read from it

// child redirects stdout to the write fd/pipe, using "dup2"
// note: if the program writes to stderr instead, then you must redirect that instead of stdin

// child calls the program, using an "exec" function

// parent reads from the fd/pipe, which should be the output of the program

// parent now has variable storing whatever the program outputted
This is a relatively complicated thing to do, so, again, if you arent familiar with the above topics then learn the basics of those. A good way to do that is by reading example code, versus digging through man or other pages. This is why my example above is very short, because if I wrote and explained the actual code, it would take--well, that isnt realistic for you to expect from us.
 
Old 02-07-2010, 08:02 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
system() will work.
exec() will also work but it will terminate your program.
 
Old 02-07-2010, 08:15 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
"exec" replaces the current process. If you "fork" and then "exec", as I described above, then there is no problem.
 
Old 02-09-2010, 05:03 AM   #5
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
you could use popen(). It opens a pipe, forks a shell and then runs the command specified in the parameter. It dumps/refirects the output on the standard output of the command to a file stream that can be read from using fgets, etc.

man 3 popen
 
Old 02-09-2010, 08:07 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Whether to use system() vs. fork() + exec() vs. popen() should depend on whether your child process requires the services of a shell and whether you need to process the output of the child process. If you simply need to invoke another process, then fork()+exec() should suffice. If you need a shell to process a commandline, then system() is your friend. If you need to grab the standard output stream from the child process, then you want popen().
--- rod.
 
  


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
How To Run Program From Command Line? neuropulse Linux - Newbie 11 05-28-2020 09:39 AM
How to enter graphical mode (run level 5) command line (run lenel 3) edmondgyampoh Linux - Newbie 3 05-15-2009 06:33 PM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM
C program code to run a Linux command line Linh Programming 10 06-11-2003 01:59 PM

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

All times are GMT -5. The time now is 01:58 AM.

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