- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C 语言新手。我正在通过 Stevens & Rago 的书学习 UNIX。我有一个问题。早期的数字之一在我的实现中不起作用。
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
char buf[MAXLINE];
pid_t pid;
int status;
printf("%% ");
while (fgets(buf, MAXLINE, stdin) != NULL) {
if (buf[strlen(buf) - 1] == "\n")
buf[strlen(buf) - 1] = 0;
if ((pid = fork()) < 0) {
err_sys("Fork error: %d");
}
else if ((pid == 0) && (access(buf, F_OK ) != 1)){ /* Here I have an extra check to ensure, that the binary exists. */
execlp(buf, buf, (char *)0);
err_ret("Couldn't execute: %s", buf);
exit(127);
}
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}
当我启动程序时:
$ ./exec
% /bin/ls
Couldn't execute: /bin/ls
: No such file or directory
我有几个关于 execlp(buf, buf, (char *)0);
的问题:
为什么execlp
无法执行二进制文件?
int execlp(const char *file, const char *arg, ...);
为什么buf
传递了两次?如果第二个参数必须是二进制参数。我找不到,它是什么意思:(char *)0
?这是什么指针?那里的零是什么?
最佳答案
Couldn't execute: /bin/ls
: No such file or directory
请注意,错误消息位于新行中,但您尝试从输入中删除行尾字符。所以您的删除似乎不起作用。
原因是:
if (buf[strlen(buf) - 1] == "\n")
您正在将字符 (buf[x]
) 与字符串 ("\n"
)(即指针)进行比较。打开编译器的警告并仔细阅读它们。
试试这个:
if (buf[strlen(buf) - 1] == '\n')
execlp
的参数在 its man page 中描述。 :
The first argument, by convention, should point to the filename associated with the file being executed.
或者在 POSIX 中 here 。
(char *)0
是 char*
类型的空指针。这里没有发生什么神奇的事情,值为 0 的整型常量是一个空指针常量。但由于 execlp 是一个可变参数函数,因此强制转换是必要的 - 否则常量零将被视为 int ,它可能与指针的大小不同(并且空指针的内部表示可能与整数 0 的内部表示不同,从而导致未定义的行为。
关于即使可执行文件存在,C `execlp`也会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20826120/
我正在学习进程间通信......这是让我烦恼的代码 #include #include #include int main(void) { int pfds[2]; pipe(pfd
#include #include #include int main(){ pid_t pid; pid = fork(); if(pid<0){ fprintf(stderr, "fo
我正在编写一个程序,其界面如下:myprog file1 file2 c 这个程序创建了两个子进程,P2 用 execlp 打开 file2,在这个文件上创建一个 grep -c 以创建 c 并将结果
我正在制作一个程序,其中我必须 fork 4 个程序,然后这些程序将执行。我的问题是如何等待执行的 child 。当子进程执行时,Wait() 不起作用 最佳答案 My problem is how
我正在尝试执行类似于以下内容的文件: ./foo bar baz band 哪里 executable = "foo" path_executable =" bar baz band" 我正在使用 s
例如,我正在尝试创建一个简单的程序,它将从参数运行 shell 命令 ./run date +"%r" 07:56:05 PM 但我不知道怎么做。我尝试这个,但它不起作用。我很困惑,完全不明白 ex
我在 execlp 方面遇到问题。当我不知道如何正确地将命令从指针数组重定向到 execlp 时。例如我想使用 ls -l | sort -n 我的程序只需要“ls”和“sort” int
我正在尝试编译 2 个可执行文件。其中一台是采样器,另一台是收集器。必须从收集器的子级调用采样器。采样器一将一些数据写入共享内存,收集器应从共享内存中读取数据。我正在使用 execlp 来调用 Sam
这是exec()系统调用的非常简单的示例。在这里,我尝试调用 execlp() 两次。但是,我没有得到异常(exception)的输出。它仅显示当前目录的第一次调用的输出。 #include #in
我正在做一个涉及管道和执行的简单作业,这是代码。 #include #include int main(void){ int out[2]; pipe(out); char
这个问题已经有答案了: How to get the return value of a program ran via calling a member of the exec family of
我正在使用 execlp 运行 wc 命令,使用文件作为额外参数来读取字数。这个 unix 命令: wc -l HelloWorld.class 输出:7 HelloWorld.class 但是在我使
我正在尝试使用 execlp 返回多行输出,但我不确定这样做的确切方法。我试过了 #include #include #include using namespace std; int main
我无法通过 execlp 执行二进制文件. chdir("/home/foo/bar/baz/MB/"); execlp("bash", "bash", "./foobarbaz 1", NULL);
嗨,有人能解释一下为什么我应该在关闭管道后使用 execlp 吗? 这是一个例子: if( cid == 0) {//Only child cid can run this code char
我正在用 C 语言创建一个应用程序,我必须使用命令 execlp 来执行 firefox,但每次执行它时我都会“丢失”我当前的终端,但是在 execlp 我仍然需要使用以前的终端,所以我的问题是:有没
exec() 系统调用的这个非常简单的例子。在这里,我尝试调用 execlp() 两次。但是,我没有得到异常输出。它仅显示对当前目录的第一次调用的输出。 #include #include int
我最近遇到了这样的问题: 如果你在一个函数中使用execlp,并且下面还有一些代码,那么在什么情况下会执行这些代码? 例如: execlp("ps","ps","-u","username",(cha
我正在尝试使用 fork 和 execlp 运行一个非常简单的程序,但它并没有像我预期的那样工作。目前,我的工作目录中有一个名为“1”的文件。所以命令 rm 1* 应该删除它。然而,当通过 execl
我想从 C 文件运行 execlp() 并将结果写入某个输出文件。我用这条线: buff = "./cgi-bin/smth"; execlp(buff, buff, "> /cgi-bin/tmp"
我是一名优秀的程序员,十分优秀!