sách gpt4 ai đã đi

c - 查看 execvp();

In lại 作者:太空宇宙 更新时间:2023-11-04 07:56:47 27 4
mua khóa gpt4 Nike

我是新手,正在尝试学习 fork() 和系统调用的功能,现在我正在使用 execvp() 来尝试制作 bash,但我遇到的问题是,当我编写正确的命令时,程序结束,我想循环使用我的 bash,直到有人在我的命令行中写下“退出”。我使用的代码是:

#include 
#include
#include
#include
#include
#include

int chính()
{
char cadena[100];
char *array[100];
char *ptr;
int i=1;
pid_t pid;
pid = fork();

if (pid < 0) {
perror("Error en la llamada a fork().");
return -1;
}
else if (pid == 0) {
LÀM {
printf("prompt$ ");
fgets(cadena, 100, stdin);
ptr = strtok(cadena, " \n");
array[0] = ptr;
while( (ptr = strtok( NULL, " \n" )) != NULL ){
array[i] = ptr;
i++;
}
array[i]=NULL;
execvp(array[0],array);
perror("Execvp failed");
} while(array[0] != "exit");
}
else {
wait(NULL);
}
trả về 0;
}

我正在使用迭代结构 do-while 来尝试循环,但它不起作用,因为当我编写正确的突击队时,程序结束,我需要继续编写突击队,因为我需要做一个列表我在程序结束后编写的所有命令。

1 Câu trả lời

您有一个普遍的设计问题:除非出现调用错误,否则所有 thực hiện 函数都不会返回给调用者。 shell 的常见设计是伪代码:

loop
prompt for a command
read a command line
parse the command line
if exit
then exit loop
khác
fork (a child to execute the command, detailed below)
if pid is 0 (child)
then exec command
khác
wait for the child to end

关于c - 查看 execvp();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49506319/

27 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com