C Linux 如何将Argument 传递给另一个程序?
C Linux How you passing Argument to another program?
我想在 C 中创建另一个进程。
但我不知道如何将论点传递给另一个人,
使用 fork() , exec()
这是我的代码
sls.c
int main() {
void start_game (const char* file_name,int N,int max_lives)
{
pid_t pid;
int child_status;
pid = fork();
if(pid!=0){
wait(&child_status);
}
else{
if(-1 == execl("./player","player",&file_name,&max_lives,(char *)NULL))
{
perror("execlp() failed");
return 1;
}
}
int r = 1+rand()%max_lives;
}
return(0);
}
我需要将file_name和max_live传递给player.c
所以 player.c 可以读取同一个文件
player.c
int main(int argc,char *argv[]) {
pid_t child_pid = fork();
int child_status;
int sidekick_amount = (int)getpid%5;
void read_ints (const char* file_name)
{
// Normal read file stuff here
}
if (child_pid!=0)
{
wait(&child_status);
}
else
{
execlp("","-l",NULL);
printf("Exec error\n");
exit(-1);
}
int lives = argv[2];
printf("\nAt pos 0: Player %d has %d lives , %d sidekicks and
\n is ready to start ",(int)getppid(),lives,sidekick_amount);
read_ints(file_name);
return (0);
}
阅读文件并设置 sidekicks 数量后。
player.c 需要生成 sidekick.c sidekick_amount 进程
作为 player.c child 。但我无法获得 file_name 和 生命 形成 player.c
sidekicks.c
enter code here
输出
At pos 0: Player 25477 has -5703328 lives , 1 sidekicks and is ready to start
感谢所有的帮助,非常感谢所有的回答
您需要将整数 max_lives
转换为字符串,以便将其作为函数参数传递。当你传递文件名时,只传递字符串本身,而不是指针的地址。
void start_game (const char* file_name,int N,int max_lives)
{
pid_t pid;
int child_status;
pid = fork();
if(pid!=0){
wait(&child_status);
}
else{
char max_lives_str[20];
sprintf(max_lives_str, "%d", max_lives);
if(-1 == execl("./player","player", file_name, max_lives_str, (char *)NULL))
{
perror("execlp() failed");
return 1;
}
}
int r = 1+rand()%max_lives;
}
我想在 C 中创建另一个进程。 但我不知道如何将论点传递给另一个人, 使用 fork() , exec() 这是我的代码
sls.c
int main() {
void start_game (const char* file_name,int N,int max_lives)
{
pid_t pid;
int child_status;
pid = fork();
if(pid!=0){
wait(&child_status);
}
else{
if(-1 == execl("./player","player",&file_name,&max_lives,(char *)NULL))
{
perror("execlp() failed");
return 1;
}
}
int r = 1+rand()%max_lives;
}
return(0);
}
我需要将file_name和max_live传递给player.c 所以 player.c 可以读取同一个文件
player.c
int main(int argc,char *argv[]) {
pid_t child_pid = fork();
int child_status;
int sidekick_amount = (int)getpid%5;
void read_ints (const char* file_name)
{
// Normal read file stuff here
}
if (child_pid!=0)
{
wait(&child_status);
}
else
{
execlp("","-l",NULL);
printf("Exec error\n");
exit(-1);
}
int lives = argv[2];
printf("\nAt pos 0: Player %d has %d lives , %d sidekicks and
\n is ready to start ",(int)getppid(),lives,sidekick_amount);
read_ints(file_name);
return (0);
}
阅读文件并设置 sidekicks 数量后。 player.c 需要生成 sidekick.c sidekick_amount 进程 作为 player.c child 。但我无法获得 file_name 和 生命 形成 player.c
sidekicks.c
enter code here
输出
At pos 0: Player 25477 has -5703328 lives , 1 sidekicks and is ready to start
感谢所有的帮助,非常感谢所有的回答
您需要将整数 max_lives
转换为字符串,以便将其作为函数参数传递。当你传递文件名时,只传递字符串本身,而不是指针的地址。
void start_game (const char* file_name,int N,int max_lives)
{
pid_t pid;
int child_status;
pid = fork();
if(pid!=0){
wait(&child_status);
}
else{
char max_lives_str[20];
sprintf(max_lives_str, "%d", max_lives);
if(-1 == execl("./player","player", file_name, max_lives_str, (char *)NULL))
{
perror("execlp() failed");
return 1;
}
}
int r = 1+rand()%max_lives;
}