
'공부이야기/Network'에 해당되는 글 23건
- 2008.11.28
- 2008.09.26
- 2008.09.26
- 2008.09.05
- 2008.06.12
- 2008.06.09
- 2008.06.09
- 2008.06.04
-
2008.05.26
fork 설명 2
- 2008.05.21
보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.
[ cron 이란 ? ]
특정 시간에 특정한 작업을 수행할 수 있게 해주는 프로그램이다.
대부분의 리눅스 배포판에 기본적으로 설치되어 있다.
내가 만약 매일 새벽 5시에 DB 백업을 받아야 한다고 가정한다면, 매일 DB 백업을
받기 위해 새벽 5시에 출근할 수는 없는 일이다. 이럴때 CRON 으로 작업을 스케줄링 해 놓
으면 cron 데몬이 알아서 백업을 받아준다.
[cron 설정하기 ]
RedHat 의 경우 /etc/crontab 이란 파일있다.
기본적으로 cron 데몬은 이 파일을 최초로 읽어 들인다.
[root@soma] vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
기본적인 crontab 내용이다.
[분] [시] [일] [월] [요일] [실행명령] 의 형식으로 이루어졌다.
위의 내용을 보면 /etc/crontab 을 읽어 들이면서 crontab 내용 안의 4 가지 파일을
설정된 시간에 맞춰서 실행한다.
1. 01 * * * * root run-parts /etc/cron.hourly
-> 매월,매일,매시,01 분에 /etc/cron.hourly를 실행한다.
2. 02 4 * * * root run-parts /etc/cron.daily
-> 매월,매일 4 시 02 분에 /etc/cron.daily 를 실행한다.
3. 22 4 * * 0 root run-parts /etc/cron.weekly
-> 매주일요일 4 시 22 분에 /etc/cron.weekly 를 실행한다.
4. 42 4 1 * * root run-parts /etc/cron.monthly
-> 매월 1일 4 시 42 분에 /etc/cron.monthly 를 실행한다.
[ 예제]
매일, 매시간10, 20 분에 /home/imsi/public_html/ 내의 모든 파일 및 디렉토리를
imsi.tar.gz 란 파일명으로 압축하고 나서 imsi.tar.gz 파일을 /home/imsi/back 이란
디렉토리로 이동하는 것을 설정해 보도록 하겠다.
1. /etc/crontab 설정하기
[root@tgs] vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
10,20 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
2. /etc/cron.hourly 스크립트 만들기
[root@tgs]vi /etc/cron.hourly/imsi
#!/bin/sh
tar cvfz /home/imsi/imsi.tar.gz /home/imsi/public_html/*
mv /home/imsi/imsi.tar.gz /home/imsi/back/
[ 옵션 사용하기 ]
1. crontab -l : 현재 설정되어있는 cron 의 리스트를 출력한다.
2. crontab -e : 이 옵션으로도 cron을 설정할 수 있다.
3. crontab -r : 이 옵션은 현재 설정되어있는 모든 cron 설정을 지운다.
etc/crontab 에 설정된 내용은 지워지지 않는다
7 TCP echo Echo
7 UDP echo Echo
9 TCP discard sink null Discard
9 UDP discard sink null Discard
13 TCP daytime Daytime
13 UDP daytime Daytime
17 TCP qotd quote Quote of the day
17 UDP qotd quote Quote of the day
19 TCP chargen ttytst source Character generator
19 UDP chargen ttytst source Character generator
20 TCP ftp-data File Transfer
21 TCP ftp FTP Control
23 TCP telnet Telnet
25 TCP smtp mail Simple Mail Transfer
37 TCP time Time
37 UDP time Time
39 UDP rlp resource Resource Location Protocol
42 TCP nameserver name Host Name Server
42 UDP nameserver name Host Name Server
43 TCP nicname whois Who Is
53 TCP domain Domain Name
53 UDP domain Domain Name Server
67 UDP bootps dhcps Bootstrap Protocol Server
68 UDP bootpc dhcpc Bootstrap Protocol Client
69 UDP tftp Trivial File Transfer
70 TCP gopher Gopher
79 TCP finger Finger
80 TCP http www, http World Wide Web
88 TCP kerberos krb5 Kerberos
88 UDP kerberos krb5 Kerberos
101 TCP hostname hostnames NIC Host Name Server
102 TCP iso-tsap ISO-TSAP Class 0
107 TCP rtelnet Remote Telnet Service
109 TCP pop2 postoffice Post Office Protocol - Version 2
110 TCP pop3 postoffice Post Office Protocol - Version 3
111 TCP sunrpc rpcbind portmap SUN Remote Procedure Call
111 UDP sunrpc rpcbind portmap SUN Remote Procedure Call
113 TCP auth ident tap Authentication Sevice
117 TCP uucp-path UUCP Path Service
119 TCP nntp usenet Network News Transfer Protocol
123 UDP ntp Network Time Protocol
135 TCP epmap loc-srv DCE endpoint resolution
135 UDP epmap loc-srv DCE endpoint resolution
137 TCP netbios-ns nbname NETBIOS Name Service
137 UDP netbios-ns nbname NETBIOS Name Service
138 UDP netbios-dgm nbdatagram NETBIOS Datagram Service
139 TCP netbios-ssn nbsession NETBIOS Session Service
143 TCP imap imap4 Internet Message Access Protocol
158 TCP pcmail-srv repository PC Mail Server
161 UDP snmp snmp SNMP
162 UDP snmptrap snmp-trap SNMP TRAP
170 TCP print-srv Network PostScript
179 TCP bgp Border Gateway Protocol
194 TCP irc Internet Relay Chat Protocol
213 UDP ipx IPX over IP
389 TCP ldap Lightweight Directory Access Protocol
443 TCP https MCom
443 UDP https MCom
445 TCP Microsoft CIFS
445 UDP Microsoft CIFS
464 TCP kpasswd Kerberos (v5)
464 UDP kpasswd Kerberos (v5)
500 UDP isakmp ike Internet Key Exchange (IPSec)
512 TCP exec Remote Process Execution
512 UDP biff comsat Notifies users of new mail
513 TCP login Remote Login
513 UDP who whod Database of who's logged on, average load
514 TCP cmd shell Automatic Authentication
514 UDP syslog
515 TCP printer spooler Listens for incoming connections
517 UDP talk Establishes TCP Connection
518 UDP ntalk
520 TCP efs Extended File Name Server
520 UDP router router routed RIPv.1, RIPv.2
525 UDP timed timeserver Timeserver
526 TCP tempo newdate Newdate
530 TCP,UDP courier rpc RPC
531 TCP conference chat IRC Chat
532 TCP netnews readnews Readnews
533 UDP netwall For emergency broadcasts
540 TCP uucp uucpd Uucpd
543 TCP klogin Kerberos login
544 TCP kshell krcmd Kerberos remote shell
550 UDP new-rwho new-who New-who
556 TCP remotefs rfs rfs_server Rfs Server
560 UDP rmonitor rmonitord Rmonitor
561 UDP monitor
636 TCP ldaps sldap LDAP over TLS/SSL
749 TCP kerberos-adm Kerberos administration
749 UDP kerberos-adm Kerberos administration
보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.
보호되어 있는 글입니다.
내용을 보시려면 비밀번호를 입력하세요.
IPC란 무엇인가? |
IPC를 쓰는 이유 |
IPC calls의 유사성과 차이점 |
기능 | 메세지큐 | 세마포어 | 공유메모리 |
1.IPC할당방법 | msgget | semget | shmget |
2.IPC제어방법 | msgctl | semctl | shmctl |
(상태변경,해제) | |||
3.IPC작동방법 | msgsnd | semop | shmat |
(send/receive) | msgrcv | shmdt |
현재 활동중인 상태를 보는 방법 |
T | Type= m (shared memory), s (semaphore) or q (message queues) |
ID | This is the identifier for the entry similar to a file descriptor. It is used by the operations function calls to access the resource after a get is performed on it. |
KEY | Similar to a file name, this is what the routine uses to get, or open, the resource. When you get this name, the return value is the ID. If the key is 0x00000000 (IPC_PRIVATE), this entry can only be used by related, parent/child processes. |
MODE | Permissions and status. The meaning of the field is different for each of the IPC types. |
R and S | These are for the message queues and indicate a process is waiting on a message send or receive call. |
D | Indicates the shared memory segment has been deleted but will not disappear until the last process attached to it releases it.(NOTE: The key will be changed to ipc_private, that is, all zeros (0) when the segment has been marked for removal but still has attached processes.) |
C | This shared memory segment will be cleared when the first process attaches to it.Flag not set. The next nine characters are permission bits with r indicating read access for this position; w indicating either write or alter access, depending upon the IPC facility it is attached to. No permissions for this operation. |
OWNER | The login name of the owner |
GROUP | The name of the group that owns the entry |
CREATOR | The login name of the creator of the entry |
CGROUP | The group of the creator of the entry |
CTIME | The time when the associated entry was created or changed |
CBYTES | The current total byte count of all messages in the queue |
QNUM | The number of messages currently in the queue |
QBYTES | The maximum number of bytes allowed for all messages in the queue |
LSPID | The ID of the last process that sent a message to the queue |
LRPIC | The ID of the last process that received a message in the queue |
STIME | The time the last message was sent to the queue |
RTIME | The time the last message was received, or read, from the queue |
NSEMS | The number of semaphores in the set for this entry |
OTIME | The time the last operations was done for this semaphore entry |
NATTCH | The current number of processes attached to this segment |
SEGSZ | The size of the segment in bytes |
CPID | The process ID of the creator of the segment |
LPID | The process ID of the last process to either attach or detach from this segment |
ATIME | The time of the last attach to the segment |
DTIME | The time of the last detach from the segment |
Semaphore |
Message queues |
Shared memory |
Memory mapping 에 대한 이해 |
P2P (peer-to-peer)
혼합형 P2P : 서버가 개입되는 혼합형
개방형 P2P : 서버 간섭이 없는 형
우선 예재 프로그램을 대충 만들어 보겠습니다.
각 section은 어떤 특정한 일을 하는 부분이고, pid, pid2는 미리 선언했다고 가정합시다.
// section A
pid = fork();
if (pid != 0) {
// section B
}
else {
// section C
pid2 = fork();
if (pid2 != 0) {
// section D
}
else {
// section E
}
// section F
}
section G
자.. 이제 이 프로그램을 컴파일해서 실행파일을 만들구요...
실행 파일을 실행 시키면 우선 첫 프로세스가 뜨겠죠? 이놈을 P1이라고 부릅시다.
P1은 프로그램이 짜여진대로 section A를 실행하고 pid = fork()문을 만납니다.
그리고 fork()를 실행하지요... 그러면 새로운 프로세스가 만들어지는데 P2라고 부릅시다.
P2는 P1의 환경을 그대로 가져옵니다. 즉, 지금 프로그램이 어디까지 실행되었는가라는
정보까지 그대로 가져오지요.. 그래서 P1도 P2도 모두 fork())이후의 pid = (fork()의 return 값)
pid에 대입하는 부분부터 실행합니다.
자.. 문제는 P1은 fork로부터 P2의 프로세스 ID를 받습니다.
새로 프로세스가 만들어졌으니 당연히 프로세스 ID가 있겠죠?
P2는 자기가 새로 만들어진 놈이므로 그냥 0을 return 받습니다.
따라서 똑같은 부분부터 시작은 하되.... fork()의 return값은 다른 놈을 가지고 P1, P2가 실행됩니다.
이 때, 우리는 주로 P1을 부모, P2를 자식이라고 설명합니다.
바로 다음 if문에 의해서
P1은 section B를 실행하게 되고 P2는 section C를 실행하게 됩니다.
return 받은 pid가 다르니까 if의 조건이 달라지죠?
P1은 section B를 실행한 후에 당연히 section G를 실행하고 끝납니다.
P2는 section C를 실행하고 다시 fork()를 만납니다.
fork를 실행하면 아시다시피 fork를 호출한 프로세스와 똑같은 프로세스가 하나 더
만들어 집니다.
즉 P2와 똑같은 놈 P3가 만들어지고 fork()로 부터 return을
P2는 P3의 프로세스 ID를 받고
P3는 자신이 fork되었으니 0을 받습니다.
그리고는 똑같은 위치인 pid2 = (fork의 return값) 을 실행합니다.
이때는 P2가 부모이고 P3가 자식이죠?
P2는 section D, F, G를 차례로 수행하고 끝날테구요..
P3는 section E,F,G를 수행하고 끝납니다.
여기서 주의할 점은 각 프로세스가 어느 놈이 먼저 수행될지는 아무도 모른다는 것이고,
이를 위해서 동기화 방법들이 있는 것입니다.