You are on page 1of 98

FreeBSD

Nathan Boeger Mana Tominaga


ChinaUnix
2006 3 14

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 ) . . . . . . . . . . . . . . . . . . . . . . . .

1.9 Makeles .include . . . . . . . . . . . . . . . . . . .

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

BSD AT&T UNIX


BSD Marshall Kirk McKusick
Open Source: Voice from the Open Source RevolutionOReilly, 1999

BSD 20 70 AT&T Unix


Unix
Unix
berkeley System Distribution BSD Unix
BSD BSD
BSD TCP/IP InternetTCP/IP
1989 Net/1
BSD AT&T Unix
BSD Unix OS
AT&T 2. Net/2
1991 6 386/BSD
386/BSD
NetBSDFreeBSD BSDI
1992 AT&T Unix USL BSDI BSDI
Net/2 1994 4.4BSDLite Net/2 USL
USL NovellNetBSDFreeBSD BSDI 4.4BSD-Lite
1995 4.4BSD-Lite release 2 CSRG
BSD CSRG
Linux BSD
Linux FreeBSDOpenBSDNetBSD
DarwinFreeBSD
1

meilincore@chinaunix

X86 NetBSD
OpenBSD NetBSD
OS X Darwin
BSD Mach NeXTStep2
FreeBSD DEC AlphaPC-98 x86

PowerPC Sparc64 BSD FreeBSD


Yahoo
7883 FreeBSD Linux FreeBSD
Linux Linux
FreeBSD
Unix Ports FreeBSD
8000
Ports NetBSD OpenBSD Ports
FreeBSD BSD
200 committer
FreeBSD committer
commiter
FreeBSD 386 BSD
Virginia Tech Macintosh Atari StAmiga
PC FreeBSD i386 NetBSD
NetBSD BSD
Sega Dreamcast
NetBSD Wasabi Systems NetBSD
NetBSD
OpenBSD NetBSD SPARC Theo de raadt
OpenBSD 1996 10
OpenBSD de Raadt
OpenBSD
OpenSSH SSH OpenSSH
SSH OpenBSD FreeBSD

Darwin
2

Steven Jobs

ii


BSD Aqua GUI
X Windows
FreeBSD BSD
FreeBSD
FreeBSD Handbook

0.2

BSD?

BSD
BSD

BSD Linux
BSD

Absolute BSD Michael Lucas BSD


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

CC++ Java Nathan Boeger 1.X


FreeBSDNathan
GetRelevantRedline Networks Penguin Computing

Mana Tominaga 1998


Fawcette Technical Publication VBPJ DevX.com
CMP Media Mana

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

man page 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

make Makele Makele


make Makele
makele Makele
Makele -f (lename)
make -f OtherMakefile

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: driver.cpp network_class.cpp file_io_class.cpp \


network_libs.cpp file_io_libs.cpp
all: myprog.o
myprog.o:
all myprog.o make myprog.o
make myprog.o all
make
all: make
all: all: make

all:

all: myprog.o lib


lib: lex
lex:
myprog.o: app.h
all: myprog.o lib
make myprog.o make myprog.o
3

app.h app.h makele app.h

myprog.o make all:


lib lib lex make lib
lex:

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

VAR = < value >


2. +=

VAR += < value to append >


3. ?=

VAR ?= < value if unset >


4. :=

VAR := < value to expand >


5. != shell shell
newline
VAR != < shell command to execute >
Makele /etc/make.conf
/etc/defaults/make.conf

#####################
# 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

# example for an all-install target


all-install:
$(CC) $(CFLAGS) $(MYSRC)
cp $(MYPROG) $(INSTALL_DIR)
echo "Finished the build and install"

tab
Makele shell

CPP = -g++
CFLAGS = -Wall -O2
myprog.o:
$(CPP) $(CFLAGS) -c myprog.c
7

make myprog.c

CLFAGS = $(LINK_FLAGS) $(LINK_LIBS) $(OTHER_LIBS) \


$(OPTIMIZER_FLAGS) $(DEFINES) $(NO_KERNEL) $(OBJS) \
$(CPU_TYPE_FLAGS) $(USE_MY_MALLOC) $(UDEFINES) \
$(SRC_FILE) $(OTHER_SRC_FILES)
make object core
log
CPP = -g++
CFLAGS = -Wall -O2
APP = myapp
DATE != date +%m%d%y_%H_%M
LOG = debug
myprog.o:
$(CPP) $(CFLAGS) -c myprog.c
rm -f *.o *.core $(APP)
mv $(LOG).log $(LOG)_$(DATE).log
clean
rm -f *.o *.core $(APP)
mv $(LOG).log $(LOG)_$(DATE).log
log make
- make
make
make
clean:
-rm -f *.o *.core $(APP)
-mv $(LOG).log $(LOG)_$(DATE).log
make rm mv
make echo echo make
echo
echo $(SOME_DEFINED_STRING)
echo @ make

