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

kill
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Process A Process B

kill

slide-2
SLIDE 2

Process A Process B

kill

Run default signal handler!

slide-3
SLIDE 3

Process A Process B

signal(SIGINT, func)

slide-4
SLIDE 4

Process A Process B

signal(SIGINT, func) kill

Run fun signal handler!

slide-5
SLIDE 5

Terminate process Generate Core Dump Ignore Stop

slide-6
SLIDE 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); }

slide-7
SLIDE 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); }

slide-8
SLIDE 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); }

slide-9
SLIDE 9

int fd = open(“hello.txt”, FLAGS)

a b c d e f g h j k l m

slide-10
SLIDE 10

read(fd, buffer, 2)

a b c d e f g h j k l m

slide-11
SLIDE 11

read(fd, buffer, 2)

a b c d e f g h j k l m

ab

slide-12
SLIDE 12

write(fd, “12”, 2)

a b c d e f g h j k l m

slide-13
SLIDE 13

write(fd, “12”, 2)

a b 1 2 e f g h j k l m

slide-14
SLIDE 14

lseek(fd, 2, SEEK_CUR)

a b 1 2 e f g h j k l m

slide-15
SLIDE 15

lseek(fd, 2, SEEK_CUR)

a b 1 2 e f g h j k l m

slide-16
SLIDE 16

lseek(fd, 10, SEEK_END)

a b 1 2 e f g h j k l m

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 19

Files Vs File Descriptor

slide-20
SLIDE 20

file descriptors

FD 1 2 Process B stdin stdout stderr a.txt b.txt Kernel File Table Process File Table

slide-21
SLIDE 21
  • pen(“b.txt”, O_CREAT|O_WR)

FD 1 2 Process B 3 stdin stdout stderr a.txt b.txt Kernel File Table Process File Table

slide-22
SLIDE 22

dup(2)

FD 1 2 Process B 3 stdin stdout stderr a.txt b.txt Kernel File Table Process File Table 4

slide-23
SLIDE 23

dup2(3, STDOUT_FD)

FD 1 2 Process B 3 stdin stdout stderr a.txt b.txt Kernel File Table Process File Table 4