gpt4 book ai didi

c - 如果不使用 sudo 运行,为什么这个带有 execl 的程序会中断?

转载 作者:太空宇宙 更新时间:2023-11-04 08:27:49 27 4
gpt4 key购买 nike

这是diner_info程序:

#include <stdio.h> 
#include <stdlib.h>

int main(int argc, char *argv[]) {
printf("Diners: %s\n", argv[1]);
printf("Juice: %s\n", getenv("JUICE"));
return 0;
}

并且它被编译成一个名为diner_info的文件

这是另一个程序:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]){
char *my_env[] = {"JUICE=peach and apple", NULL};
execle("diner_info", "diner_info", "4", NULL, my_env);
}

当我这样运行这个程序时:

Korays-MBP:hello2 koraytugay$ sudo ./a.out 
Diners: 4

没问题...

但是当我不包含 sudo 时,我遇到了段错误。为什么?

操作系统是 OS X。

最佳答案

它不应该,您可能在第一个二进制文件中犯了一些错误。我在我的 MAC 和 Ubuntu 14.04 盒子上测试了它,两个基础都运行良好:

brenohl@sid:/tmp$ ./a.out 
Diners: 4
Juice: peach and apple

brenohl@sid:/tmp$ cat diner.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]){
char *my_env[] = {"JUICE=peach and apple", NULL};
execle("diner_info", "diner_info", "4", NULL, my_env);
}

brenohl@sid:/tmp$ cat diner_info.c
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
printf("Diners: %s\n", argv[1]);
printf("Juice: %s\n", getenv("JUICE"));
return 0;
}

关于c - 如果不使用 sudo 运行,为什么这个带有 execl 的程序会中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29634805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com