황준서 학생에게 segment tree with lazy propagation에 대해서 가르쳐 주었다.
그 후 본인도 관련 심화 문제들을 풀어 보면서 개념을 다시 한 번 상기시켰다.
이전 소켓 프로그래밍 실습에 이어서 추가 공부를 진행했다.
이번주차에는 소켓구조체의 실제 구조에 대해서 공부하였고, Linux에서는 소켓 또한 파일로 관리 된다는 것을 알게 되었다. 그리고 이를 확인하기 위한 실습 코드를 작성하였다.
파일 입/출력 복습 코드:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
void error_handling(char* message);
int main(void){
int fd;
char buf[] = "Let's go!\\n";
fd = open("data.txt", O_CREAT | O_WRONLY | O_TRUNC);
if (fd == -1) error_handling("open() error!");
printf("file descriptor: %d \\n", fd);
if (write(fd, buf, sizeof(buf)) == -1) error_handling("write() error!");
close(fd);
return 0;
}
void error_handling(char *message){
fputs(message, stderr);
fputc('\\n', stderr);
exit(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 100
void error_handling(char* message);
int main(void){
int fd;
char buf[BUF_SIZE];
fd = open("data.txt", O_RDONLY);
if (fd == -1) error_handling("open() error!");
printf("file descriptor: %d \\n", fd);
if (read(fd, buf, sizeof(buf)) == -1) error_handling("read() error!");
printf("file data: %s", buf);
close(fd);
return 0;
}
void error_handling(char *message){
fputs(message, stderr);
fputc('\\n', stderr);
exit(1);
}
실습코드:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
int main(void){
int fd1, fd2, fd3;
fd1 = socket(PF_INET, SOCK_STREAM, 0);
fd2 = open("test.dat", O_CREAT | O_WRONLY | O_TRUNC);
fd3 = socket(PF_INET, SOCK_DGRAM, 0);
printf("file descriptor 1: %d\\n", fd1);
printf("file descriptor 2: %d\\n", fd2);
printf("file descriptor 3: %d\\n", fd3);
close(fd1); close(fd2); close(fd3);
return 0;
}
위 코드를 실행해보면 fd1, 2, 3에 각각 운영체제가 file descriptor 즉 파일 번호를 부여해 준다는 사실을 확인 할 수 있다. 이는 운영체제에서 소켓과 파일을 동일하게 취급한다는 사실을 알 수 있게 해준다.
푼 문제 정리