Skip to content

Commit 0374656

Browse files
committed
시그널, 메모리 매핑
1 parent e371034 commit 0374656

21 files changed

Lines changed: 396 additions & 0 deletions

ch07/ex7_10.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <sys/time.h>
2+
#include <unistd.h>
3+
#include <signal.h>
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
7+
void handler() {
8+
printf("Timer Invoked..\n");
9+
}
10+
11+
int main(void) {
12+
struct itimerval it;
13+
14+
sigset(SIGALRM, handler);
15+
it.it_value.tv_sec = 3;
16+
it.it_value.tv_usec = 0;
17+
it.it_interval.tv_sec = 2;
18+
it.it_interval.tv_usec = 0;
19+
20+
if(setitimer(ITIMER_REAL, &it, (struct itimerval *)NULL) == -1) {
21+
perror("setitimer");
22+
exit(1);
23+
}
24+
25+
while(1) {
26+
if(gettimer(ITIMER_REAL, &it) == -1) {
27+
perror("getitimer");
28+
exit(1);
29+
}
30+
printf("%d sec, %d msec.\n", (int)it.it_value.tv_sec,
31+
(int)it.it_value.tv_usec);
32+
sleep(1);
33+
}
34+
35+
return 0;
36+
}

ch07/ex7_11.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <unistd.h>
2+
#include <signal.h>
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
void handler(int signo) {
8+
char *s;
9+
10+
s = strsignal(signo);
11+
printf("Received Signal : %s\n", s);
12+
}
13+
14+
int main(void) {
15+
if(sigset(SIGINT, handler) == SIG_ERR) {
16+
perror("sigset");
17+
exit(1);
18+
}
19+
20+
sighold(SIGINT);
21+
22+
pause();
23+
24+
return 0;
25+
}

ch07/ex7_12.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <unistd.h>
2+
#include <signal.h>
3+
#include <stdio.h>
4+
5+
int main(void) {
6+
sigset_t new;
7+
8+
sigemptyset(&new);
9+
sigaddset(&new, SIGINT);
10+
sigaddset(&new, SIGQUIT);
11+
sigprocmask(SIG_BLOCK, &new, (sigset_t *)NULL);
12+
13+
printf("Blocking Signals : SIGINT, SIGQUIT\n");
14+
printf("Send SIGQUIT\n");
15+
kill(getpid(), SIGQUIT);
16+
17+
printf("UnBlocking Signals\n");
18+
sigprocmask(SIG_UNBLOCK, &new, (sigset_t *)NULL);
19+
20+
return 0;
21+
}

ch07/ex7_13.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <unistd.h>
2+
#include <signal.h>
3+
#include <siginfo.h>
4+
#include <stdio.h>
5+
6+
void handler(int signo) {
7+
psignal(signo, "Received Signal:");
8+
}
9+
10+
int main(void) {
11+
sigset_t set;
12+
13+
sigset(SIGALRM, handler);
14+
15+
sigfillset(&set);
16+
sigdelset(&set, SIGALRM);
17+
18+
alarm(3);
19+
20+
printf("Wait...\n");
21+
22+
sigsuspend(&set);
23+
24+
return 0;
25+
}

ch07/ex7_6.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <unistd.h>
2+
#include <signal.h>
3+
#include <siginfo.h>
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
7+
void handler(int signo) {
8+
psignal(signo, "Received Signal:");
9+
sleep(5);
10+
printf("In Signal Handler, After Sleep\n");
11+
}
12+
13+
int main(void) {
14+
struct sigaction act;
15+
16+
sigemptyset(&act.sa_mask);
17+
sigaddset(&act.sa_mask, SIGQUIT);
18+
act.sa_flags = 0;
19+
act.sa_handler = handler;
20+
if(sigaction(SIGINT, &act, (struct sigaction *)NULL) < 0) {
21+
perror("sigaction");
22+
exit(1);
23+
}
24+
25+
fprintf(stderr, "Input SIGINT: ");
26+
pause();
27+
fprintf(stderr, "After Signal Handler\n");
28+
29+
return 0;
30+
}

