c - 调用 execlp() 时为 "Bad Address"

标签 c exec

这是一个简单的程序,只会执行 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/

相关文章:

c - 奇怪的数字出现在输出-C

c - A a = (A) b; 中是否需要显式类型转换?

javascript - nodejs 'exec' 命令无法正确执行 'sed' 命令

python - 在Python中的类函数中使用exec时出现问题

linux - 为什么程序没有执行这个C编程或unix编程中的某些语句(execvp()系统调用)?

将用户输入与文本文件进行比较并在 C 中循环

c - 打印字符串时出错

c - 字符串或文件熵

go - 我如何从向 stdout 发出错误的一系列 exec 命令中获取错误消息

java - 从 Java 运行 AWK 创建文件并使用 BufferedReader 读取它