-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |