In Linux, I would like to write a program that launches another program. When the program runs, the shell is waiting for you to input a command that you have defined in the program. This command will launch another program. For example, suppose that is a simple program that is in the same directory as "Hello" in the same directory as the invoking program. "Hello" program prints out "Hello, World" prints. The first program will be played and user will input the input "Hello". The "Hello" program will be executed and "Hello, world." There will be output in the shell.
I have searched something, and people have suggested "fork ()" and "exec ()" functions others asked to use "system ()" about these tasks I have no knowledge how do I call these functions? Are they suitable to use?
Example code with explanation will be most useful Other answers are also welcome Your assistance is highly appreciated.
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Unistd.h & gt; / * Fork * / #include & lt; Sys / types.h & gt; / * Pid_t * / #include & lt; Sys / wait.h & gt; For / * waiting * / int main () {/ * Leave the egg to the child to run the program. * / Pid_t pid = fork (); If (pid == 0) {/ * hair process * / stable char * argv [] = {"echo", "Foo is my name", NULL}; Execv ("/ bin / echo", argv); Get out (127); / * Only if execv fails} and / / pid! = 0; Basic Process * / OpticalPad (Plague, 0,0); / * Waiting for the child to leave * Return 0; }
Comments
Post a Comment