Professional Documents
Culture Documents
0.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.2 BSD? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
iii
0.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
iii
0.4
iv
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 FreeBSD make
1.1
FreeBSD make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
Makele . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.8 (#if#ifndef ) . . . . . . . . . . . . . . . . . . . . . . . .
12
1.10
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
1.12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15
1.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
2 BSD
20
2.1 BSD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
FreeBSD . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
24
2.2
28
3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
3.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
30
3.4 ID . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
36
39
4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
4.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
45
4.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
4.5 . . . . . . . . . . . . . . . . . . . . . . . .
48
4.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
4.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
Alarms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
4.8
5 I/O
55
5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
55
5.2
55
I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61
5.4 FLOCK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
64
5.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
5.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
6 I/O
68
6.1 I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
6.2
select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
6.3
poll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
70
6.4
kqueue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
73
6.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80
81
7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81
7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81
7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
84
7.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
87
8 FreeBSD 5.x
8.1
88
FreeBSD 5.X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
88
88
8.3
Devfs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
88
8.4
a.out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
8.5
gcc-3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
8.6
SMPng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
8.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
89
8.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
90
1
0.1
meilincore@chinaunix
X86 NetBSD
OpenBSD NetBSD
OS X Darwin
BSD Mach NeXTStep2
FreeBSD DEC AlphaPC-98 x86
Darwin
2
Steven Jobs
ii
BSD Aqua GUI
X Windows
FreeBSD BSD
FreeBSD
FreeBSD Handbook
0.2
BSD?
BSD
BSD
BSD Linux
BSD
BSD
BSD TCP/IP
0.3
BSD C C++
:
FreeBSD: http://www.freebsd.org/
OpenBSD: http://www.openbsd.org/
NetBSD: http://www.netbsd.org/
FreeBSD : http://www.freebsd.org/handbook/history.html
The FreeBSD Handbook: http://www.freebsd.org/handbook
iii
0.4
iv
1
1.1
FreeBSD make1
FreeBSD make2
Unix make
make
make
Unix shell make
shell
make GNU make System V make
make
FreeBSD make bmake
pmake FreeBSD make
world FreeBSD make
BSD
Makele
make
make
make
1
2
@chinaunix.net
Make: to t, intend, or destine by or as if by creating www.m-w.com
1.2
Makele
1.3
Makele \
shell #
########################################
# Simple Makefile with comment example #
########################################
# when run, it will just echo hello
all:
echo "hello"
make
Makele make
bash$ make
bash$ make all
bash$ make <target name>
1.4
:
::!
myprog:
<some commands to the compile myprog target>
another::
<some commands to the compile another target>
sample!
<some commands to the compile sample target>
\ newline
Makele make
all:
Makele
1.5
make
:
1. all: make
2.
app.h myprog.c myprog.o make
touch
touch myprog.c
::
1.
2.
3.
! make
?* [ ]
myprog.[oc]
{ }
{mypgog,test}.o
# the expression above would match myprog.o test.o only
shell
{dprog,aprog,bprog,cprog}.cpp
1.6
make a.c
b.c makele
a.c b.c
MYSRC = a.c
MYSRC = b.c
make
$(<>) $
$(GCC) = /usr/local/bin/gcc
make make
1.
5
2. make
3. Makele Makele
Makele
4. Makele make shell
Makele
1. = shell
#####################
# Example make file #
#####################
CURDIR != pwd
CFLAGS ?= -g
CFLAGS += -Wall -O2
all:
echo $CFLAGS
#######################
bash$ CFLAGS="-g -Wall" make
CURDIR shell pwd
backtick CFLAGS -g
-Wall -O2
1.7
make make
make shell
shell make
make
tab
Makele shell
CPP = -g++
CFLAGS = -Wall -O2
myprog.o:
$(CPP) $(CFLAGS) -c myprog.c
7
make myprog.c
@echo $(SOME_DEFINED_STRING)
- @
@
ECHO = echo
MESSAGE = "Print this message"
msg::
@$(ECHO) $(MESSAGE)
1.8
(#if#ifndef )
C C++ make
Makele
30 Makele
. .endif
AND && OR ||
! ! AND
OR
>>=<<=== !=
== !=
0
VER
VER .else TAG 2.4 stable
if/else
.ifmake debug
CFLAGS += -g
.elifmake production
CFLAGS += -O2
.endif
target(< arg >)
target
.if target(debug)
FILES += $(DEBUG_FILES)
.endif
debug FILES
empty(< arg >)
VAR $(VAR)
.if empty (CFLAGS)
CFLAGS = -Wall -g
.endif
10
.if defined(OS_VER)
.if $(OS_VER) == 4.4
DIRS += /usr/local/4.4-STABLE_src
.endif
.else
DIRS += /usr/src
.endif
.ifdef OS_VER
. if $(OS_VER) == 4.4
DIRS += /usr/local/4.4-STABLE_src
. endif
.else
OS_VER = 3.2
DIRS += /usr/src
.endif
make dene C
if
if
.if $(DEBUG) == 1
$(CFLAGS) = -g
.
ifndef $(DEBUG_FLAGS)
$(FLAGS) = $(DEBUG_FLAGS)
.
endif
.endif
exists(< arg >)
exists
tmp make -rm tmp/*.o .if
clean
clean:
-rm -f *.o *.core
.if exists(tmp)
-rm tmp/*.o
.endif
11
1.9
Makeles .include
C #include
make Makele
C
Makele Makele Makele
include .
.include file name
make Makele
.include <file name>
-I
C #include
.include "file name"
project.mk CFLAGS
Makele
.include "../project.mk"
CFLAGS = -g
FreeBSD Makele
FreeBSD Makele /usr/share/mk/
/etc/defaults/make.conf /etc/make.conf
man make.conf
Makele
Makele Makele bsd.<type>.mk
<type> Makele
bsd.dep.mk Makele
bsd.port.mk ports
12
.include Makele
Makele Makele
Makele Makele
Makele
Makele
1.10
make
System V
System V
.TARGET
old style notation: @
.ALLSRC
old style notation: >
.SUFFIX
.IMPSRC
old style notation: <
.OODATE
old style notation: ?
13
.PREFIX
old style notation: *
.ARCHIVE
old style notation: !
.MEMBER
old style notation: %
.TARGET.PREFIX.ARCHIVE
.MEMBER
.undef <variable>
.ifdef DEBUG
.undef RELEASE
.endif
1.11
14
1.12
man page
-D <variable name to dene>
Makele .ifdef
.ifdef DEBUG
CFLAGS += -g -D__DEBUG__
.endif
make -D DEBUG make CFLAGS
15
-f <makele to use>
Makele Makele
bash$
make -f Makefile.BSD
4
make -j 4 world
4 CPU
job
-n
Makele make
16
1.13
Makele Makele
.SUFFIXES C++
Makele
Makele Makele
Makele
########################################################
#
# FILE: Makefile
#
# AUTHOR: Nathan Boeger
#
# NOTES:
# This is a generic Makefile for *BSD make, you will
# need to customize the listed variables below inside
# the Makefile for your application.
#
# INSTALL_DIR = name of the directory that you want to install
#
this applicaion (Ex: /usr/local/bin/ )
#
# APP
= name of the application
#
# C_SRC
= C source files (Ex: pstat.c )
#
# CPP_SRC = CPP source files (Ex: node.cpp)
#
#
# $ Id: ch01.html,v 1.5 2004/08/10 14:41:39 nathan Exp $
#########################################################
# Make the OBJs from our defined C & C++ files
.ifdef CPP_SRC
OBJS
=
${CPP_SRC:.cpp=.o}
.endif
.ifdef C_SRC
17
OBJS
.endif
+=
${C_SRC:.c=.o}
> .depend
#######################################################
#
#
INSTALL SECTION
#
# install will copy the defined application (APP) into the
# directory INSTALL_DIR and chmod it 0755
# for more information on install read MAN(1) install
########################################################
install: all
install -b -s $(APP) $(INSTALL_DIR)
clean
rm -f $(APP) *.o *.core
# SUFFIX RULES
.SUFFIXES: .cpp .c .o
.c.o:
$(CPP) $(CFLAGS) -c ${.IMPSRC}
.cpp.o:
$(CPP) $(CFLAGS) -c ${.IMPSRC}
Makele
#######################################################
#
PROJECT Makefile
#
# This is what the programs makefile would look like
# These are the only variables you will need to define
######################################################
APP
C_SRC
= myapp
= debug_logger.c
18
CPP_SRC
INSTALL_DIR
= myapp.cpp base_classes.cpp
= /usr/local/bin/
19
2
2.1
BSD1
BSD2
FreeBSD i386
NetBSD OpenBSD i386
i386
2.2
FreeBSD
FreeBSD BIOS
DISK0 512
MBR
boot0boot1
512 slice boot2 boot2
loader program 512
1
2
meilincore@chinaunix
() www.m-
w.com
boot0
BIOS boot0 512
MBR /usr/src/sys/boot/i386/boot0
BIOS IDE
CPU
boot0
boot0
Linux DOS BSD
boot1
boot1
boot0 boot1 512
FreeBSD
boot2
21
boot1 boot0
FreeBSD
BIOS
FreeBSD PC fdisk slice
PC
boot1 boot0
boot1
boot1 0X7C00
boot1 BIOS
I/Oboot1
0X0boot1
boot2
boot2 boot2 boot2 BTX
boot Extender boot0 boot1 boot1
boot2 0X9000 0X9010 boot1
boot2 boot2 boot1
boot1 xread BIOS
boot1 boot2
boot2
... BIOS
I/O
FreeBSD F1 ASCII
boot2
: boot2 boot2
boot2
boot2 :
>>FreeBSD/i386 BOOT
Defualt: 0:ad(0,a)/boot/loader
boot:
22
boot2
boot kernel/kernel
boot(8)
boot2 BTX Boot Extender
BTX 86 Intel
Intel CPU
BTX
8088 80186Intel
CPU 16 20 16
20 16
4
20
Intel 1M Word
80386 1M
4G
16 386 32
32
BTX
23
BTX
BTX exit exec exit
BTX exec
CPU
BTX BTX
BIOS BTX BIOS
pushfpopfclistiiret hlt
BTX btxld
BTX
2.3
Forth cl
/boot/loader.conf
BSD
2.4
CPU
C
CPU MMU
24
1/sbin/init
1 & 2 C
0 CPU
CPU CPUi386
CPU MMX
CPU
CPU
I/O
C
init shell
/sbin/init
/sbin/init
/etc/ttys
init /etc/ttys
bash$ kill -HUP 1
init
UNIX
shell Linux System V
/etc/rc<n>.d BSD
/etc/ rc
/etc/rc.conf
PicoBSD PicoBSD
/usr/local/etc/rc.d/
.sh /etc/rc
/etc/rc.local
/etc/rc.local
BSD rc :
/etc/rc - rc
/etc/rc.atm - ATM
/etc/rc.devfs - /dev/
/etc/rc.diskless1 - diskless
/etc/rc.diskless2 - diskless
/etc/rc.firewall -
/etc/rc.firewall6 - IPV6
/etc/rc.i386 - intel
/etc/rc.isdn - isdn
/etc/rc.network - IPV4
/etc/rc.network6 - IPV6
/etc/rc.pccard - pc card
26
/etc/rc.serial -
/etc/rc.sysctl - sysctl
/usr/local/etc/rc.d/ -
rsync :
#!/bin/sh
if [ -x /usr/local/bin/rsync ]; then
/usr/local/bin/rsync --deamon
fi
rsync
27
3
3.1
1
2
BSD
BSD
19 60 Multics
3.2
BSD Netscape
3 BSD
Netscape Netscape BSD
PID
7.1
CPU(s)
CPU(s)
nice
Nice values
Priority
-20 - 0
0 - 20
nice
@chinaunix.net
processwww.mw.com
1
2
(scheduling algo calculation) + (nice value)
nice 0 nice
root niceBSD
0
CPU 20 time
0
$ time nice_code -1
29
bash$ time nice_code 20
root 0
root nice Unix
CPU root 0
CPU
3.3
CPU
CPU
FreeBSD
PID
Name
swapper
init
pagedaemon
vmdaemon
bufdaemon
syncer
init
pagedaemon
CPU
30
init
init init
ttys gettys
3.4
ID
PID PID
0 PID MAX FreeBSD
/usr/include/sys/proc.h PID MAX 99999
PID PID MAX
PID
getpid/getppid PID
grpID
BSD shells
bash$ ps auwx
| grep root
| awk {print $2 }
psgrepawk print
ID / ID getpgrp getppid
#include <sys/types.h>
#include <unistd.h>
pid_t
pid_t
pid_t
getpid(void);
getppid(void);
getpgrp(void);
proc ids.c
31
bash$ sleep 10 |
ps auwx |
| ./proc_ids
bash$ ps -j
shell PPID PGID
3.5
BSD 3
forkvfork rfork system 3
fork
fork
#include <sys/types.h>
#include <unistd.h>
pid_t
fork(void);
fork
PID 0
fork
Controlling terminal
Working directory
Root directory
Nice value
Resource limits
Real, eective and saved user ID
Real, eective and saved group ID
Process group ID
32
Supplementary group ID
The set-user-id bits
The set-group-id bits
All saved environment variables
All open le descriptors and current osets
Close-on-exec ags
File mode creation (umask)
Signals handling settings (SIG DFL, SIG IGN, addresses)
Signals mask
PID PPID PID
0
semiphorespipes signals
wait
wait :
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
#include <sys/time.h>
#include <sys/resource.h>
pid_t waitpid(pid_t wpid, int *status, int options);
pid_t wait3(int *status, int options, struct rusage *rusage);
pid_t wait4(pid_t wpid, int *status, int options, struct rusage *rusage);
wait options
WNOHANG wait
33
WUNTRACED
SIGTTINSIGTTOUSIGTSTP SIGSTOP
wait
WNOHANG status
status NULL WNOHANG
/usr/include/sys/wait.h
3
WIFEXITED(status) true 0
exit() exit()
WIFSIGNALED(status) true
WIFSTOPPED(status) true
WUNTRACED
ptrace
WEXITSTATUS(status) WIFEXITED(status)
true exit() exit() 8
WTERMSIG(status) WIFSIGNALED(status) true
WCOREDUMP(status) WIFSIGNALED(status)
true core dump
true
WSTOPSIG(status) WIFSTOPPED(status) true
34
init
vfork rfork
vfork fork 2.9BSD vfork
execv
vfork
Irix 5.x vfork vfork :
#include <sys/types.h>
#include <unistd.h>
int
vfork(void);
rfork(int flags);
rfork
RFPROC
RFNOWAIT
RFFDG
RFCFDG RFDG
RFMEM
C
rfork thread
35
#include <unistd.h>
int rfork_thread(int flags, void *stack,
int (*func)(void*arg), void *arg);
fork PID
-1
errno
RFSIGSHARE FreeBSD
FreeBSD 4.3
sigacts
RFLINUXTHPN FreeBSD
SIGUSR1 SIGCHILD
rfork linux clone
rfork fork 0 PID
rfork fork
RFFDG | RFPROC rfork rfork
-1 errno error man page
rfork fork
BSD Unix
NetBSD-1.5 rfork FreeBSD
OpenBSD pthreads
3.6
exec
shell
ls shell fork vfork exec exec
ls exec shell
perl
#
#!
interpreter [arg]
36
-w Perl
#!/usr/bin/perl
-w
exec exec.c (
)
#include <sys/types.h>
#include <unistd.h>
extern char **environ;
int
int
int
int
int
int
exec *argv[ ]
exec
execlp execvp
37
system
shell NULL shell 1
0 shell shell -1 fork
waitpid 127 shell
SIGINT SIGQUIT SIGCHILD
BSD
38
4
4.1
1
2
4.2
CPU
specify
SIGSTOP SIGKILL
1
2
gvim@chinaunix/bsd
4 a
BSD /usr/include/sys/signals.h3
NetBSD
#define SIGHUP
/* hangup */
SIGHUP Unix
#define SIGINT
/* interrupt */
#define SIGQUIT
/* quit */
#define SIGILL
SIGILL
pointer functions
5
#define SIGTRAP
FB5.2.1 signal.h
5
If your program makes use of use of threads, or pointer functions, try to
catch this signal if possible for aid in debugging. use of use of
3
40
SIGTRAP POSIX
#define SIGABRT
/* abort() */
SIGABRT (abort)
#define SIGFPE
SIGFPE
#define SIGKILL
SIGKILL
SIGKILL
I/O
#define SIGBUS
10
/* bus error */
CPU SIGBUS
#define SIGSEGV
11
/* segmentation violation */
SIGSEGV C/C++
dirty pointers6
http://en.wikipedia.org/wiki/Wild pointer
41
#define SIGSYS
12
SIGSYS
#define SIGPIPE
13
SIGPIPE
#define SIGALRM
14
/* alarm clock */
SIGALRM delivered
setitimer alarm
#define SIGTERM
15
SIGTERM
SIGTERM Unix kill
#define SIGURG
16
SIGURG
#define SIGSTOP
17
SIGSTOP
(stop) SIGCONT
SIGCONT
#define SIGTSTP
18
#define SIGCONT
19
SIGCONT
SIGCONT
SIGSTOP SIGSTP
#define SIGCHLD
20
(exit) wait
(terminating information
7
SIGCHLD
fork
SIGCHLD wait
SIGSTOPSIGSTP
SIGCONF SIGCHLD
#define SIGTTIN
21
SIGTTIN
SIGCONT
SIGCONT
#define SIGTTOU
22
43
SIGTTOU
SIGCONT
#define SIGIO
23
I/O SIGIO
fcntl
#define SIGXCPU
24
25
SIGXFSZ
#define SIGVTALRM 26
SIGVTALRM
#define SIGPROF
27
SIGPROF
#define SIGWINCH
28
xterm SIGWINCH
#define SIGUSR1
29
#define SIGUSR2
30
SIGUSR1 SIGUSR2
8
8
44
4.3
Kill
kill kill
int
Kill pid
IDUID
setuid
rootUID
SIGCONT
kill 9
PID 0 sig
PID 0 sig ID
PID -1 ID
ID
9
45
root proc
p ag P SYSTEM
could not be sentsig kill
sig 0kill
kill raise
int
raise(int sig);
raise sig
raise 0 -1 errno
signal 10
void (*signal(int sig, void (*func)(int)))(int);
4.4
signal
signal C
signal
FreeBSD typedef
typedef void (*sig_t) (int);
sig_t
10
as in: signal
46
func
int
voidsignal func
SIG IGN: func SIG IGN
SIG DFL: func SIG DFL
sigaction
sigaction signal
act sigaction
oact
int sigaction(int sig,const struct sigaction *act,struct sigaction *oact);
sigaction
void
(*sa_handler)(int);
void
siginfo t
context
sigset_t sa_mask;
bitwise mask
SIGKILL SIGSTOP
unblock sigprocmask
global masks
47
int
sa_flags;
SA NOCLDSTOP: SA NOCLDSTOP
SIGCHLDstop
SA NOCLDWAIT: SA NOCLDWAIT
SIGCHLD
wait
-1 APUE2ed 1
errno ECHILD
SA ONSTACK: sigaction
SA NODEFER: SA NODEFER
SA RESETHAND: SA RESETHAND
SIG DEF
SA SIGINFO: sigaction sa sigaction
SIG IGN SIG DFL
sigaction 0 -1 error
4.5
4.2BSD
SVR3
life and delivery
pending
48
SIGBUS
sigprocmask
sigprocmask
int sigprocmask(int how, const sigset_t *set, sigset_t *oset);
sigprocmask modify or examine set
null sigprocmask how
routine
int
sigemptyset(sigset_t *set)
set
int
sigfillset(sigset_t *set)
set
int
signo set
int
signo set
int
signo set
1 0
int
sigpending(sigset_t *set);
sigpending sigpending
sigpending
0 -1 errno
4.6
alternate signaltstack
signaltstack
int sigaltstack(const struct sigaltstack *ss, struct sigaltstack *oss);
char
*ss_sp;
MINSIGSTKSZ
SIGSTKSZ
signaltstack
size_t
ss_size;
ss size inaccurate
50
int
ss_flags;
SS DISABLE:
SS ONSTACK:
signaltstack oss null 0
-1errno
4.2BSD
BSD
siginterrupt :
int
sig ag 1 ag
0
4.7
sigsuspend sigmask
sigsuspend
sigsuspend
-1errno EINTR
int
sigwait set
sig
sigwait
51
int
SIGHUP SIGHUP
env;
setjmp 0 env
longjmp longjmp
env setjmp setjmp
longjmp va
setjmp longjmp
setjmp env longjmp setjmp
longjmp
jmp longjmp:
setjmp longjmp:
sigsetjmp siglongjmp: savemask 0
52
env setjmp
longjmp longjmperror longjmperror
longjmperror longjmperror
longjmperror longjmp botch
4.8
unsigned int
Alarms
100,000,000
int getitimer(int which, struct itimerval *value);
getitimer which itimerval
int
#define ITIMER_REAL
CPU
natural timebased on atural real
time SIGALRM
#define ITIMER_VIRTUAL
1
53
CPU CPU
SIGVTALRM
#define ITIMER_PROF
Prole CPU
prole
SIGPROF SIGPROF
I/O
I/O
54
I/O1
5
5.1
Unix
FreeBSD 5 /dev
Plan 9Plan 9
Plan 9
exec
exec
I/O
5.2
I/O
UNIX
UNIX 3
0
1
1
horsemanHUGH CHEN
2
shell
, cat /etc/hosts >> hosts.out, shell hosts.out cat /etc/hosts
cat 1 tty
hosts.out cat
Open Close
Open
int
mode */ );,
Open
exec fork
0 fork exec
close-on-exec fork
exec
0
Close
int
close(int fd);
close
exit
exit
0
getdtablesize
int
getdtablesize();
56
getdtablesize
getdtablesize
fcntl
int
fcntl fcntl ,
fnctl
FreeBSD /usr/include/fcntl.h
#define F_DUPFD
F DUPFD dup
F DUPFD fcntl
fcntl
fcntl
fcntl
O RDONLY, O RDWR,
O WRONLY
close-on-exec exec
57
F GETFD
#define
F_GETFD
F SETFD
#define
F_SETFD
F SETFD close-on-exec
FD CLOEXEC close-on-exec 0 close-on-exec
F GETFL F SETFL
#define
#define
F_GETFL
F_SETFL
3
4
open open
#define
O_RDONLY 0x0000
O RDONLY
O RDONLY fnctl F SETFL
#define
O_WRONLY 0x0001
58
O WRONLY
open fcntl F SETFL
#define
O_RDWR
0x0002
O RDWR
open
#define
O_NONBLOCK
0x0004
O NONBLOCK
ttyread
O NONBLOCK read EAGAIN
#define
O_APPEND 0x0008
O APPEND
#define
O_SHLOCK 0x0010
O SHLOCK
O_EXLOCK 0x0020
O EXLOCK
fcntl F SETLK F GETLK
#define
O_ASYNC
0x0040
O ASYNC SIGIO
IO
#define
O_FSYNC
0x0080
59
O FSYNC
#define
O_NOFOLLOW
0x0100
O NOFOLLOW open
#define
O_CREAT
0x0200
O CREAT open
(when one of the original creators of C was asked
What one thing would you change about C? he replied, I would change O CREAT
to O CREATE!, or at least how the rumor goes.)
#define
O_TRUNC
0x0400
O TRUNC open
#define
O_EXCL
0x0800
O EXCL open
#define F_GETOWN
F GETOWN SIGIO
#define F_SETOWN
IO F SETOWN SIGIO
ID fcntl
fcntl
60
5.3
scheme fcntl
fcntl
struct flock {
off_t
l_start;
off_t
l_len;
pid_t
l_pid;
short
l_type;
short
l_whence;
};
/*
/*
/*
/*
/*
starting offset */
len = 0 means until end of file */
lock owner */
lock type:
*/
type of l_start */
l start
l whence
l_whence + l_start
l len
l_whence + l_start
l len l len 0 l len
l pid
ID(PID)
l type
61
F RDLCK -
F WRLCK -
F UNLCK -
l whence
l start
SEEK CUR -
SEEK SET -
SEEK END -
fcntl
fcntl
#define
F_GETLK
F GETLK fnctl
fnctl ock
ock l type
F UNLCK
#define
F_SETLK
F SETLK ock
fcntl EAGAIN errno ock
l type F UNLCK
#define
F_SETLKW
62
5.4
FLOCK
POSIX.1
exec
exec
exec
BSD ock
ock BSD fcntl ock
ock
DUP() OPEN()
ock fcntl
ock
#define
LOCK_SH
0x01
LOCK SH fcntl
#define
LOCK_EX
0x02
LOCK EX
#define
LOCK_NB
0x04
ock
LOCK NB ORedock EWOULDBLOCK 0
1
#define
LOCK_UN
0x08
/* unlock file */
63
LOCK UN
ockock
DUP
int dup(int old);
fcntl dup
dup old
read()write() lseek()
fcntl close on exec , close on exec
exec
dup old
DUP2
DUP2 -1 errno
5.5
System V IPC
BSD IPC
64
open BSD
PIPE
int pipe(int *array);
pipe int array[2]pipe
, array[0],array[1]
pipe ,
Unix shell
bash$ find /
-user frankie
| grep -i jpg
| more
nd grep grep
more shell
fork,
1. SIGPIPE
2. 0
pipe 0 -1 errno
BSD pipe
65
Mkfo
int mkfifo(const char *path, mode_t mode);
mkfo Mkfo
FIFO
FIFO FIFO
FIFO Mkfo
null
unix
/usr/include/sys/stat.h S IRUSR, S IRGRP
mkfo open fo
-1
fo fo
fo ID ID ID
ID
FIFO fo
O NOBLOCK
0 SIGPIPE
5.6
FIFO
FIFO FIFO
66
O NONBLOCK
FIFO
FIFO
FIFO
FIFO FIFO
/usr/local/myapp/fo dir
FIFO
ftok
5.7
fork exec
FIFO Queue
BSD
67
6
6.1
I/O1
I/O
fopipe socket
pine
email
O NONBLOCK I/O
I/O
CPU CPU
I/O
I/O
CPU I/O
web
100
I/O
fo write
write
select()poll() kqueue()
I/O
@chinaunix.net
I/O
select
select select read
readread
6.2
select
select()
int
select nfds
1 {0,1,8}nfds
9 8
1 4
select
select
readfdswritefds exceptfds man(2) select
readfdswritefds execptfds select
NULL
readfdswritefds execptfds 4
FD ZERO(&fdset);
FD ZERO bit
select select
FD SET(fd, &fdset);
FD SET
69
FD CLR(fd, &fdset);
FD CLR
FD ISSET(fd, &fdset);
FD ISSET select
I/O
select NULL select
timeval timeval
struct timeval {
long
tv_sec;
long
tv_usec;
};
/* seconds */
/* microseconds */
select select
0 -1 errno
6.3
poll
POLLERRPOLLHUP POLLNVAL
select poll
poll pollfd
struct pollfd {
int
fd;
short
events;
short
revents;
};
fd
fd poll
fd -1
revents
events, revents
events bit revents
bit poll
#define POLLIN
0x0001
POLLIN
socket
#define POLLPRI
0x0002
POLLPRI
#define POLLOUT
0x0004
#define POLLWRNORM
POLLOUT
POLLOUT POLLWRNOMR
FreeBSD OpenBSD
/usr/include/poll.h
POLLWRNOMR 0
71
#define POLLRDNORM
0x0040
POLLRDNORM
POLLIN NetBSD
FreeBSD POLLIN
/usr/include/poll.hPOLLRDNORM 0
#define POLLRDBAND
0x0080
POLLRDBAND 0
#define POLLWRBAND
0x0100
POLLWRBAND 0
FreeBSD
FreeBSD
poll
UFS
kqueue
#define POLLEXTEND
0x0200
POLLEXTEND
#define POLLATTRIB
0x0400
POLLATTIB
#define POLLNLINK
0x0800
POLLNLINK
72
#define POLLWRITE
0x1000
POLLWRITE
pollfd events poll
pollfd revents
#define POLLERR
0x0008
POLLERR
#define POLLHUP
0x0010
0x0020
POLLNVAL poll
poll poll
-1poll
0 poll
poll
poll 0poll -1
6.4
kqueue
poll select
kqueue
73
kqueue
kqueue
kqueue
kqueue API
int kqueue(void);
kqueue kqueue
kqueue kqueue
kqueuekqueue
fork
rfork RFFDG
kqueue -1 errno
int kevent(int kq, const struct kevent *changelist,
int nchanges, struct kevent *eventlist, int nevents,
const struct timespec *timeout);
kevent kqueue kqueue changelist
nchanges kevent changelist
kqueue
eventlist nevents kevent kevent
eventlist eventlist changelist
kevent
NULLkevent NULL
74
kevent 0 kevent
kevent eventlist
eventlist kevent
eventlist
EV ERROR data
-1 errno
kevent kqueue FreeBSD /usr/include/sys/event.h
kevent select
poll kqueue
kevent
struct kevent {
uintptr_t
short
u_short
u_int
intptr_t
void
};
ident;
filter;
flags;
fflags;
data;
*udata;
ident
ident kqueue
ident
lter
lter ident
ags
ags
ags
75
ags
ags ags
data
data
udata
udata kqueue kqueue
kqueue
kqueue
kevent ags
#define EVFILT_READ
(-1)
vnode
data
pipe fodata
EV EOF bit
socket EVFILT READ kqueue
#define EVFILT_WRITE
(-2)
EVFILT WRITE
pipefo socket data
EV EOF bit
#define EVFILT_AIO
(-3)
76
(-4)
0x0001
NOTE DELETE ag
#define NOTE_WRITE
0x0002
NOTE WRITE ag
#define NOTE_EXTEND
0x0004
NOTE EXTEND ag
#define NOTE_ATTRIB
0x0008
NOTE ATTRIB ag
#define NOTE_LINK
0x0010
NOTE LINK ag
link man(2)
link
#define NOTE_RENAME
0x0020
NOTE RENAME ag
#define NOTE_REVOKE
0x0040
77
(-5)
EVLILT PROC
PID ident ags
ags OR
#define NOTE_EXIT
0x80000000
NOTE EXIT ag
#define NOTE_FORK
0x40000000
0x20000000
0x00000001
0x00000002
NOTE TRACKERR ag
ag
#define NOTE_CHILD
0x00000004
NOTE CHILD ag ag
#define EVFILT_SIGNAL
(-6)
78
EVFILT SIGNAL
data SIG IGN
kqueue
EV CLEAR
#define EVFILT_TIMER
(-7)
0x0001
EV DISABLE
#define EV_DELETE
0x0002
EV DELETE kqueue
#define EV_ENABLE
0x0004
EV ENABLE kqueue
#define EV_DISABLE
0x0008
79
kqueue
kqueue
OR
#define EV_ONESHOT
0x0010
EV ONESHOT kqueue
#define EV_CLEAR
0x0020
kqueue
kevent ags
#define EV_EOF
0x8000
EV EOF
#define EV_ERROR
0x4000
EV ERROR data
6.5
BSD
selectpoll kqueue
kqueue kqueue
I/O
FreeBSD 5.x
80
7
7.1
BSD UNIX
CPU
UNIX
BSD
7.2
getrlimitsetrlimit
getrlimit ./
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
int
int
getrlimit setrlimit :
struct rlimit {
rlim_t rlim_cur;
rlim_t rlim_max;
};
1
FinalBSD
getrlimit setrlimit
/usr/include/sys/resource.h
#define
RLIMIT_CPU
SIGXCPU
#define
RLIMIT_FSIZE
RLIMIT FSIZE
RLIMIT FSIZE 0
SIGFSZ
#define
RLIMIT_DATA 2
/* data size */
RLIMIT DATA
(C/C++ malloc() )
#define
RLIMIT_STACK
/* stack size */
RLIMIT STACK
SIGSEV
#define
RLIMIT_CORE 4
#define
RLIMIT_RSS
#define
RLIMIT_MEMLOCK 6
#define
RLIMIT_NPROC
/* number of processes */
RLIMIT NPROC
ID
#define
RLIMIT_NOFILE
RLIMIT NOFILE
#define
RLIMIT_SBSIZE
RLIMIT_VMEM 10
RLIMIT VMEM
mmap
#define
RLIM_INFINITY
RLIM INFINITY
RLIM INFINITY
RLIM INFINITY
getrlimit rlimit
getrlimit setrlimit
0
RLIM INFINITY 0
-1 errno
83
getpagesize
#include <unistd.h>
int
getpagesize(void);
getrusage getpagesize
(pages) 4096
(hard-coded)
(pagesize) getpagesezi getpagesize
7.3
getrusage
getrusage
CPU getrusage
runaway
CPU
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#define
#define
int
RUSAGE_SELF
0
RUSAGE_CHILDREN
-1
rusage /usr/include/sys/resource.h
struct
rusage {
84
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
};
ru utime, ru stime
ru utime ru stime
timeval
ru maxrss
ru maxrss
ru ixrss
ru ixrss text segment
ru idrss
ru idrss KB
ru isrss
ru isrss KB
ru mint
ru mint I/O
85
ru majt
ru majt I/O
ru nswap
ru nswap
ru inblock
ru inblock
ru oublock
ru oublock
ru msgsnd
ru msgsnd IPC
ru msgrcv
ru msgrcv IPC
ru nsignals
ru nsignals
ru nvcsw
CPU
ru nivcsw
ru nivcsw
86
7.4
4096
64 8096 32
4096
87
FreeBSD 5.x1
8
8.1
FreeBSD 5.X
8.2
FreeBSD 5.x
/boot FreeBSD
/ /boot()
8.3
Devfs
Devfs devfs
chroot jail /dev
devfs
1
FinalBSD
devfs cd
/dev
8.4
a.out
gcc-3.2
8.5
8.6
SMPng
8.7
(KSE)KSE
Scheduler Activations KSE FreeBSD
POSIX
KSE
KSE libpthreads libpthreads
libpthreads
89
8.8
FreeBSD
SMP FreeBSD
Sparc64 ia64
BSD
90