使用fork()和execvp()函数创建C程序时出现问题

使用fork()和execvp()函数创建C程序时出现问题,c,fork,ipc,scanf,execvp,C,Fork,Ipc,Scanf,Execvp,以下是我目前遇到的问题代码: #include <stdio.h> #include <unistd.h> #include<sys/types.h> #include<sys/wait.h> #define MAX_LINE 80 int main(void) { char *args[MAX_LINE/2+1]; int background= 0;//integer that acts a boolean for if &

以下是我目前遇到的问题代码:

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

#define MAX_LINE 80

int main(void)
{
    char *args[MAX_LINE/2+1];
    int background= 0;//integer that acts a boolean for if & appears
    int should_run = 1;
    int status;

        while(should_run)//Just to keep the interface going until the user chooses to stop
        {
            printf("osh>");//prompt
            fflush(stdout);

            int i = 0;
            while(getchar() != '\n')//Use scanf until a new line is found
            {
                scanf("%s", &args[i]);

                if(strcmp(&args[i], "exit") == 0)//temporary exit protocal
                {
                    printf("Exiting now...");
                    return 0;
                }


                if(strcmp(&args[i], "&") == 0)//If we find a & in our input then we changed background to 1 or true
                    background = 1;
                printf("Args[%i] = %s\n", i, &args[i]);//Tester
                i++;
            }


            printf("Background = %i\n",background);//Test
            pid_t pid= fork();// Create new child process
            printf("process %i created\n", pid);//Test



            if(pid < 0)//fork() failed
            {
                printf("Fork Failed.\n");
                return 1;
            }

            else if(pid == 0)//Child process id
            {
                printf("Child process started %s command\n", &args[0]);
                if(execvp(args[0], args) < 0)//change the current child process to execute the input given by the user 
                                //with args[0] being the command and args being the parameters(ls -l is an example).
                {
                    printf("Command failed\n");
                }
                return 0;
            }

            else//Parent Process
            {
                if(background == 1)//If the user typed in a & then the parent will wait for a change in state from the child, if there is no &
                            //then we will just finish the parent process
                {
                    printf("Parent process waiting on child\n");
                    wait(NULL);
                }
            }

        }

    return 0;
#包括
#包括
#包括
#包括
#定义最大行80
内部主(空)
{
字符*args[最大行/2+1];
int background=0;//为if&显示布尔值的整数
int应该运行=1;
智力状态;
while(should_run)//只是为了让界面继续运行,直到用户选择停止
{
printf(“osh>”);//提示
fflush(stdout);
int i=0;
while(getchar()!='\n')//使用scanf直到找到新行
{
scanf(“%s”、&args[i]);
if(strcmp(&args[i],“exit”)==0)//临时退出协议
{
printf(“现在退出…”);
返回0;
}
if(strcmp(&args[i],“&”)==0)//如果我们在输入中找到一个&,那么我们将background更改为1或true
背景=1;
printf(“Args[%i]=%s\n”、i和Args[i]);//测试仪
i++;
}
printf(“背景=%i\n”,背景);//测试
pid_t pid=fork();//创建新的子进程
printf(“进程%i创建\n”,pid);//测试
if(pid<0)//fork()失败
{
printf(“Fork失败。\n”);
返回1;
}
else if(pid==0)//子进程id
{
printf(“子进程已启动%s命令\n”,&args[0]);
if(execvp(args[0],args)<0)//更改当前子进程以执行用户给定的输入
//args[0]是命令,args是参数(ls-l就是一个例子)。
{
printf(“命令失败\n”);
}
返回0;
}
else//父进程
{
if(background==1)//如果用户键入a&则父级将等待子级的状态更改,如果没有&
//然后我们将完成父进程
{
printf(“父进程等待子进程”\n);
等待(空);
}
}
}
返回0;
我现在有一个主要问题和一个次要问题。主要问题是,在execvp启动之前,我有一个printf方法,上面写着“子进程已启动”,我得到这一行进行打印,但没有其他事情发生。没有引发中断,程序似乎在我的execvp命令上被冻结

我的小问题是,当我的程序在请求输入之前启动一个提示“osh>”时。例如,如果我输入“osh>ls-l”,那么我会得到args[0]=s,args=-l。现在如果我将“osh>ls-l”放在这个精确的格式中,我会得到args[0]=ls,args=-l。这是scanf()的一部分吗我在这里没有正确地使用它来确保在“osh>”之后和空格之间获得字符作为字符串

编辑: 这是用户输入“ls-l”的输出

缺少字符的问题是因为
getchar()
scanf
尝试输入之前,正在使用输入的第一个字符。您可能需要执行以下操作:

while (scanf("%s", &buffer) > 0)
{
    strcpy(args[i], buffer);
    /* then do stuff with args[i] */
}

您从不为
args
中的指针分配内存。它们的任何使用都有未定义的行为。此外,如果您不采取任何措施确保最后一个参数后的指针为
NULL
execvp()
希望以这种方式标记参数列表的结尾。无论如何,这将是一个开始。人们还必须担心避免缓冲区溢出,而且由于没有为参数分配空间,因此可能会出现另一个问题,
strdup()
将是
strcpy()的有用替代方案
在这种情况下。当然。您还需要对
i
进行边界检查,以确保不会溢出数组本身。