User Tools

Site Tools


Sidebar

os_cp:exec_dup:solutions

Exercise 1

#include <stdio.h>
#include <unistd.h>
int main(void) {
  execlp("ps", "ps", "-F", NULL);
  perror("execvp");
  return -1;
}

Exercise 2

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
  char **av = malloc(sizeof(char *) * (argc + 4));
  char a0[] = "ls";    av[0] = a0;
  char a1[] = "-l";    av[1] = a1;
  char a2[] = "-t";    av[2] = a2;
  char a3[] = "-r";    av[3] = a3;
  for (int i = 0; i < argc; ++i)
    av[i + 4] = argv[i + 1];
  execvp("ls", av);
  perror("execvp");
  return -1;
}

Exercise 3

#include <stdio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
 
int main(int argc, char *argv[]) {
  struct timespec start, end;
  timespec_get(&start, TIME_UTC);
  if (!fork()) {
    execvp(argv[1], argv + 1);
    perror("execvp");
    return -1;
  }
  wait(NULL);
  timespec_get(&end, TIME_UTC);
  double elapsedSec = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;
  printf("%f\n", elapsedSec);
  return 0;
}

Exercise 4

#include <assert.h>
#include <stdio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
 
int main(int argc, char *argv[]) {
  struct timespec start, end;
  timespec_get(&start, TIME_UTC);
  if (!fork()) {
    execvp(argv[1], argv + 1);
    perror("execvp");
    return -1;
  }
  close(0);
  close(1);
  int status;
  wait(&status);
  timespec_get(&end, TIME_UTC);
  double elapsedSec = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;
  if (WIFEXITED(status)) {
    // You may test exit status by compiling & using the following program:
    // main(int,char**v){return atoi(v[1]);}
    fprintf(stderr, "%f (returned %d)\n", elapsedSec, WEXITSTATUS(status));
  } else {
    assert(WIFSIGNALED(status));
    fprintf(stderr, "%f (killed by signal %d)\n", elapsedSec, WTERMSIG(status));
  }
  return 0;
}

Exercise 5

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
 
int main() {
  int fd = open("output", O_WRONLY | O_TRUNC | O_CREAT, 0666);
  if (fd == -1) {
    perror("output");
    return 1;
  }
  dup2(fd, 1);
  close(fd);
  execlp("ps", "ps", "-F", NULL);
  return 1;
}

Exercise 6

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
 
int main(int argc, char **argv) {
  if (argc < 2) {
    write(2, "Too few arguments\n", 18);
    return 1;
  }
  int fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror(argv[1]);
    return 1;
  }
  dup2(fd, 0);
  close(fd);
  argv[1] = "tr";
  execvp("tr", argv + 1);
  return 1;
}

Exercise 7

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
 
int main() {
  int fileFd = open("file", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fileFd == -1) {
    perror("file");
    return 1;
  }
  int errorFd = dup(2);
 
  errno = ELOOP;
  perror("ELOOP");
  dup2(fileFd, 2);
  perror("ELOOP");
 
  errno = EMFILE;
  dup2(errorFd, 2);
  perror("EMFILE");
  dup2(fileFd, 2);
  perror("EMFILE");
 
  close(errorFd);
  close(fileFd);
  return 1;
}
os_cp/exec_dup/solutions.txt · Last modified: 2023/04/18 23:53 by jkonczak