@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 $(VER) >= 2.4


TAG = 2.4_current
.elif $(VER) == 2.3
TAG = 2.3_release
.else
TAG = 2.4_stable
.endif

if/else

make(< arg >) short hand [ .ifmake, .ifnmake, .elifmake, .elifnmake ]


make
make make()
CFLAGS
.if make(debug)
CFLAGS += -g
.elif make(production)
CFLAGS += -O2
.endif

.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

dened(< arg >) short hand [ .ifdef , .ifndef , .elifdef, elifndef ]

.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 make man


page make

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

.SUFFIXES: (sux list)


.cpp.cc.c
a.omake
.SUFFIXES
Makele .SUFFIXES target

14

.SUFFIXES: .cpp .c .o .sh


.c.o:
$(CC) $(CFLAGS) -c ${.IMPSRC}
.cpp.o:
$(CPP) $(CXXFLAGS) -c ${.IMPSRC}
.sh:
cp ${.IMPSRC} ${.TARGET}
chmod +x ${.TARGET}
C C++ .sh:
make shell shell
.sh shell .sh

.sh install script


.sh shell install script.sh
make
make
apps.h
all: install_script $(OBJS)
$(CPP) $(CFLAGS) -o $(APP) $(OBJS) $(LINK)

1.12

man page
-D <variable name to dene>
Makele .ifdef

.ifdef DEBUG
CFLAGS += -g -D__DEBUG__
.endif
make -D DEBUG make CFLAGS

15

-E < variable name to override >


Makele
shell
bash $ CFLAGS="-O2 -Wall" make -E CFLAGS
-e
-e Makele

-f <makele to use>
Makele Makele

bash$

make -f Makefile.BSD

-j < number of max jobs >


make job make

4
make -j 4 world
4 CPU
job
-n
Makele make

bash $ make -n >> make_debug.log 2>&1

16

-V < variable name >


make
-V
make -V CFLAGS -V LINK -V OBJS

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}

# define the Compiler. The compiler flags will be appended to


# if defined, else they are just assigned the values below
CPP
=
g++
CFLAGS
+=
-Wall -Wmissing-prototypes -O
LINK
+=
-lc
# Add a debug flag.
.ifdef DEBUG
CFLAGS += -g
.endif
# Targets
all: ${OBJS}
$(CPP) $(CFLAGS) -o $(APP) ${OBJS} $(LINK)
depend:
$(CPP) -E -MM ${C_SRC} ${CPP_SRC}

> .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/

# And include the template Makefile, make sure its


# path is correct.
.include "../../bsd_make.mk"

19

2
2.1

BSD1
BSD2

FreeBSD i386
NetBSD OpenBSD i386
i386

PPC Alpha i386 BSD


Mac OS X

i386 Given the install


basei386 64 CPU

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

0C: BIOS 0X80


512 0X7C00BIOS
0X7DFE 0XAA55
0XAA55 i386
0X7DFE BIOS
boot0 0X7C00
Intel i386
0X0 BIOS 0X7C00

CPU

I/O BIOS Intel CPU


boot0
boot0
boot0 boot0

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

... MMUmemory management unit


BTX BTX
BTX
FreeBSD
BTX

BTX

23


BTX
BTX exit exec exit
BTX exec

CPU
BTX BTX
BIOS BTX BIOS
pushfpopfclistiiret hlt
BTX btxld
BTX

2.3

Forth cl

rootdevice module path


/boot/loader.conf
/boot/defaults/loader.conf
/etc/rc.conf

/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

BSD cpu startup()


3 0
cpu startup() 0 0
ps swapper
FreeBSD
pagedaemonvmdaemonbufdaemon syncer
VM

C
init shell

/sbin/init FreeBSD 1,700


BSD
/sbin/init
/sbin/init
/sbin/init
25

/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

Higher priority for execution

0 - 20

Lower priority for execution

nice
@chinaunix.net
processwww.mw.com
1
2


(scheduling algo calculation) + (nice value)

nice 0 nice
root niceBSD

int getpriority(int which, int who);


int setpriority(int which, int who, int prio);

int nice(int incr);