ch07/ex7_8.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <sys/ucontext.h>
2+
#include <unistd.h>
3+
#include <signal.h>
4+
#include <siginfo.h>
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
8+
void handler(int signo, siginfo_t *st, ucontext_t *uc) {
9+
psiginfo(sf, "Received Signal:"); // 시그널 핸들러 함수에서 psiginfo 함수를 호출해 시그널 정보를 출력한다.
10+
printf("si_code : %d\n", sf->si_code);
11+
}
12+
13+
int main(void) {
14+
struct sigaction act;
15+
16+
act.sa_flags = SA_SIGINFO; //SA_SIGINFO 플래그를 지정한다.
17+
act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler; // sa_handler 대신 sa_sigaction 멤버를 사용해 시그널 핸들러를 지정한다.
18+
sigemptyset(&act.sa_mask);
19+
if(sigaction(SIGUSR1, &act, (struct sigaction *)NULL) < 0) { // SIGUSR1 시그널을 지정해 sigaction 함수로 시그널 처리를 등록한다.
20+
perror("sigaction");
21+
exit(1);
22+
}
23+
24+
pause();
25+
26+
return 0;
27+
}

ch07/ex7_9.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <unistd.h>
2+
#include <signal.h>
3+
#include <siginfo.h>
4+
#include <stdio.h>
5+
6+
void handler(int signo) {
7+
psignal(signo, "Received Signal");
8+
}
9+
10+
int main(void) {
11+
sigset(SIGALRM, handler);
12+
13+
alarm(2);
14+
printf("Wait...\n");
15+
sleep(3);
16+
17+
return 0;
18+
}

ch08/ex8_1.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <sys/mman.h>
2+
#include <sys/stat.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
8+
int main(int argc, char *argv[]) {
9+
int fd;
10+
caddr_t addr;
11+
struct stat statbuf;
12+
13+
if(argc != 2) { // 명령행 인자로 파일명을 제대로 지정했는지 확인한다.
14+
fprintf(stderr, "Usage : %s filename\n", argv[0]);
15+
exit(1);
16+
}
17+
18+
if(stat(argv[1], &statbuf) == -1) { // stat 함수를 사용해 파일의 크기를 알아낸다.
19+
perror("stat");
20+
exit(1);
21+
}
22+
23+
if((fd = open(argv[1], O_RDWR)) == -1) { //파일을 열고
24+
perror("open");
25+
exit(1);
26+
}
27+
28+
addr = mmap(NULL, statbuf.st_size, PROT_READ|PROT_WRITE, // 파일을 열고 mmap 함수로 메모리 매핑을 한다.
29+
MAP_SHARED, fd, (off_t)0); // 매핑된 메모리 영역의 읽기와 쓰기가 가능하게 PROT_READ|PROT_WRITE 로 지정하고 MAP_SHARED 플래그를 지정한다.
30+
if(addr == MAP_FAILED) {
31+
perror("mmap");
32+
exit(1);
33+
} // 파일의 내용을 메모리에 매핑했으므로 파일을 닫느다. 열린 파일을 닫아도 매핑된 메모리 주소인 addr을 이용해 데이터를 읽거나 쓸 수 있다.
34+
close(fd);
35+
36+
printf("%s", addr); // addr이 가리키는 주소에 저장된 내용을 모두 출력한다.
37+
38+
return 0;
39+
}
40+
41+
// 먼저 명령행 인자로 지정한 파일인 mmap.dat의 내용을 살펴본 후 실행 결과를 살펴보면 파일의 내용이 모두 출력되었음을 확인할 수 있다.

ch08/ex8_1.out

8.77 KB
Binary file not shown.

ch08/ex8_2.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <sys/mman.h>
2+
#include <sys/stat.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
8+
int main(int argc, char *argv[]){
9+
int fd;
10+
caddr_t addr;
11+
struct stat statbuf;
12+
13+
if(argc != 2) {
14+
fprintf(stderr, "Usage : %s filename\n", argv[0]);
15+
exit(1);
16+
}
17+
18+
if(stat(argv[1], &statbuf) == -1) {
19+
perror("stat");
20+
exit(1);
21+
}
22+
23+
if((fd = open(argv[1], O_RDWR)) == -1) {
24+
perror("open");
25+
exit(1);
26+
}
27+
28+
addr = mmap(NULL, statbuf.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
29+
30+
if(addr == MAP_FAILED) {
31+
perror("mmap");
32+
exit(1);
33+
}
34+
close(fd);
35+
36+
printf("%s", addr);
37+
38+
if(munmap(addr, statbuf.st_size) == -1) {
39+
perror("munmap");
40+
exit(1);
41+
}
42+
43+
printf("%s", addr);
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)