Unix进程-编译并运行c程序

Unix进程-编译并运行c程序,c,unix,process,C,Unix,Process,创建一个父进程,该进程从命令行n个参数arg1、arg2、…、,阿尔金。arg1是源C的名称,arg2是编译arg1的可执行文件结果的名称,arg3,argn是要启动的参数 父进程编译arg1并创建可执行文件arg2,然后将其运行到子进程中 我试着用一些例子来解决这个问题,但我并没有真正理解它们,所以程序不起作用。我真的需要一些帮助 #include<unistd.h> #include<stdio.h> #include<sys/wait.h> #inclu

创建一个父进程,该进程从命令行n个参数arg1、arg2、…、,阿尔金。arg1是源C的名称,arg2是编译arg1的可执行文件结果的名称,arg3,argn是要启动的参数

父进程编译arg1并创建可执行文件arg2,然后将其运行到子进程中

我试着用一些例子来解决这个问题,但我并没有真正理解它们,所以程序不起作用。我真的需要一些帮助

#include<unistd.h>
#include<stdio.h>
#include<sys/wait.h>
#include<string.h>

int main(int argc, char* argv[]){
   char com[200];
   int p;
   p=fork();
   strcpy(com,"gcc -o prog.c");
   strcat(com,argv[1]);

   if(p==0){
       if(WEXITSTATUS(system(com))==0)
          execl("./prog.c","./prog.c",argv[3],argv[4],argv[5],NULL);
   }

   wait(0);
   exit(0);

   return 0;
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
char-com[200];
INTP;
p=fork();
strcpy(com,“gcc-o prog.c”);
strcat(com,argv[1]);
如果(p==0){
if(WEXITSTATUS(系统(com))==0)
execl(“./prog.c”、“/prog.c”、argv[3]、argv[4]、argv[5]、NULL);
}
等待(0);
出口(0);
返回0;
}

我要使用的C程序从两个文件中读取一些输入数据,并将数据存储到另一个文件中

您应该查阅
exec
手册,该手册将告诉您如何运行exec来派生另一个按照规范运行的进程。此代码可以帮助您将变量传递给子进程:

#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;
}
#包括
#包括
#包括/用于叉子*/
#包括/*用于pid\u t*/
#包括/*等待*/
int main()
{
/*生成一个子项以运行该程序*/
pid_t pid=fork();
如果(pid==0){/*子进程*/
静态字符*argv[]={“echo”,“Foo是我的名字。”,NULL};
execv(“/bin/echo”,argv);
退出(127);/*仅当execv失败时*/
}
else{/*pid!=0;父进程*/
waitpid(pid,0,0);/*等待子进程退出*/
}
返回0;
}

这段代码或多或少执行了您所说的程序应该执行的操作。特别是,它使用
argv[2]
作为程序名。它使用
snprintf()
避免长参数溢出(但不验证是否溢出)。它打印各种状态消息——部分是作为调试辅助,部分是为了给程序的各个部分赋予意义

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    int p;

    if (argc != 6)
    {
        fprintf(stderr, "Usage: %s source program file1 file2 file3\n", argv[0]);
        return(1);
    }

    if ((p = fork()) == 0)
    {
        char com[200];
        snprintf(com, sizeof(com), "gcc -o %s %s", argv[2], argv[1]);
        if (system(com) == 0)
        {
            printf("Compilation of %s successful\n", argv[2]);
            fflush(0);
            execl(argv[2], argv[2], argv[3], argv[4], argv[5], (char *)NULL);
            fprintf(stderr, "Failed to execute %s\n", argv[2]);
            return(1);
        }
        fprintf(stderr, "Compilation of %s from %s failed\n", argv[2], argv[1]);
        return(1);
    }

    int status;

    wait(&status);
    printf("Compilation and execution of %s yielded status %d\n",
           argv[2], WEXITSTATUS(status));
    return 0;
}

来自
gc2
的使用信息正确;程序需要6个参数,而不是程序给出的4个参数。

“程序不工作”不是一个问题……你确实需要问一个问题。看看你的第二个strcat的结果,你会发现它的格式不正确。此外,您尝试执行C程序,而不是编译后的输出。如果您像代码那样以
.C
后缀结束可执行文件,您会让大多数人感到困惑。通常,
.c
用于将文件标记为c源文件,而不是从c代码编译的程序。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    int p;

    if (argc != 6)
    {
        fprintf(stderr, "Usage: %s source program file1 file2 file3\n", argv[0]);
        return(1);
    }

    if ((p = fork()) == 0)
    {
        char com[200];
        snprintf(com, sizeof(com), "gcc -o %s %s", argv[2], argv[1]);
        if (system(com) == 0)
        {
            printf("Compilation of %s successful\n", argv[2]);
            fflush(0);
            execl(argv[2], argv[2], argv[3], argv[4], argv[5], (char *)NULL);
            fprintf(stderr, "Failed to execute %s\n", argv[2]);
            return(1);
        }
        fprintf(stderr, "Compilation of %s from %s failed\n", argv[2], argv[1]);
        return(1);
    }

    int status;

    wait(&status);
    printf("Compilation and execution of %s yielded status %d\n",
           argv[2], WEXITSTATUS(status));
    return 0;
}