nice nice incr
nice nice
setpriority()
-1 getpriority
errno() getpriority
errno errno man(2)
erron nice code.c setpriority getpriority
which who
nice code.c nice root -20

0
CPU 20 time

bash$ time nice_code 10

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);

FreeBSD man pages


PID PID
PID PID

proc ids.c

31

bash$ sleep 10 |

ps auwx |

awk {print $2}

| ./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

WLINUXCLONE linux clone


wait4 waitpid wpid PID
-1 rusage
NULL

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 fork vfork Plan9


FreeBSD/OpenBSD
Linux clone rfork fork
rfork
#include <unistd.h>
int

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

execl(const char *path, const char *arg0,


... const char *argn,
NULL);
execle(const char *path, const char *arg0,
... const char *argn, NULL, char *const envp[]);
execlp(const char *file, const char *arg0,
... const char *argn , NULL );
exect(const char *path, char *const argv[],
char *const envp[]);
execv(const char *path, char *const argv[]);
execvp(const char *file, char *const argv[]);

arg exec arg NULL


arg0arg1arg2 ..argn NULL 0

exec *argv[ ]

exec
execlp execvp

array: exect, execv, execvp


sequence: execl, execle, eseclp

path: execl, execle, exect, execv


le: execlp, exevp

37

specify: execle, exect


inherit: execl, execlp, execv, execvp
system
#include <stdlib.h>
int

system(const char *string);

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

Unix SIGHUP kill


SIGHUP
SIGFPESIGILL
SIGSYS

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 */

Unix SIGINT shell CTRL-C

#define SIGQUIT

/* quit */

SIGQUIT shell CTRL-/


4

#define SIGILL

/* illegal instr. (not reset when caught) */

SIGILL
pointer functions
5
#define SIGTRAP

/* trace trap (not reset when caught) */

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

functions pointer pointer functionsgoogle


fortran

40

SIGTRAP POSIX

#define SIGABRT

/* abort() */

SIGABRT (abort)

#define SIGFPE

/* floating point exception */

SIGFPE

#define SIGKILL

/* kill (cannot be caught or ignored) */

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

/* non-existent system call invoked */

SIGSYS

#define SIGPIPE

13

/* write on a pipe with no one to read it */

SIGPIPE

#define SIGALRM

14

/* alarm clock */

SIGALRM delivered
setitimer alarm
#define SIGTERM

15

/* software termination signal from kill */

SIGTERM
SIGTERM Unix kill

#define SIGURG

16

/* urgent condition on IO channel */

SIGURG

#define SIGSTOP

17

/* sendable stop signal not from tty */

SIGSTOP
(stop) SIGCONT
SIGCONT
#define SIGTSTP

18

/* stop signal from tty */

SIGSTP SIGSTOP SIGSTP


shell CTRL-Z deliver
SIGCONT
42

#define SIGCONT

19

/* continue a stopped process */

SIGCONT

SIGCONT
SIGSTOP SIGSTP
#define SIGCHLD

20

/* to parent on child stop or exit */

SIGCHLD Berkeley Unix SRV 4 Unix


(not a retroactive process) BSD
SIGCHID system V Unix

