如何用c++写一个在后台运行的cp函数(在linux shell中)?

问题描述 投票:0回答:1

我正试图写一个我自己的小linux shell,我想写一个函数。cp 该函数的格式如下。

cp <old-file-path> <new-file-path>

它将第一个文件复制到第二个文件中(覆盖它),如果第二个文件不存在,它将创建一个新的文件,如果文件没有打开或任何系统调用不成功,它将打印一个错误信息。cp 命令在后台进行(使用 fork 不等它完成)。)

我的问题是:如何使用 fork 目前,子进程成为僵尸进程。

这是我的代码。

// num_args contains the number of arguments sent to cp
class CopyCommand : public BuiltInCommand {
 public:
  CopyCommand(const char* cmd_line) : BuiltInCommand(cmd_line){}
  virtual ~CopyCommand() {}
  void execute() override{
      if(this->num_args < 1){  // if no arguments were send to cp
          perror("invalid arguments");
          return;
      }
      char* buff;
      int fd1 = open(args[1], O_RDONLY);   
      if(fd1 == -1){
            perror("open failed");
            return;
      }
      if(this->num_args==2){          // copy file1 into file2 (overrite file 1)
          int fd2 = open(args[2], O_TRUNC);
            if (fd2 == -1) {                // if we couldn't open the file then create a new one (not sure if we supposed to this ?)
                fd2 = open(args[2], O_CREAT, 0666);
                if (fd2 == -1) {
                    perror("open failed");
                    return;
                }
            }
          pid_t PID = fork();
          if(PID == -1){
              perror("fork failed");
              return;
          }
          else if(PID == 0){

             // i need to use fork here :( before i start to write
            int read_res = read(fd1, &buff, 1);  /// read from the file fd1 into fd2
            while (read_res != -1) {
                if (!read_res) {
                    break;
                }
                if (write(fd2, buff, 1) == -1) {
                    perror("write failed");
                }
                read_res = read(fd1, buff, 1);
            }
            if (read_res == -1) {
                perror("read failed");
            }     
}   
      }
      else if(this->num_args==1){               // create file2 and copy file1 into file2

          // don't know how to do this yet 
          // i need to use fork here :(

      }
  }
};
c++ fork
1个回答
0
投票

首先,我把你的代码重写了一下,特别是注意子分支(PID==0)在完成后退出,父分支在分叉后和出错时关闭传下来的文件描述符。

if (this->num_args == 2) {
    int fd1 = open(args[1], O_RDONLY);
    if (fd1 == -1) {
        perror("open failed");
        return;
    }

  int fd2 = open(args[2], O_TRUNC);
  if (fd2 == -1) {
    fd2 = open(args[2], O_CREAT, 0666);
    if (fd2 == -1) {
      perror("open failed");
      close(fd1);
      return;
    }
  }

  pid_t PID = fork();
  if (PID == -1) {
    perror("fork failed");
  } else if (PID == 0) {
    char buff[1024];
    int read_res = read(fd1, &buff, 1024); /// read from the file fd1 into fd2
    while (read_res != -1) {
      if (!read_res) {
        break;
      }
      if (write(fd2, buff, read_res) == -1) {
        perror("write failed");
      }
      read_res = read(fd1, buff, 1024);
    }
    if (read_res == -1) {
      perror("read failed");
    }
    exit(0);
  } else {
    printf("Copy running in background (pid: %d)\n", PID);
  }

  close(fd1);
  close(fd2);
  return
}

当子进程调用 exit进程将停留在 "僵尸 "状态。这个状态允许父进程(你)调用 waitwaitpid 来获取退出代码。

作为进程结束的次要影响,内核会发送一个 "退出代码"。SIGCHLD 到你的过程中,让你知道你实际上可以调用 wait 而不被阻塞。在你的情况下,你并不关心退出代码,所以你可以在程序开始时设置一个 "don't care "信号处理程序,让内核来清理这个过程。

signal(SIGCHLD, SIG_IGN);

这一点在signal(2)里有记载。

如果一个进程明确指定SIGCHLD信号的动作是SIG_IGN,那么当调用进程的子进程退出时,系统将不会创建僵尸进程。 因此,系统将丢弃子进程的退出状态。

© www.soinside.com 2019 - 2024. All rights reserved.