Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用fork和exec启动bin程序_C_Fork_Exec_Command Prompt_Bin - Fatal编程技术网

使用fork和exec启动bin程序

使用fork和exec启动bin程序,c,fork,exec,command-prompt,bin,C,Fork,Exec,Command Prompt,Bin,我正在编写一个microshell,它将使用fork和exec从bin文件启动程序,它工作正常,但是它总是在没有命令提示的情况下在下一行中获得输入,我不知道如何处理这个问题 #include <unistd.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #de

我正在编写一个microshell,它将使用fork和exec从bin文件启动程序,它工作正常,但是它总是在没有命令提示的情况下在下一行中获得输入,我不知道如何处理这个问题

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAX 4096
#define BLUE "\x1b[94m"
#define CLEAR "\x1b[0m"
int main()
{
    int pyk = 1;
    while (pyk == 1)
    {
        struct stat buf;
        const char *space = " ";
        const char *enter = "\n";
        char *token;
        char input[MAX];
        char *arg;
        char *cwd = getcwd(NULL, 0);
        printf(BLUE "[{%s}] $ ", cwd);
        printf(CLEAR);

        fgets(input, MAX, stdin);
        token = strtok(input, space);
        arg = strtok(NULL, enter);

        if (!strncmp(token, "/bin/", 5))
        {
            if (!stat(token, &buf))
            {
                if (fork() == 0)
                {

                    pyk = 0;

                    execl(token, arg, NULL);

                    continue;
                }
                else
                {
                    continue;
                }
            }
        }
    }
    return 0;
}

空的空间是它在ls之后得到输入的地方,我想做的就是把命令promt放在那里记住它只是我代码的一部分,你只想在子进程完成后继续,所以你必须等待它这样做:

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define MAX 4096
#define BLUE "\x1b[94m"
#define CLEAR "\x1b[0m"
int main() 
{  
  int pyk=1;
   while(pyk==1){
             struct stat buf;
              const char *space=" ";
            const char *enter="\n"; 
            char *token;
            char input[MAX];
            char *arg;
            char * cwd = getcwd(NULL, 0);
            int child;
              printf(BLUE "[{%s}] $ ", cwd);
               printf(CLEAR);
                fflush(stdout);
              fgets(input, MAX, stdin);
              token = strtok(input, space);
              arg = strtok(NULL, enter);      
             
               if (!strncmp(token,"/bin/",5))
                {
                 
                    if (!stat(token,&buf))
                    {
                       child = fork();

                        if(child==0)
                        {  
                            pyk=0;
                              execl(token, arg, NULL);
                                    continue;               
                        }
                            else
                            {      
                                    wait(NULL);           
                            }  
                }
   }
   }
 return 0;
}   
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大值4096
#定义蓝色“\x1b[94m”
#定义清除“\x1b[0m”
int main()
{  
int-pyk=1;
while(pyk==1){
结构统计buf;
常量字符*空格=”;
常量字符*enter=“\n”;
字符*令牌;
字符输入[MAX];
char*arg;
char*cwd=getcwd(NULL,0);
智力儿童;
printf(蓝色“[{%s}]$”,cwd);
printf(清晰);
fflush(stdout);
fgets(输入、最大值、标准输入);
令牌=strtok(输入,空间);
arg=strtok(空,输入);
如果(!strncmp(标记“/bin/”,5))
{
如果(!stat(令牌和buf))
{
child=fork();
如果(子项==0)
{  
pyk=0;
execl(令牌,arg,NULL);
继续;
}
其他的
{      
等待(空);
}  
}
}
}
返回0;
}   
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define MAX 4096
#define BLUE "\x1b[94m"
#define CLEAR "\x1b[0m"
int main() 
{  
  int pyk=1;
   while(pyk==1){
             struct stat buf;
              const char *space=" ";
            const char *enter="\n"; 
            char *token;
            char input[MAX];
            char *arg;
            char * cwd = getcwd(NULL, 0);
            int child;
              printf(BLUE "[{%s}] $ ", cwd);
               printf(CLEAR);
                fflush(stdout);
              fgets(input, MAX, stdin);
              token = strtok(input, space);
              arg = strtok(NULL, enter);      
             
               if (!strncmp(token,"/bin/",5))
                {
                 
                    if (!stat(token,&buf))
                    {
                       child = fork();

                        if(child==0)
                        {  
                            pyk=0;
                              execl(token, arg, NULL);
                                    continue;               
                        }
                            else
                            {      
                                    wait(NULL);           
                            }  
                }
   }
   }
 return 0;
}