(exit) wait
(terminating information
7

SIGCHLD
fork

SIGCHLD wait
SIGSTOPSIGSTP
SIGCONF SIGCHLD

#define SIGTTIN

21

/* to readers pgrp upon background tty read */

SIGTTIN
SIGCONT
SIGCONT
#define SIGTTOU

22

/* like TTIN if (tp->t_local&LTOSTOP) */

SIGTTOU SIGTTIN SIGTTOU


TOSTOP tty tty
7

43

SIGTTOU
SIGCONT
#define SIGIO

23

/* input/output possible signal */

I/O SIGIO
fcntl
#define SIGXCPU

24

/* exceeded CPU time limit */

CPU CPU limitSIGXCPU


setrlimit
#define SIGXFSZ

25

/* exceeded file size limit /*

SIGXFSZ

#define SIGVTALRM 26

/* virtual time alarm */

SIGVTALRM

#define SIGPROF

27

/* profiling time alarm */

SIGPROF

#define SIGWINCH

28

/* window size changes */

xterm SIGWINCH

#define SIGUSR1

29

/* user defined signal 1 */

#define SIGUSR2

30

/* user defined signal 2 */

SIGUSR1 SIGUSR2

8
8

44

4.3

Kill
kill kill

int

kill(pid_t pid, int sig);

Kill pid

IDUID
setuid
rootUID
SIGCONT

kill 9
PID 0 sig

PID 0 sig ID

PID -1 ID
ID
9

PID kill pid

45

root proc
p ag P SYSTEM
could not be sentsig kill

sig 0kill

kill 0 -1kill errno

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

signal(int sig, sig_t func);

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 signal func SIG IGN


SIG DFL signal
void

(*sa_sigaction)(int, siginfo_t *, void *);

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

SIG BLOCK: set

SIG UNBLOCK: set


SIG SETMASK: set oset
null set nullhow

set null oset null sigprocmask

routine
int

sigemptyset(sigset_t *set)

set
int

sigfillset(sigset_t *set)

set
int

sigaddset(sigset_t *set, int signo)

signo set
int

sigdelset(sigset_t *set, int signo)


49

signo set
int

sigismember(const sigset_t *set, int signo)

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;

calling circumstances ss ags


ss ags SS DISABLE
ss sp ss size

non-null oss signaltstackss ags

SS DISABLE:
SS ONSTACK:
signaltstack oss null 0
-1errno
4.2BSD

BSD
siginterrupt :
int

siginterrupt(int sig, int flag);

sig ag 1 ag
0

4.7

sigsuspend sigmask
sigsuspend
sigsuspend
-1errno EINTR
int

sigsuspend(const sigset_t *sigmask);

sigwait set
sig
sigwait

51

int

sigwait(const sigset_t *set, int *sig);

SIGHUP SIGHUP

system call will be restarted SIGHUP

setjmp longjmp nonlocal branching setjmp evn


jmp_buff
int
void
int
void
int
void
void

env;

sigsetjmp(sigjmp_buf env, int savemask);


siglongjmp(sigjmp_buf env, int val);
setjmp(jmp_buf env);
longjmp(jmp_buf env, int val);
_setjmp(jmp_buf env);
_longjmp(jmp_buf env, int val);
longjmperror(void);

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

alarm(unsigned int seconds);

alarm alarm clock

SIGALRM alarm alarm


sleep

100,000,000
int getitimer(int which, struct itimerval *value);
getitimer which itimerval

int

setitimer(int which, const struct itimerval *value,


struct itimerval *ovalue);

setitmer alarm BSD

#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

BSD Unix BSD


/dev

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

open(const char *path, int flags, /*

mode */ );,

Open

BSD ledesc /usr/include/sys/ledesc.h

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

bash$ sysctl kern.maxfilesperproc


kern.maxfilesperproc: 3722

getdtablesize

fcntl
int

fcntl(int fd, int cmd, ...);

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 GETFD close-on-exec FD CLOSEXEC


0 1 0close-on-exec
(so the le descriptor will remain open across exec
calls.) 1close-on-exec exec

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

F GETFL fcntl O ACCMODE


(#define O_ACCMODE 0x0003) F SETFL

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

fnctl F GETLK F SETL


#define

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

F GETLK ock fnctl

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

/* shared file lock */

LOCK SH fcntl

#define

LOCK_EX

0x02

/* exclusive file lock */

LOCK EX

#define

LOCK_NB

0x04

/* dont block when locking */

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

dup STDIN FILENO 0 dup


STDIN FILENO dup
-1 errno
DUP2
int dup2(int old, int new);
new dup2 dup

DUP2
DUP2 -1 errno

5.5

System V IPC
BSD IPC

STDIN FILENO CAT cat

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

fo Unix Domain Socket

FIFO

FIFO FIFO
66

O NONBLOCK
FIFO
FIFO

FIFO
FIFO FIFO
/usr/local/myapp/fo dir
FIFO
ftok

ftok(3), msgget(3), msgctl(3),


msgrcv(3) msgsnd(3)

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(int nfds, fd_set *readfds, fd_set *writefds,


fd_set *exceptfds, struct timeval *timeout);

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

I/O BSD System V


I/O STREAMS socket STREAMS
STREAMS
BSD System V
System V
System V STREAMS
poll select
int

poll(struct pollfd *fds, unsigned int nfds, int timeout);

BSD select poll System V Unix BSD


BSD poll
select poll

select poll select


70

POLLERRPOLLHUP POLLNVAL
select poll
poll pollfd
struct pollfd {
int
fd;
short
events;
short
revents;
};

/* which file descriptor to poll */


/* events we are interested in */
/* events found on return */

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

POLLHUP STREAMS POLLHUP POLLOUT STREAMS


#define POLLNVAL

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)

EVFILT READ kevent ident


select poll

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

EVFILT AIO I/O aio error


#define EVFILT_VNODE

(-4)

EVFILT VNODE ident


ags
ags
#define NOTE_DELETE

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

NOTE REVOKE ag revoke man(2) revoke


#define EVFILT_PROC

(-5)

/* attached to struct proc */

EVLILT PROC
PID ident ags
ags OR
#define NOTE_EXIT

0x80000000

NOTE EXIT ag
#define NOTE_FORK

0x40000000

NOTE FORK ag fork


#define NOTE_EXEC

0x20000000

NOTE EXEC ag exec


#define NOTE_TRACK

0x00000001

NOTE TRACK ag kqueue fork


ags NOTE CHILD PID data
#define NOTE_TRACKERR

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)

