execvp在C中跳过certian代码

时间:2018-05-20 15:25:54

标签: c shell exec

这个程序做了两件事: 1)复制shell的动作 2)将用户输入记录到tmp.log文件

这里的问题是在我的子进程中,printf(" ABC");什么也没做。 输出日志文件工作正常,但它不会打印。

为什么这样做?

我知道execvp应该替换当前的进程,但这并不能解释为什么它会执行输出而不是print。 我看到了以下链接,但这并没有回答我的问题。 exevp skips over all code until wait call in c

#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include  <sys/types.h>
#include <wordexp.h>

void execute(char *user_input)
{
    pid_t pid;        
    int state_loc; 
    if( (pid = fork()) == -1){
      printf("fork failed\n");
      exit(1);
    }
    else if(pid == 0){

      FILE *f;
      //open the file and append. Create if not there.
      f = fopen("tmp.log", "a+"); 
      if (f == NULL) { printf("Something is wrong");}

      struct tm *p;
      struct tm buf;
      char timestring[100];
      time_t ltime = time(NULL);
      if (NULL != (p=localtime_r(&ltime, &buf))){
        strftime(timestring, sizeof(timestring),"** %c: ", p);
        fprintf(f, "%s %s \n", timestring, user_input);
      }
      fclose(f);      

      char* separator = " ";
      char* argv[64];
      int argc = 0;
      char* tmp;
      argv[argc] = strtok_r(user_input, separator, &tmp);
      while( argv[argc] != NULL){
        argc+=1;
        argv[argc] = strtok_r(NULL, separator, &tmp);
      }

      printf("ABC"); //why doesn't this print??

      execvp(argv[0],argv);
    }
    else{          
      wait(&state_loc);
    }
}


int main ()
{
  while(1)
  {
    char user_input[1024];
    printf("recsh>> ");
    //empty the buffer right scanf
    scanf("%[^\n]", user_input);
    //calls each character in the user input, repeat until it reaches the terminating \n
    while( getchar() != '\n'); 
    if(strcmp(user_input, "exit") == 0){
      printf("Exiting\n");
      break;
    }
    else{
      execute(user_input);
    }
  }
  return 0;
}

1 个答案:

答案 0 :(得分:1)

在调用printf之前,已在孩子中执行对execvp的调用。

由于stdout默认为line-buffered,并且打印的文本不构成一行(因为没有换行符号),所以它会留在输出缓冲区中。当图像被execvp替换时,该输出缓冲区将与原始可执行文件的其余部分一起消失。

道德:总是使用换行符(\n)终止输出。