我正在使用execvp來執行一些命令,如ls -l,who,cp -r ./aaa ./bbb等。一切正常,直到我嘗試執行相同的命令第二次。例如,我會告訴你我使用的命令:ls -l(一切正常),誰(一切正常),ls -l(壞地址)。這是我的代碼:在execvp中沒有運行相同的命令兩次或更多
if(strcmp(com_instr, "issuejob") == 0)
{
pid = fork();
if(pid < 0)
{
perror("Fork");
exit(1);
}
else if(pid == 0)
{
sleep(0.1);
//number of args
read(fd, &t_args, sizeof(int));
printf("t_args %d\n",t_args);
commands = malloc(t_args*sizeof(char *));
for(i=0; i<t_args; i++)
{
commands[i] = malloc(SIZE*sizeof(char));
}
for(i=0; i<t_args; i++)
{
read(fd, commands[i], SIZE);
}
//Receiving data from named-pipe
/*temp_run = run_node->next;
while(temp_run != NULL)
{
printf("jjjjjjjjjjjjjjjjjjjj %d",temp_run->job_id);
temp_run = temp_run->next;
}*/
printf("command %s\n", commands[0]);
execvp(commands[0], commands);
perror("execvp");
exit(1);
}
else if(pid > 0)
{
temp_run = run_node;
for(i=0; i<run_num; i++)
{
if(temp_run->next != NULL) temp_run = temp_run->next;
else break;
}
if((i <= run_num-1) && (wait_node->next == NULL))
{
temp_run->next = malloc(sizeof(run_list));
temp_run = temp_run->next;
saving_data_run(temp_run, j_id, line, 1, arg_num-1, pid);
printf("pid:%d\n",temp_run->pid);
++running;
send_data_for_exec(line, arg_num-1, fd);
}
else
{
temp_wait = wait_node;
while(temp_wait->next != NULL) temp_wait = temp_wait->next;
temp_wait->next = malloc(sizeof(wait_list));
saving_data_wait(temp_wait, j_id, line, 0, arg_num-1, pid);
wait_num++;
}
}
j_id++;
}
我使用命名管道來傳遞我的參數在char **命令中。參數正確傳遞給char **命令,我用printf檢查過。在execvp中多次執行相同的命令是否有問題?提前致謝!
我假設你叉首先,由於execvp新的方案替代了調用進程?問題可能與叉子有關。如果您可以提供一個完整的,最小的示例,但仍然會出現問題,它會使我們更容易。 –
是的,我使用fork()。我會嘗試編輯它。 –
請提供[完整,可重現的示例](http://stackoverflow.com/help/mcve)。通過運行程序並查看崩潰位置,而不是通過盯着代碼來發現問題通常更容易。 – Gilles