페이징기법에서, 실제 얻어서 사용하는 포인터의 번지는 물리 메모리의 번지와는 다르다. 이런식으로 메모리를 맵핑해주는 개념이 필요하다.
geshi c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
typedef struct _cData { int num; char str[12]; } cData ;
#define NUM 20
int main()
{
cData m, *pm;
int fd, n ;
fd = open("test.dat", O_RDWR|O_CREAT|O_TRUNC, 0644) ;
for(n=0; n<NUM; n++) {
m.num = n ;
sprintf(m.str, "Arminia %2d", n);
write(fd, &m, sizeof(cData)) ;
}
close(fd) ;
fd = open("test.dat", O_RDWR) ;
m.num = 70 ;
sprintf(m.str, "Arminia %2d", 70) ;
lseek(fd, sizeof(cData)*10, SEEK_SET) ;
write(fd, &m, sizeof(cData)) ;
pm = (cData *) mmap(0, NUM*sizeof(cData), PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0) ;
pm[15].num = 90 ;
sprintf(pm[15].str, "Arminia %2d", 90) ;
msync((void *)pm, NUM*sizeof(cData), MS_ASYNC);
munmap((void *)pm, NUM*sizeof(cData)) ;
close(fd);
exit(0);
}
frame buffer나 대용량 데이터에 접근할 경우, 이와 같이 mmap을 이용 Mapping방법을 통해 접근한다.
이러한 방식은 특정 메모리를 I/O 영역으로 할당. 디바이스 제어에도 쓰일 수 있다.
geshi c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int main()
{
pid_t pid; int fd ; FILE *fp;
char str[4444][20] = { "Arminia-209 ", "Ellias-404 ",
"GL-209 ", "Deillous-404 " };
printf("\n[ Processs PID:%d PPID:%d ]\n\n", getpid(), getppid()) ;
fd = open("test.dat", O_RDWR|O_CREAT|O_TRUNC, 0644) ;
fp = fopen("temp.dat", "w") ;
write(fd, str[0], 20) ; fputs(str[1], fp) ;
switch(pid=fork()) {
case -1:
perror("[ Fork Error ]\n") ;
exit(0);
case 0:
printf("[ Child PID:%d PPID:%d ]\n", getpid(), getppid());
printf("[ Child END ]\n");
break;
default:
printf("[ Parent PID:%d PPID:%d ]\n", getpid(), getppid() );
printf("[ Parent END ]\n");
break;
}
write(fd, str[2], 20) ; fputs(str[3], fp) ;
printf("[ Process End : %d ]\n\n", getpid() );
exit(0);
}
결과값은 아래와 같이 예상되나...
test.dat =>
Arminia-209 GL-209 GL-209
temp.dat =>
Ellias-404 Deillous-404 Deillous-404
이와 같은 결과가 나온다.
test.dat =>
Arminia-209 GL-209 GL-209
temp.dat =>
Ellias-404 Deillous-404 Ellias-404 Deillous-404
why? 저수준 함수(open)과 라이브러리 함수(fopen)을 함께 사용하면서, 남아있는 찌거기가 같이 써지게 된다.
geshi c
#include <stdio.h>
typedef int Fun(int, int);
typedef Fun *pFun ;
#define function(pm, x, y) pm(x, y) ;
Fun add, sub, mul, div ; // 함수선언과 동일.
void main()
{
int a =100, b = 20, c ;
pFun fun[4] = { add, sub, mul, div } ;
c = function(mul, a, b); printf("[ %d * %d = %d]\n, a, b, c);
c = function(fun[0], a, b); printf("[ %d + %d = %d]\n, a, b, c);
}
int add(int x, int y) { return(x+y) ; }
int sub(int x, int y) { return(x-y) ; }
int mul(int x, int y) { return(x*y) ; }
int div(int x, int y) { return(x/y) ; }
좀비 프로세스
geshi c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
pid_t pid, cpid ; int status, n ;
printf("\n[ Process PID:%d PPID:%d ]\n\n", getpid(), getppid()) ;
switch(pid=fork()) {
case -1:
perror("[ Fork Error ]\n") ;
exit(0);
case 0:
printf("[ Child PID:%d PPID:%d ]\n", getpid(), getppid());
sleep(1);
printf("\n[ Child Process END :%d ]\n\n", getpid());
exit(0xFE) ;
default:
printf("[ Parent PID:%d PPID:%d ]\n", getpid(), getppid() );
for(n=0; n<7; n++)
sleep(3), write(1,". ",4) ;
printf("\n[ Wait Fun ]\n") ;
cpid = wait(&status);
printf("[ CPID : %d ]\t\t[ Status : %x]\n", cpid, status) ;
printf("[ Parent Process END : %d ]\n\n", getpid());
break;
}
exit(0);
}
Signal
geshi c
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
void Signal_INT_Handler( int ) ;
int main()
{
int n = 0;
signal(SIGINT, Signal_INT_Handler);
for(n=0; n<10; n++) {
sleep(1);
write(1,". ", 4) ;
}
printf("\n{ Process End ]\n") ;
exit(0);
}
void Signal_INT_Handler(int num)
{
printf("\n[ Signal INT Handler : %d ]\n", num);
}