ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.