EVFILT TIMER kqueue


EV ONESHOT ident
data data
EV CLEAR
kqueue
kqueue OR
#define EV_ADD

0x0001

EV ADD kqueue kqueue

EV DISABLE
#define EV_DELETE

0x0002

EV DELETE kqueue
#define EV_ENABLE

0x0004

EV ENABLE kqueue
#define EV_DISABLE

0x0008

EV DISABLE kqueue kqueue

79

kqueue
kqueue
OR
#define EV_ONESHOT

0x0010

EV ONESHOT kqueue
#define EV_CLEAR

0x0020

EV CLEAR kqueue kqueue

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(int resource, struct rlimit *rlp);


setrlimit(int resource, const struct rlimit *rlp);

getrlimit setrlimit :
struct rlimit {
rlim_t rlim_cur;
rlim_t rlim_max;
};
1

FinalBSD

rlim cur rlim max

getrlimit setrlimit
/usr/include/sys/resource.h

#define

RLIMIT_CPU

/* cpu time in milliseconds */

RLIMIT CPU CPU

SIGXCPU
#define

RLIMIT_FSIZE

/* maximum file size */

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

/* core file size */

RLIMIT CORE core


0 core

#define

RLIMIT_RSS

/* resident set size */


82

RMIMIT RSS (resident set size).

#define

RLIMIT_MEMLOCK 6

/* locked-in-memory address space */

RLIMIT MEMLOCK mlock

#define

RLIMIT_NPROC

/* number of processes */

RLIMIT NPROC
ID
#define

RLIMIT_NOFILE

/* number of open files */

RLIMIT NOFILE
#define

RLIMIT_SBSIZE

/* maximum size of all socket buffers */

RLIMIT SBSIZE mbufs socket


man mbufs
#define

RLIMIT_VMEM 10

/* virtual process size (inclusive of mmap) */

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

getrusage(int who, struct rusage *rusage);

getrusage RUSAGE_SELF RUSAGE_CHILDREN


RUSAGE_SELF rusage
RUSAGE_CHILDREN rusage

rusage /usr/include/sys/resource.h

struct

rusage {
84

struct timeval ru_utime;


struct timeval ru_stime;
long ru_maxrss;
long ru_ixrss;
long ru_idrss;
long ru_isrss;
long ru_minflt;
long ru_majflt;
long ru_nswap;
long ru_inblock;
long ru_oublock;
long ru_msgsnd;
long ru_msgrcv;
long ru_nsignals;
long ru_nvcsw;
long ru_nivcsw;

/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*

user time used */


system time used */
max resident set size */
integral shared memory size */
integral unshared data */
integral unshared stack */
page reclaims */
page faults */
swaps */
block input operations */
block output operations */
messages sent */
messages received */
signals received */
voluntary context switches */
involuntary " */

};

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

2003 1 FreeBSD-5.x FreeBSD


3 FreeBSD
FreeBSD

8.2

FreeBSD 5.x
/boot FreeBSD

/ /boot()

8.3

Devfs

FreeBSD 5.x devfs/dev


1000
IDE
SCSI SCSI devfs
/dev
FreeBSD 5.x /dev
devfs
devfs proc

Devfs devfs
chroot jail /dev
devfs
1

FinalBSD

devfs cd
/dev

8.4

a.out

FreeBSD 5.x a.out


a.out a.out
ELF ELF

gcc-3.2

8.5

FreeBSD 5.x gcc-3.2 gcc3.x ISO C++ ABI


gcc-3.x ex
yacc

8.6

SMPng

FreeBSD 5.x SMP


SMPng( SMP) SMP

8.7

(KSE)KSE
Scheduler Activations KSE FreeBSD
POSIX

KSE
KSE libpthreads libpthreads
libpthreads
89

makele -pthread -lpthread (relink).

8.8

FreeBSD
SMP FreeBSD
Sparc64 ia64
BSD

90

You might also like