微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误的文件描述符和我的文件输出被锁定我无法访问我的代码创建的文件

如何解决错误的文件描述符和我的文件输出被锁定我无法访问我的代码创建的文件

所以我在将终端命令输出输出文件时遇到了问题。我的任务是创建一个程序,该程序对给定文本文件中的每个命令使用 fork,并使用 execvp() 运行所述命令。在我的代码(特别是我在代码中注释的子进程)中,我使用 open() 创建文件并使用 dup2() 将命令行输出输出文件中。我的问题是出现错误:“写入错误文件描述符错误。”我用来测试我的程序的文件包含以下 shell 命令

sleep 5
ls -latr
sleep 3
pwd
sleep 1
wc /etc/passwd

该程序对 sleep 命令运行良好,但对其他命令不起作用。该程序还会创建一个我无法访问的 .out 文件

Here is a picture of the files I can't access

Here is a screenshot of the output if I run the program in my terminal

这是我的代码

   #include <stdio.h>

    #include <unistd.h>

    #include <fcntl.h>

    #include <sys/types.h>

#include <sys/wait.h>

#include <string.h>



int main(int argc,char *argv[])

{

FILE* myfile = fopen(argv[1],"r"); //opens file 

    if(myfile == NULL)

    {

        printf("Error file is null");

    }



    //separate each line of the file into a command

    char commands[100][1000]; //Contains the commands of the file. Max array of size of 100 commands and 1000 chars per command

    char line [128];

    int i = 0; //iterator,counts total amount of lines/commands

    while (fgets(line,sizeof line,myfile) != NULL) /* read a line */

    {

        strcpy(commands[i],line); /* write the line */

        i++;

    }



    //Parse commands to be readable by execvp by using strtok

    char *arguments[100][(1000/2)+1]; //max of 501 arguments,will be used for execvp()

for(int k = 0; k < i; k++)

    {

        commands[k][strlen(commands[k])-1] = '\0';

        arguments[k][0] = strtok(commands[k]," ");

        int l = 0; //iterator

        while(arguments[k][l] != NULL)

        {

                arguments[k][++l] = strtok(NULL," ");

        }

    }



    pid_t pids[i]; //create process pid array of size [total commands in the file]



    //for loop runs i amount of times,i == total commands

    for(int j = 0; j < i; j++)

{

        pids[j] = fork();



        //If fork fails,exit

    if((pids[j]  < 0))

    {

        printf("An error occured creating the fork \n");

        return 2;

    }



    //This is the Child Processes

    else if(pids[j] == 0)

    {

                if(myfile == NULL)

        {

            printf("\n Error! File is NULL... \n");

        }

                else

                {   

                    char outBuffer[50];

                    char errBuffer[50];

                    pid_t childPID = getpid();

                    pid_t parentPID = getppid();

            

                    sprintf(outBuffer,"%d.out",childPID);

                    sprintf(errBuffer,"%d.err",parentPID);

            

                    int outFile = open(outBuffer,O_RDWR | O_CREAT | O_APPEND | 0777);

                    int errFile = open(errBuffer,O_RDWR | O_CREAT | O_APPEND | 0777);

                    if (outFile < 0 || errFile < 0)

                    {

                        printf("Error with opening the out/err file");

                    }

            

                    dup2(outFile,1);

                    dup2(errFile,2);



                    close(errFile);

                    close(outFile);



                    printf("Starting command %d: child %d pid of parent %d \n",j+1,childPID,parentPID);

                    int err = execvp(arguments[j][0],arguments[j]);

                    if(err == -1)

                    {

                        printf("Could not find the given program to execute \n");

                            return 2;

                    }

                    printf("Finished child %d pid of parent %d \n",parentPID);    

                }

        }

    }



    fclose(myfile);

    //This is the parent process

    for(int j = 0; j < i; j++)

{

        int wstatus;



    wait(&wstatus);

        if(WIFEXITED(wstatus))

        {

                int statusCode = WEXITSTATUS(wstatus);

                if(statusCode == 0)

                {

                    printf("Successful execution! \n");

                }

                else

                {

                    printf("Exited with exit code: %d \n",statusCode);

                }

        

        }

}



    return 0;

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。