服务器程序一般功能可概括为:初始化资源;等待客户端的连接;创建一个进程处理客户端请求。Linux(Android NDK)中创建多进程序程序是非常简单的。但是稍微不注意,就会出现僵死进程,造成的主要危害有:系统的进程号是有限的,僵死进程始终占用进程号,有进程号耗尽之忧;僵死进程仍然消耗销量的系统资源,造成资源浪费;如果是服务器程序,长时间运行后,产生了成千上万的僵死进程,在客户和主管面前情何以堪呢。所以僵死进程一定要避免。
先看现象吧,打开Linux命令行终端,输入ps,看看僵死进程的样子吧。IPTV_Server内部创建了3个进程,多次运行后产生了5个僵死进程(父进程号为328;僵死标志位Z)。
root 320 319 11236 3840 ffffffff 4015576c S /system/bin/iptv_server root 328 320 11136 1784 c06a0f0c 40155a70 S /system/bin/iptv_server root 901 328 11164 2044 c063af54 40154d50 S /system/bin/iptv_server root 902 901 15948 2968 ffffffff 40154d50 S /system/bin/iptv_server root 399 328 0 0 c056360a 00000000 Z iptv_server root 405 328 0 0 c085982e 00000000 Z iptv_server root 423 328 0 0 c0322437 00000000 Z iptv_server root 455 328 0 0 c0368148 00000000 Z iptv_server root 652 328 0 0 c04976e2 00000000 Z iptv_server
僵死进程产生的原因。下面程序在一个循环中每隔10s创建一个子进程。长时间运行后会发现产生了非常多的僵死进程。假设子进程执行时间很短,过程中完美的释放了所有资源,这样的子线程也会成为僵死进程,如下面的程序。一个子进程调用exit退出,资源虽然被系统回收了,但是其进程的数据结构仍保存在系统进程表中。进程的数据结构仍然存在,是Linux希望提供一种机制能让其他进程有机会收集该进程的信息。僵死进程几乎不占内存空间,没有可执行代码,也不能被调度,仅仅在进程列表中保留PID。子进程一般需要父进程负责收尸,如果他的父进程没安装SIGCHLD信号处理函数调用wait或waitpid()等待子进程结束,又没有显式忽略该信号,那么它就一直保持僵死状态。父进程结束了,僵死的子进程会过继为1号进程init,init负责清理工作。
int main(){ while(1){ int pid = fork(); if(pid==0){ childprocess_doSomething(); exit(0); }else if(pid>0){ parentprocess_doSomething(); usleep(1000*1000*10) } } }避免僵死进程的方法:
1、父进程通过wait和waitpid等函数等待子进程结束,父进程会被挂起。
pid = fork(); //生成子进程 if (pid > 0) //父进程 { waitpid(pid, &nStatus, 0); //等待子进程结束,否则子进程会成为僵死进程,一直存在,即便子进程已结束执行 }else if (0 == pid) //子进程 { dosomething(); exit(0); //子进程结束后,父进程会收到SIGCHLD消息 }
2、如果父进程很忙,需要为SIGCHLD安装信号处理函数。父进程收到子进程结束信号后,由信号处理函数回调wait回收。
struct sigaction sa; sa.sa_handler = SIG_IGN; sa.sa_flags = SA_NOCLDWAIT; sigemptyset(&sa.sa_mask); sigaction(SIGCHLD, &sa, NULL);
3、如果父进程用signal(SIGCHLD, SIG_IGN)通知内核,告知该子进程自动回收
http Content-Type一览表,方便查找
http://www.utoronto.ca/webdocs/HTMLdocs/Book/Book-3ed/appb/mimetype.html
Description of Data Content
Typical Filename Extensions
MIME type/subtype
Text and Text-Related Types
HTML text data (RFC 1866)
html htm
text/html
Plain text: documents; program listings
txt c c++ pl cc h
text/plain
Richtext (obsolete - replaced by text/enriched)
text/richtext
Structure enhanced text
(etx?)
text/x-setext
Enriched text markup (RFC 1896)
text/enriched
Tab-separated values (tabular)
(tsv?)
text/tab-separated-values
SGML documents (RFC 1874)
text/sgml
Speech synthesis data (MVP Solutions)
talk
text/x-speech
Document Stylesheet Types
Cascading Stylesheets
css
text/css
DSSSL-online stylesheets
application/dsssl (proposed)
Image Types
GIF
gif
image/gif
X-Windows bitmap (b/w)
xbm
image/x-xbitmap
X-Windows pixelmap (8-bit color)
xpm
image/x-xpixmap
Portable Network Graphics
png
image/x-png
Image Exchange Format (RFC 1314)
ief
image/ief
JPEG
jpeg jpg jpe
image/jpeg
TIFF
tiff tif
image/tiff
RGB
rgb
image/rgb
image/x-rgb
Group III Fax (RFC 1494)
g3f
image/g3fax
X Windowdump format
xwd
image/x-xwindowdump
Macintosh PICT format
pict
image/x-pict
PPM (UNIX PPM package)
ppm
image/x-portable-pixmap
PGM (UNIX PPM package)
pgm
image/x-portable-graymap
PBM (UNIX PPM package)
pbm
image/x-portable-bitmap
PNM (UNIX PPM package)
pnm
image/x-portable-anymap
Microsoft Windows bitmap
bmp
image/x-ms-bmp
CMU raster
ras
image/x-cmu-raster
Kodak Photo-CD
pcd
image/x-photo-cd
Computer Graphics Metafile
cgm
image/cgm
North Am. Presentation Layer Protocol
image/naplps
CALS Type 1 or 2
mil cal
image/x-cals
Fractal Image Format (Iterated Systems)
fif
image/fif
QuickSilver active image (Micrografx)
dsf
image/x-mgx-dsf
CMX vector image (Corel)
cmx
image/x-cmx
Wavelet-compressed (Summus)
wi
image/wavelet
AutoCad Drawing (SoftSource)
dwg
image/vnd.dwg
image/x-dwg
AutoCad DXF file (SoftSource)
dxf
image/vnd.dxf
image/x-dxf
Simple Vector Format (SoftSource)
svf
image/vnd.svf
also vector/x-svf
Audio/Voice/Music Related Types
"basic"audio - 8-bit u-law PCM
au snd
audio/basic
Macintosh audio format (AIpple)
aif aiff aifc
audio/x-aiff
Microsoft audio
wav
audio/x-wav
MPEG audio
mpa abs mpega
audio/x-mpeg
MPEG-2 audio
mp2a mpa2
audio/x-mpeg-2
compressed speech (Echo Speech Corp.)
es
audio/echospeech
Toolvox speech audio (Voxware)
vox
audio/voxware
RapidTransit compressed audio (Fast Man)
lcc
application/fastman
Realaudio (Progressive Networks)
ra ram
application/x-pn-realaudio
NIFF music notation data format
application/vnd.music-niff
MIDI music data
mmid
x-music/x-midi
Koan music data (SSeyo)
skp
application/vnd.koan
application/x-koan
Speech synthesis data (MVP Solutions)
talk
text/x-speech
Video Types
MPEG video
mpeg mpg mpe
video/mpeg
MPEG-2 video
mpv2 mp2v
video/mpeg-2
Macintosh Quicktime
qt mov
video/quicktime
Microsoft video
avi
video/x-msvideo
SGI Movie format
movie
video/x-sgi-movie
VDOlive streaming video (VDOnet)
vdo
video/vdo
Vivo streaming video (Vivo software)
viv
video/vnd.vivo
video/vivo
Special HTTP/Web Application Types
Proxy autoconfiguration (Netscape browsers)
pac
application/x-ns-proxy-autoconfig
See Chapter 6
application/x-www-form-urlencoded
See Chapter 9
application/x-www-local-exec
See Chapter 9 (Netscape extension)
multipart/x-mixed-replace
See Chapter 9 and Appendix B
multipart/form-data
Netscape Cooltalk chat data (Netscape)
ice
x-conference/x-cooltalk
Interactive chat (Ichat)
application/x-chat
Application Types
不得不说,用哈希操作来存对象,有点自讨苦吃!
不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?!
或许,是我的理解不对,没有真正的理解哈希表。
相关链接:
Redis实战
Redis实战之Redis + Jedis
Redis实战之征服 Redis + Jedis + Spring (一)
Redis实战之征服 Redis + Jedis + Spring (二)
Redis实战之征服 Redis + Jedis + Spring (三)
一、预期接上一篇,扩充User属性:
public class User implements Serializable { private static final long serialVersionUID = -1267719235225203410L; private String uid; private String address; private String mobile; private String postCode; }
我期望的是:
1) "\xe4\xb8\x8a\xe6\xb5\xb7"
2) "13800138000"
3) "100859"
几乎就是一个对象了!
但是,接下来的代码实现,让我彻底崩溃了!
二、代码实现 1.保存——HMSET