- 浏览: 849084 次
- 性别:
- 来自: lanzhou
文章分类
最新评论
-
liu346435400:
楼主讲了实话啊,中国程序员的现状,也是只见中国程序员拼死拼活的 ...
中国的程序员为什么这么辛苦 -
qw8226718:
国内ASP.NET下功能比较完善,优化比较好的Spacebui ...
国内外开源sns源码大全 -
dotjar:
敢问兰州的大哥,Prism 现在在12.04LTS上可用么?我 ...
最佳 Ubuntu 下 WebQQ 聊天体验 -
coralsea:
兄弟,卫星通信不是这么简单的,单向接收卫星广播信号不需要太大的 ...
Google 上网 -
txin0814:
我成功安装chrome frame后 在IE地址栏前加上cf: ...
IE中使用Google Chrome Frame运行HTML 5
Recently there has been some chatter on various programming blogs about how we should be using classic Unix features to build more scalable infrastructure. This all started when Ryan Tomayko wrote I like Unicorn because it’s Unix. The gist of that post was that Eric Wong’s Unicorn, an HTTP server written in Ruby, performed extremely well despite being written in Ruby because Eric wasn’t afraid to drop down to the lower-level Unix system calls instead of using the language’s traditional higher level abstractions.
Ryan does an excellent job of explaining exactly how following age-old Unix design patterns and using those system calls is the right way to go. He provided the code for a simple TCP “echo server” that can handle clients very efficiently. Not to be outdone, other popular scripting languages saw their advocates step forward with examples of doing the same thing. In Python is Unix, Jacob Kaplan-Moss provides a Python implementation of the same echo server. In Perl is Unix, Aristotle Pagaltzis presented a Perl implementation of a pre-forking echo server as well.
What you notice in each example is that the code is surprisingly readable and simple, letting the operating system to the really heavy lifting. In fact, they’re all very similar. Most of the real differences are syntactic sugar from the particular language. And that’s the whole point of these examples. Linux and Unix have some amazing built-in facilities for solving common problems and they’ve been around for a long time. But the reality is that many of the people coding in higher level languages like Ruby, Python, or Perl may not even be aware of them. Making matters worse, as Ryan points out, is a lack of documentation. Some of the high-level languages (Ruby in particular) do a poor job and really describing the low-level calls they expose or why you might use them. So if you don’t already have more than a basic understanding of Unix systems programming to fall back on, the odds are really not in your favor.
I won’t reiterate the benefits of the networking system calls that Ryan, Jacob, and Aristotle showcase in their examples. But I would like to consider a few other Unixisms that are ofter overlooked and can make some classes of problems easier to solve.
Multi-process with fork()
Ryan touches on this a bit in his post, but I’d like to draw a bit more attention to the power of the fork() system call. When you have a lot of work to do, using fork() to make one or more “clones” of your process so you can divide and conquer works quite well–especially on multi-core CPUs. Using waidpid(), the parent process has a reliable mechanism for waiting for all the workers to exit().
One of the main benefits of fork() is that child processes will inherit almost everything from their parent. That can be especially helpful if there’s a large amount of identical information that each worker needs to have fast access to. The parent can read that data in before forking and each child will also inherit a copy. But thanks to modern copy-on-write techniques, the child process ends up sharing the exact same copy that the parent had. So if the parent reads in 512MB of data and then forks 10 children, you don’t need 5GB of RAM to support the children. Unless they start modifying the data, memory bloat will not be an issue.
The best part of all is that this all completely automatic, on by default, and something you get for free. There’s not low-level programming required on your part to get these benefits. The operating system knows how to do these things and does them quite well.
Atomic File Operations and Locking
Often times when you have multiple process all trying to fetch data from the same pool or perform the same service, you can end up end up with a hard to recreate and debug race condition. You may end up with multiple process believe they have exclusive access to a given resource, duplicating each other’s work and potentially causing a myriad of problems that could be challenging to undo.
A classic Unix solution to this problem is to use an atomic file operation. The common choice is to use one of the atomic filesystem metadata operations, such as rename(). The basic operation is like this. If you have multiple processes all trying to get exclusive access to a resource, you can use a file to mediate that. Each process will try to create /tmp/data.lock
and write its process id (PID) into it. The process that wins is then allowed to use the resource, removing the file when done. For added safety, processes should check to see if the current lock holder is alive. If not, they may treat the lock as stale and remove it.
But simply trying to create the file and write a PID into it is not atomic. You could check to see if the file exists, create it, and write to it. That’s the simple approach that sounds good on paper, but if multiple processes are trying to do that at once, you end up with a number of possible races. The traditional solution is for each process to create a uniquely named file such as /tmp/data.lock.$PID
(though that can be improved too) and then use an atomic file operation that results in creating /tmp/data.lock
. The two common choices for an atomic operation are link and rename() or link().
The rename()
call is what the Unix command mv
uses under the hood. It will change the file from its temporary name to the final name as long as that file doesn’t already exist. The link()
call will try to create a new directory entry (a hard link) that references the same file. In either case, the underlying file (the inode) does not change, only the meta data in the file system does. And those changes are atomic.
This works well as long as you’re not using NFS. That’s a whole can of worms all its own.
Others
Those are just a few examples of letting the OS doing some of your heavy lifting in sticky situations. If you’re been working in scripting languages for a while but haven’t spent much time looking at lower-level facilities like this, it might be worth making the time do do you. You might be surprised by how much you can learn and how much better your code could be. In addition to those described so far, I suggest learning more about signals and your your language of choice implements them. And if you have the time and inclination, I highly recommend a copy of Advanced Programming in the Unix Environment (second edition) by Richard Stevens. It’s truly a classic that provides rich examples of so many useful things that Unix can handle for you.
Have you found yourself simplifying code and making it more reliable by stepping back and letting the lower-level system calls do the hard work? Tell us about it in the comments.
发表评论
-
Fedora 13 Alpha 发布
2010-03-09 10:03 1057经过一周的延期过后,代号 Goddard 的 Fedora 1 ... -
超简单 无需光驱Windows下硬盘安装CentOS
2010-01-02 05:23 6242只要按照以下步骤做了,包你能把CentOS请回家:loveli ... -
Make Linux faster and lighter<4>
2009-11-05 15:54 841Speed up your software Almost ... -
Make Linux faster and lighter<3>
2009-11-05 15:54 808Boost your network You've twea ... -
Make Linux faster and lighter<2>
2009-11-05 15:53 609Optimise Gnome Along with KDE, ... -
Make Linux faster and lighter<1>
2009-11-05 15:52 1018With just a few tweaks, your Li ... -
20 Free Best Linux Wallpapers
2009-11-05 15:51 1507Hey linux guys! Who said I fo ... -
The 10 Best Linux Distributions of 2009
2009-11-05 15:49 920It was exactly one year ago tod ... -
Linux虚拟化:10个让你不得不爱的理由
2009-10-31 15:04 724对于很多云技术供应商、虚拟软件 生产商以及大型IT公司来 ... -
CentOS 5.4 发布
2009-10-22 11:09 935CentOS Linux 是一个依 GPL 规范,及利用 Re ... -
GNU项目发布Debugger 7.0
2009-10-10 09:55 836做为一款多种编程语言(如C,C++和Pascal)的调试器,G ... -
openSuse 11.2的最后beta版
2009-10-06 08:06 674openSUSE11.2发布了最后的beta版本。第一个最终候 ... -
Gemtoo Linux为10周年发布特别版的LiveDVD
2009-10-06 08:00 1074Gentoo的开发者宣布为了10周年纪念发布了特别版的Live ... -
Linux saves Aussie electrical grid
2009-10-06 07:44 800QUICK THINKING open sourcerers ... -
恢复LINUX的root密码
2009-10-04 08:43 999一. lilo 1. 在出现 lilo: 提示 ... -
Linux系统中Mysql 密码恢复
2009-10-04 08:43 952【IT168 专稿】Mysql隔一 ... -
戴尔的即时启动Linux主板:是在浪费时间?
2009-10-04 08:42 818本周早些的时候,我 ... -
Linux TCP Tuning
2009-10-01 08:16 1215There are a lot of difference ...
相关推荐
8. **程序设计哲学**:UNIX的设计哲学,如“做一件事并做好”(Do One Thing and Do It Well)和“一切皆文件”(Everything Is a File),这些理念至今仍影响着软件工程。 通过分析这些源代码,开发者不仅可以提升...
Unix是由贝尔实验室的研究人员在1960年代末开发的,其核心设计原则是“一切皆文件”(Everything is a file),这意味着所有资源,包括硬件设备,都被抽象为文件进行操作。这种设计理念简化了系统架构,使得程序设计...
Kochan, Patrick Wood Publisher : Sams Publishing Pub Date : February 27, 2003 ISBN : 0-672-32490-3 Pages : 456 Slots : 1 <br/>Unix Shell Programming is a tutorial ...
- **Everything you know is wrong**:纠正了一些关于信号处理的常见误解。 - **Some signals to make you popular**:列出了几种常见的信号及其用途,例如SIGINT、SIGTERM等。 #### 五、管道 - **“These pipes ...
As the best way to do this, I have built a Dig class which acts like the good-old-Unix-style dig. Its acts like dig, but it is not a complete dig implementation. It does, however, do everything you ...
Shell scripting is essential for Unix users and system administrators-a way to quickly harness and customize the full power of any Unix system. With shell scripts, you can combine the fundamental ...
The majority of both Unix and Linux code is still written at the system level, and Linux System Programming focuses on everything above the kernel, where applications such as Apache, bash, cp, vim, ...
In addition to covering the basic principles behind cryptographic authentication, it covers everything from basic installation to advanced topics like cross-realm authentication, defending against ...
在计算机操作系统中,"一切皆文件"(Everything is a file)是Unix哲学的一个核心概念,意味着所有的资源,包括硬件设备、系统调用、进程等,都可以通过文件系统接口进行访问和操作。这一理念使得文件系统成为了操作...
Although there is no direct Linux/UNIX support yet, JBMail runs nearly perfectly under Wine v20041201 (even with SSL/TLS connections). Stay informed about JBMail developments by subscribing to our ...
autorun.inf ISScript8.Msi SplashBitmap.bmp Container.ico Legal UNIX Data1.cab License Pack unix.zip [msilm16 tmp]$ cd UNIX/ [msilm16 UNIX]$ ls Install IRIX License_Pack Linux_IA64 Tru64 installer ...
Linux System Administration is ideal as an introduction to Linux for Unix veterans, MCSEs, and mainframe administrators, and as an advanced (and refresher) guide for existing Linux administrators who...
Covering everything from reverse engineering to SQL attacks, and including topics like social engineering, antiforensics, and common attacks against UNIX and Windows systems, this book teaches you to...
Emacs (Editor Macros) is an advanced text editor developed by the Free Software Foundation and widely used in Unix-like systems, including Linux. It is renowned for its extensibility and ...
About A natural language parser is a program ... Simple scripts are included to invoke the parser on a Unix or Windows system. For another system, you merely need to similarly configure the classpath.
Users who come from other operating systems like Unix or VMS and think it is unnecessary to do everything with a GUI interface. Users who want to automate tasks like password changing in a batch file ...