Skip to content

Commit

Permalink
add chapter8 exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
tinylcy committed Jan 5, 2017
1 parent 061b4c7 commit 0e71a4f
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 0 deletions.
Binary file modified Chapters/chapter8/a.out
Binary file not shown.
23 changes: 23 additions & 0 deletions Chapters/chapter8/exercise_8_7.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

unsigned int snooze(unsigned int secs) {
int leftsecs = sleep(secs);
printf("Slept for %d of %d secs.\n", secs - leftsecs, secs);
return secs - leftsecs;
}

void sigint_handler(int sig) {

}

int main(void) {
if(signal(SIGINT, sigint_handler) == SIG_ERR) {
perror("signal error.");
}

snooze(10);
}
40 changes: 40 additions & 0 deletions Chapters/chapter8/exercise_8_8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

volatile long counter = 2;

void handler1(int sig) {
sigset_t mask, prev_mask;

sigfillset(&mask);
sigprocmask(SIG_BLOCK, &mask, &prev_mask);
printf("%ld\n", --counter);
sigprocmask(SIG_SETMASK, &prev_mask, NULL);
_exit(0);
}

int main(void) {
pid_t pid;
sigset_t mask, prev_mask;

printf("%ld\n", counter);
fflush(stdout);

signal(SIGUSR1, handler1);
if((pid = fork()) == 0) {
while(1) {}
}
kill(pid, SIGUSR1);
waitpid(-1, NULL, 0);

sigfillset(&mask);
sigprocmask(SIG_BLOCK, &mask, &prev_mask);
printf("%ld\n", ++counter);
sigprocmask(SIG_SETMASK, &prev_mask, NULL);

exit(0);
}
14 changes: 14 additions & 0 deletions Chapters/chapter8/exittest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>

int func() {
printf("This is func function.\n");
// exit(0);
return 0;
}

int main(void) {
func();
printf("会执行这句话吗?\n");
return 0;
}
17 changes: 17 additions & 0 deletions Chapters/chapter8/kill.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

int main(void) {
pid_t pid;
if((pid = fork()) == 0) {
pause();
printf("control should never reach here!\n");
exit(0);
}

kill(pid, SIGKILL);
exit(0);
}
21 changes: 21 additions & 0 deletions Chapters/chapter8/sigint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

void sigint_handler(int sig) {
printf("Caught SIGINT!\n");
// exit(0);
}

int main(void) {
if(signal(SIGINT, sigint_handler) == SIG_ERR) {
perror("signal error.");
}

pause();

printf("会执行这句话吗?\n");
return 0;
}
46 changes: 46 additions & 0 deletions Chapters/chapter8/signal1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>

#define MAXBUF 1024

void handler1(int sig) {
int olderrno = errno;
if(waitpid(-1, NULL, 0) < 0) {
perror("waitpid error");
}
printf("handler reaped child\n");
sleep(1);
errno = olderrno;
}

int main(void) {
int i, n;
char buf[MAXBUF];

if(signal(SIGCHLD, handler1) == SIG_ERR) {
perror("signal error");
}

for(i = 0; i < 3; i++) {
if(fork() == 0) {
printf("Hello from child %d\n", (int)getpid());
exit(0);
}
}

if((n = read(0, buf, sizeof(buf))) < 0) {
perror("read");
}

printf("Parent processing input\n");
while(1) {
;
}

exit(0);
}
45 changes: 45 additions & 0 deletions Chapters/chapter8/signal2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>

#define MAXBUF 1024

void handler2(int sig) {
int olderrno = errno;
while(waitpid(-1, NULL, 0) > 0) {
printf("handler reaped child\n");
}
sleep(1);
errno = olderrno;
}

int main(void) {
int i, n;
char buf[MAXBUF];

if(signal(SIGCHLD, handler2) == SIG_ERR) {
perror("signal error");
}

for(i = 0; i < 3; i++) {
if(fork() == 0) {
printf("Hello from child %d\n", (int)getpid());
exit(0);
}
}

if((n = read(0, buf, sizeof(buf))) < 0) {
perror("read");
}

printf("Parent processing input\n");
while(1) {
;
}

exit(0);
}

0 comments on commit 0e71a4f

Please sign in to comment.