small program that can only Run by another program
Answers
Answer:
i think software
Explanation:
but i am not sure
Answer: In linux, I would like to write a C program that launches another program. When the program runs, the shell will be waiting for you to input a command that you have defined in you program. This command will launch the second program.
For example, assume there is a simple C program called "hello" in the same directory as the invoking program. The "hello" program prints the output "hello, world". The first program would be run and the user would input the command "hello." The "hello" program would be executed and "hello, world." would be output to the shell.
I have done some search, and people suggested the "fork()" and "exec()" functions. Others said to use "system()". I have no knowledge about these functions. How do I call these functions? Are they appropriate to use?
Example code with explanations would be most helpful. Other answers are also welcome. Your help is greatly appreciated.
Explanation: #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */
int main()
{
/*Spawn a child to run the program.*/
pid_t pid=fork();
if (pid==0) { /* child process */
static char *argv[]={"echo","Foo is my name.",NULL};
execv("/bin/echo",argv);
exit(127); /* only if execv fails */
}
else { /* pid!=0; parent process */
waitpid(pid,0,0); /* wait for child to exit */
}
return 0;
}
If you are new to fork, graphical representation about fork and exec might be helpful to you.
Depiction of fork()
+-------------+
|main program |
+-------------+ (fork())
|___________________________
+-------------+ |
|main program | +-------------+
+-------------+ |main program |
| +-------------+
+-------------+ | (exec())
|main program | +-------------+
+-------------+ |hello program|
+-------------+
As you might have already read in a tutorial, after calling fork() a duplicate copy of the existing program is created, and the exec() after that replaces that copy with the new program, which you pass to it as arguments. Two execution units for two programs will be running after fork()
Won't system() be enough for you?
/* ... */
if (!strcmp(cmd, "hello")) system("hello.exe");
/* ... */
For the most simple case you should two compiled programs in one directory:
> ls
.
hello
second
In second program you just need to call system("hello");
I have done some search, and people suggested the fork() and exec() functions. Others said to use system(). I have no knowledge about these functions. How do I call these functions? Are they appropriate to use?
Yes. Read first the documentation (man page), e.g. of fork(2), exec(3), system(3). Quite probably you have that documentation locally on your computer, using man(1). Notice that system uses sh (thru bash(1) or dash(1)), because it is fork-ing, execve(2)-ing and waitpid(2)-ing the /bin/sh POSIX shell.
I think that fork is difficult to understand because on success it returns "twice". I won't try to explain it here (I'll need many pages for that). I recommend reading fork (system call) wikipage at first. Then, read some good Linux programming book, e.g Advanced Linux Programming (freely downloadable).