使用 fork 和 exec 启动 bin 程序
launching bin programs with fork and exec
我正在编写一个 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;
}
[{/home/maks/lab/vsc}] $ /bin/ls ls
[{/home/maks/lab/vsc}] $ a.out messenger pliczek.cm shel shel.c shel.o test test.c -W
[{/home/maks/lab/vsc}] $
空 space 是它在 ls 之后立即输入的地方,我想做的就是把命令提示符放在那里 记住它只是我代码的一部分
你只想在子进程完成后继续,所以你必须等待它完成:
#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;
}
我正在编写一个 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;
}
[{/home/maks/lab/vsc}] $ /bin/ls ls
[{/home/maks/lab/vsc}] $ a.out messenger pliczek.cm shel shel.c shel.o test test.c -W
[{/home/maks/lab/vsc}] $
空 space 是它在 ls 之后立即输入的地方,我想做的就是把命令提示符放在那里 记住它只是我代码的一部分
你只想在子进程完成后继续,所以你必须等待它完成:
#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;
}