gpt4 book ai didi

c - 为什么这个 Linux 编程 C 示例代码失败?

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:47 24 4
gpt4 key购买 nike

<分区>

我正在尝试运行《高级 Linux 编程》一书中的示例( list 3.4,第 51 页):

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

/* Spawn a child process running a new program. PROGRAM is the name
of the program to run; the path will be searched for this program.
ARG_LIST is a NULL-terminated list of character strings to be
passed as the program’s argument list. Returns the process ID of
the spawned process. */
int spawn(char* program, char** arg_list) {
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork();
if (child_pid != 0)
/* This is the parent process. */
return child_pid;
else {
/* Now execute PROGRAM, searching for it in the path. */
execvp(program, arg_list);
/* The execvp function returns only if an error occurs. */
fprintf(stderr, "an error occurred in execvp\n");
abort();
}
return 0;
}

int main() {
/* The argument list to pass to the "ls” command. */
char* arg_list[] = { "ls", /* argv[0], the name of the program. */
"-l", "/", NULL /* The argument list must end with a NULL. */
};
/* Spawn a child process running the "ls” command. Ignore the
returned child process ID. */
spawn(" ls", arg_list);
printf("done with main program\n");
return 0;
}

我得到了:

an error occurred in execvp
done with main program

知道这里出了什么问题吗? (使用 Ubuntu 10.10)

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