kill
play

kill Run default signal handler! Process Process A B kill - PowerPoint PPT Presentation

Process Process A B kill Run default signal handler! Process Process A B kill signal(SIGINT, func) Process Process A B signal(SIGINT, func) Process Process A B kill Run fun signal handler! Terminate process Generate Core


  1. Process Process A B kill

  2. Run default signal handler! Process Process A B kill

  3. signal(SIGINT, func) Process Process A B

  4. signal(SIGINT, func) Process Process A B kill Run fun signal handler!

  5. Terminate process Generate Core Dump Ignore Stop

  6. void sigint_handler(int sig) { char c; signal(sig, SIG_IGN); printf(Ouch, you just hit Ctrl-C?. Do you really want to quit [y/n]?); c = getchar(); if (c == y || c = Y) exit(0); else signal(SIGINT, sigint_handler); }

  7. int main(int argc, char **argv) { int pid, status; int newfd; if ((newfd = open("output_file.txt", O_CREAT|O_TRUNC|O_WRONLY, 0644)) < 0) { exit(1); } printf("Luke, I am your..."); dup2(newfd, 1); printf("father"); exit(0); }

  8. int main(int argc, char **argv) { int pid, status; int newfd; char *cmd[] = { "/bin/ls", "-al", "/", 0 }; if (argc != 2) { fprintf(stderr, "usage: %s output_file\n", argv[0]); exit(1); } if ((newfd = open(argv[1], O_CREAT|O_TRUNC|O_WRONLY, 0644)) < 0) { perror(argv[1]); /* open failed */ exit(1); } printf("writing output of the command %s to \"%s\"\n", cmd[0], argv[1]); dup2(newfd, 1); execvp(cmd[0], cmd); perror(cmd[0]); /* execvp failed */ exit(1); }

  9. int fd = open(“hello.txt”, FLAGS) a b c d e f g h j k l m

  10. read(fd, buffer, 2) a b c d e f g h j k l m

  11. read(fd, buffer, 2) ab a b c d e f g h j k l m

  12. write(fd, “12”, 2) a b c d e f g h j k l m

  13. write(fd, “12”, 2) a b 1 2 e f g h j k l m

  14. lseek(fd, 2, SEEK_CUR) a b 1 2 e f g h j k l m

  15. lseek(fd, 2, SEEK_CUR) a b 1 2 e f g h j k l m

  16. lseek(fd, 10, SEEK_END) a b 1 2 e f g h j k l m

  17. lseek(fd, 10, SEEK_END) a b 1 2 e f g h j k l m note that we don’t write \0 in the middle until we make a write

  18. write(fd, “lo”, 2) a b 1 2 e f g h j k l m \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 l o

  19. Files Vs File Descriptor

  20. file descriptors Process File Table Kernel File Table stdin stdout stderr FD a.txt 0 b.txt Process B 1 2

  21. open(“b.txt”, O_CREAT|O_WR) Process File Table Kernel File Table stdin stdout stderr FD a.txt 0 b.txt Process B 1 2 3

  22. dup(2) Process File Table Kernel File Table stdin stdout stderr FD a.txt 0 b.txt Process B 1 2 3 4

  23. dup2(3, STDOUT_FD) Process File Table Kernel File Table stdin stdout stderr FD a.txt 0 b.txt Process B 1 2 3 4

Recommend


More recommend