How do you write a C program to execute another pr

2019-01-08 22:54发布

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.

标签: c linux
5条回答
男人必须洒脱
2楼-- · 2019-01-08 23:31

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().

查看更多
仙女界的扛把子
3楼-- · 2019-01-08 23:33
#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;
}
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-08 23:45

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).

Read also about Virtual Address Space, and proc(5).

You could also read Operating Systems: Three Easy Pieces for a more general view.

查看更多
做自己的国王
5楼-- · 2019-01-08 23:54

Won't system() be enough for you?

/* ... */
if (!strcmp(cmd, "hello")) system("hello.exe");
/* ... */
查看更多
放荡不羁爱自由
6楼-- · 2019-01-08 23:54

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");

查看更多
登录 后发表回答