简体   繁体   English

使用 execvp、fork 和等待执行文件中的代码

[英]Using execvp,fork and wait to execute codes in file

I have been using execvp to execute unix commands written in a text file.我一直在使用 execvp 来执行写在文本文件中的 unix 命令。 Below is the code i have written but it doesn't seem to work.下面是我写的代码,但它似乎不起作用。
I am reading lines from a file and each line contains a unix command(mv,cp etc).我正在从文件中读取行,每行都包含一个 unix 命令(mv、cp 等)。

#include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #include <unistd.h>
    int main ( int argc, char *argv[] )
    {   pid_t  pid;
        pid_t child_pid;
        int child_status;  
        char *token;    
        FILE *fp;
        fp = fopen(argv[1],"r");
        int i,j,ctr,rc_wait;
        if( fp == NULL){
            perror("An error has occurred\n");
            exit(1);
            }
        char buff[100];
        while(fgets(buff, sizeof(buff), fp)){    
            child_pid=fork();
            if(child_pid<0){
                printf("\nfork failed, error");
                exit(0);
            }
            else if(child_pid==0){
            //printf("\nString value = %s", buff);
            token = strtok(buff, " \t");
            execvp(token,buff);
            }
            else{
            rc_wait = wait(NULL);
            continue;
            }

            }   

            }
            return 0;
            }

input file has been provided as an argument to the program and the input contains below as example:输入文件已作为参数提供给程序,输入包含以下示例:

    cp temp1/f1.txt              temp2/
    mv temp1/f2.c                           temp2/
    cp temp1/f3.txt                temp2/
    cp temp1/f4.txt                temp2/
    cp temp1/f5.txt                temp2/
    cp temp1/f6.txt                 temp2/
    mv temp1/f7.c                   temp1/f8.c
    mv temp1/f9.c                   temp2/
    cp temp1/f1.txt              temp1/f1a.txt

You're misusing strtok and execvp .你在滥用strtokexecvp The former mutates the input string as you call it, so after it has run once, it's split up buff by a NUL ( token and buff actually refer to the same address after the first call, though token would change after additional tokenization).前者在您调用它时会改变输入字符串,因此在它运行一次之后,它会被一个NUL拆分bufftokenbuff在第一次调用后实际上指的是同一个地址,尽管token会在额外的标记化后发生变化)。 You can only use strtok to fully parse the input if you make copies of each result as it's generated (eg via strdup if you can rely on modern POSIX standards), because only the most recently returned pointer is ever valid.如果您在生成每个结果时复制每个结果(例如,如果您可以依赖现代 POSIX 标准,则通过strdup ),您只能使用strtok来完全解析输入,因为只有最近返回的指针才是有效的。

Your execvp usage is also wrong;你的execvp用法也是错误的; the second argument to execvp is an array of C-style strings, char* s (the array itself being terminated with a NULL pointer). execvp的第二个参数是 C 样式字符串数组char* s(数组本身以NULL指针终止)。 You passed it a plain array of char (one string, not an array of them).您向它传递了一个普通的char数组(一个字符串,而不是它们的数组)。

Please read the man pages for both APIs;请阅读这两个 API 的man页; you're so far off the mark it's clear you're just guessing at how they work.你离目标太远了,很明显你只是在猜测它们是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM