Error[8]: Undefined offset: 2, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我正在尝试编写 shell.但是我的 shell没有执行命令 – ls -l |减.我正在使用execvp.代码如下. #include <stdio.h>#include <unistd.h>#include <string.h>int main(){ int pid, status, num, len; char str[1000], cwd[100]; cha 我正在尝试编写 shell.但是我的 shell没有执行命令 – ls -l |减.我正在使用execvp.代码如下.
#include <stdio.h>#include <unistd.h>#include <string.h>int main(){    int pID,status,num,len;    char str[1000],cwd[100];    char* word[100];    getcwd(cwd,sizeof(cwd));    while(1){        chdir(cwd);        printf("%s > ",cwd);        gets(str);        pID=vfork();        if(pID == 0){            num = 0;            word[num] = strtok (str," ");            while (word[num] != NulL) {                word[num] = strdup (word[num]);                len = strlen (word[num]);                if (strlen (word[num]) > 0)                    if (word[num][len-1] == '\n')                        word[num][len-1] = '
word[0] = "sh"word[1] = "-c"word[2] = "ls -l | less"word[3] = NulL
'; word[++num] = strtok (NulL," "); } if(strcmp(word[0],"cd") == 0){ chdir(word[1]); getcwd(cwd,sizeof(cwd)); } else{ execvp(word[0],word); } exit(0); } else{ wait(&status); } } return 0;}
解决方法 ls -l | less实际上是一个shell命令行,它由两个由管道连接的进程组成. execvp()调用只能生成一个进程.

如果要从程序中执行此 *** 作,则必须显式调用shell – 通过使用system()调用或将命令行更改为sh -c’ls -l |减’.你的单词数组应如下所示:

[+++]

[编辑]或者,您可以执行shell在内部执行的 *** 作:生成两个进程并使用管道连接它们.这将涉及使用fork(),pipe(),dup2()和execve()调用.但是,调用shell的工作量要少得多,而且因为交互式程序的数量较少,所以不需要担心性能太高:任何时间不到100毫秒的东西都被认为是瞬时的.

总结

以上是内存溢出为你收集整理的linux execvp; ls无法访问|,没有这样的文件目录全部内容,希望文章能够帮你解决linux execvp; ls无法访问|,没有这样的文件或目录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
linux execvp; ls无法访问|,没有这样的文件或目录_系统运维_内存溢出

linux execvp; ls无法访问|,没有这样的文件或目录

linux execvp; ls无法访问|,没有这样的文件或目录,第1张

概述我正在尝试编写 shell.但是我的 shell没有执行命令 – ls -l |减.我正在使用execvp.代码如下. #include <stdio.h>#include <unistd.h>#include <string.h>int main(){ int pid, status, num, len; char str[1000], cwd[100]; cha 我正在尝试编写 shell.但是我的 shell没有执行命令 – ls -l |减.我正在使用execvp.代码如下.
#include <stdio.h>#include <unistd.h>#include <string.h>int main(){    int pID,status,num,len;    char str[1000],cwd[100];    char* word[100];    getcwd(cwd,sizeof(cwd));    while(1){        chdir(cwd);        printf("%s > ",cwd);        gets(str);        pID=vfork();        if(pID == 0){            num = 0;            word[num] = strtok (str," ");            while (word[num] != NulL) {                word[num] = strdup (word[num]);                len = strlen (word[num]);                if (strlen (word[num]) > 0)                    if (word[num][len-1] == '\n')                        word[num][len-1] = '
word[0] = "sh"word[1] = "-c"word[2] = "ls -l | less"word[3] = NulL
'; word[++num] = strtok (NulL," "); } if(strcmp(word[0],"cd") == 0){ chdir(word[1]); getcwd(cwd,sizeof(cwd)); } else{ execvp(word[0],word); } exit(0); } else{ wait(&status); } } return 0;}
解决方法 ls -l | less实际上是一个shell命令行,它由两个由管道连接的进程组成. execvp()调用只能生成一个进程.

如果要从程序中执行此 *** 作,则必须显式调用shell – 通过使用system()调用或将命令行更改为sh -c’ls -l |减’.你的单词数组应如下所示:

[编辑]或者,您可以执行shell在内部执行的 *** 作:生成两个进程并使用管道连接它们.这将涉及使用fork(),pipe(),dup2()和execve()调用.但是,调用shell的工作量要少得多,而且因为交互式程序的数量较少,所以不需要担心性能太高:任何时间不到100毫秒的东西都被认为是瞬时的.

总结

以上是内存溢出为你收集整理的linux execvp; ls无法访问|,没有这样的文件目录全部内容,希望文章能够帮你解决linux execvp; ls无法访问|,没有这样的文件或目录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1049443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存