- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个简单的程序,只会执行 3 个子进程。每个都将创建一个锁,读取一个文本文件,并取消该锁的链接。如果正确执行,子进程将退出,并将其进程 ID 转换为 8 位。如果无法创建/取消链接锁,程序将尝试 n_try
次,并在其间随机休眠一段时间。我对 C 和使用 exec() 相当陌生,所以我不确定我的程序出了什么问题。第一个子进程正确执行,但之后的任何内容都会返回错误地址
。我所做的就是复制确切的代码。一些帮助将不胜感激。这三个参数是文本文件的名称、创建/取消链接锁的尝试次数以及尝试之间的 sleep 时间。谢谢!
父级:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void main(int argc, char *argv[]) {
pid_t childpid1, childpid2, childpid3;
char* fname = "lock";
int fd, ret_val, status;
pid_t w;
printf("I AM %d\n", (int)getpid());
if ((childpid1 = fork()) == 0) { // If child 0
execlp("./proj3b", "./proj3b", argv[1], argv[2], argv[3], "0", "lock");
}
printf("I AM %d FORKED CHILD %d\n", (int)getpid(), (int)childpid1);
while ((w = waitpid(childpid1, &status, 0)) && w != - 1) {
if (w != - 1)
printf ("Wait on PID: %d return status of: %04X\n", w, status);
}
unlink(fname); // Unlink the lock to make sure nothing is left.
// End of child 1
printf("\nEND OF CHILD 0 \n\n");
if ((childpid2 = fork()) == 0) { // If child 1
execlp("./proj3b", "./proj3b", argv[1], argv[2], argv[3], "1", "lock");
perror("Exec failure"); // Exec failed if this line is reached.
exit(-1);
}
printf("I AM %d FORKED CHILD %d\n", (int)getpid(), (int)childpid2);
while ((w = waitpid(childpid2, &status, 0)) && w != - 1) {
if (w != - 1)
printf ("Wait on PID: %d return status of: %04X\n", w, status);
}
unlink(fname);
printf("\nEND OF CHILD 1 \n\n");
}
child :
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void main(int argc, char *argv[]) {
char *text;
char *fname;
int n_try, sleeptime, fd, k, ret_val, count = 0, status;
pid_t pid;
text = argv[1];
n_try = atoi(argv[2]);
sleeptime = atoi(argv[3]);
k = atoi(argv[4]);
fname = argv[5];
pid = getpid();
ret_val = (int) (pid % 256);
srand((unsigned) pid);
while ((fd = creat(fname, 0)) == -1 && errno == EACCES) {
if (++count < n_try)
sleep(rand()%sleeptime);
else {
printf ("\n Unable to generate.\n");
kill(pid,0);
}
}
if (!fork())
execlp("/bin/cat", "cat", text, (char *) 0); // Reading text
while (wait(&status) >= 0);
while (unlink(fname)!=0)
if (++count < n_try)
sleep(sleeptime);
else {
printf ("\n Cannot release file\n");
exit(-1);
}
exit(ret_val);
}
最佳答案
您在子级中正确终止了 execlp
的参数列表,但在父级中没有正确终止。
关于c - 调用 execlp() 时为 "Bad Address",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33506564/
我正在学习进程间通信......这是让我烦恼的代码 #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"
我是一名优秀的程序员,十分优秀!