Wednesday, February 27, 2008

UNIX Zombie Process

Defunct processes, also known as "zombie" processes, are those that for some reason lose the handle to the parent, or the parent loses the handle to the child process, so the last step of cleanup does not occur when the process finishes.

When a process dies, it becomes a zombie process. Normally, the parent performs a wait() and cleans up the PID. But the parent can handle only one signal at a time, and sometimes the parent receives too many SIGCHLD signals at once. It is possible to resend the signal on behalf of the child via kill -18 PPID. Killing the parent or rebooting will also clean up zombies. The better resolution is to fix the faulty parent code that failed to perform the wait() properly.

Usually keeping the OS and the application up to par with respect to patches should take care of the problem; however, occasionally you may encounter zombies. It is not critical that they be cleaned up, as they are inactive and generally harmless.

The other technique is to catch the SIGCHLD signal, and have the signal
handler call waitpid() or wait3().

http://www-1.ibm.com/support/docview.wss?uid=swg21085739

http://www.unixguide.net/unix/programming/

Tuesday, February 19, 2008

Tutorials

http://www.yolinux.com/TUTORIALS/

Saturday, February 16, 2008

vmware

Understand the differences between VMware's Workstation, GSX, and ESX

VMware GSX Server


VMware Workstation supports only one CPU and up to 1 GB of RAM. GSX Server supports 2 CPUs and up to 2 GB of RAM. GSX Server is very similar to Workstation in most other ways, but one of its coolest features is the Remote Console that allows you to remotely manage and access your virtual machine from anywhere on your network. In addition, it's much easier to work with in a high availability configuration.

While VMware Workstation is mostly used by a single user to run multiple instances of operating systems for testing and support purposes, GSX Server is often used for server consolidation by running virtual machines of server operating systems that simply appear to be stand-alone servers to clients on the network.

VMware ESX Server is mainframe-class virtual machine software. This solution is typically used by mainframe data centers and cutting-edge companies. I've also seen this solution used by startup companies. With ESX Server, you can do amazing things such as more extensive server consolidation and virtual machine clustering.

How does it differ from GSX Server and VMware Workstation?
With VMware Workstation and GSX Server, the software sits on top of a host operating system such as Windows or Linux. With ESX Server, the software runs directly on the system's hardware, eliminating the need to install a base OS. In fact, ESX has its own OS. The software basically runs on its own Linux kernel, and Linux is quite beneficial to know when working with the product, although it's not an absolute necessity.

Installation of this product is quite basic. You place the CD in the tray of a system and boot from the CD. It runs you through a typical Linux installation. At the end of the install, you're instructed to go to a separate machine and type in a specific Web address to access the virtual console of ESX Server. From there, you'll configure your system and create virtual machines. With ESX Server, you can have up to 3.6 GB of RAM per virtual machine as well as high performance network cards.

-------------------------------------

Performances versus flexibility

Apart price, the very first difference between VMware Server and ESX Server every consultant or salesman would underline is performance achievement.

At today nobody ever published a benchmark comparison between the two platforms running the same virtual machine on the same hardware, but they way they are architected let people presume which products performs better.

While VMware Server needs an underlying operating system to be installed on, its bigger brother ESX doesn't: it's a bare metal solution as the industry usually calls it.

In other words part of ESX Server acts as an operating system, booting the hardware and managing it to achieve all required virtualization tasks, in the same way an appliance would do.

This difference in approach reflects a deep difference in behaviour: while Server capabilities and performances are capped by the hosting operating system, ESX Server is designed to take out the most from available hardware, with the thinnest OS layer possible.

Performing by design means ESX Server can offer customers not only better performances, but also a better consolidation ratio: the amount of concurrent running virtual machines which can be safely allocated for each CPU core in the physical host.

While VMware suggests keeping this ratio between 2 and 4 with Server, customers can reach 4 to 8 with ESX.

These values are highly dependant on expected workload on running virtual machines and on hosted applications inside: a very busy ESX Server may not allow more than three virtual machines per core while a very light Sever could easily run 10 web servers per core.

But in general they can be considered as a good reference point.


The ESX Server capability to directly control hardware components permits to improve performances also in other aspects: for example VMware developed a special file system called VMFS where to store virtual machines, which is faster and more reliable than traditional multi-purpose file systems modern operating systems offer.

Unfortunately when a virtual machine stored on VMFS needs to be migrated on another VMware product its format has to be converted.

VMware Server cannot count on VMFS and its performances depend on Windows and Linux file systems but its virtual machines can be moved on the fly on any computer with a burned DVD or a USB key, considering it just like a standard folder with a bunch of files.

But a bare metal approach brings severe limitations along with benefits: since the product itself acts as an OS, only hardware which has its drivers included will work.

So, for example, at the moment of writing VMware doesn't include drivers for local SATA disks in ESX Server, and customers adopting it have to choose between local SCSI disks or remote storage facilities, like NAS and SANs.

Customers must also be aware that not only single equipment pieces could not be usable, but a whole machine could not as well: VMware reserves itself right to officially support only a limited amount of systems on the market, so we cannot run ESX Server on any hardware available in our datacenter and receive support for it.

The hosted approach of VMware Server instead permits to rely on underlying operating system for hardware support and drivers availability.

Anything the OS can do, like connecting a remote iSCSI disk or driving a local tape backup unit, is immediately available for Server virtual machines use.

Software availability and support is impacted as well by this difference in approach.

In ESX Server there is a limited amount of installed service utility, new installed applications could not work on it because some mandatory libraries are missing, and customers are highly discouraged to add them, to not mine reliability of the whole system.

This reduces risks but can translate in administrative pains anytime VMware didn't implement a component needful for us.

In VMware Server environments any program can be installed on the host operating system, allowing customers to achieve tasks like disks defragmentation or backup, performance monitoring or remote management, with tools of their choice, maybe returning on an investment already done before.

ESX Server is much more integrated with VirtualCenter than Server, and can perform more complex operations like the famous VMotion: migration of a virtual machine from a physical host to another without interruption of service.

Another impressive capability permits to dynamically move away a running virtual machine from the physical host where it's running if it becomes overloaded, and to place it on a less busy server. All without manual intervention and without interruption of service.






Reference:
http://www.virtualization.info/2006/12/choosing-between-vmware-server-and-esx.html
http://articles.techrepublic.com.com/5100-6350_11-5109611.html

Friday, February 15, 2008

IPC: interprocess communication

A pipe is used for one-way communication of a stream of bytes.

Creating a pipe:

int pfd[2];
pipe(pfd);

These two file descriptors can be used for block I/O.

write(pfd[1], buf, size);
read(pfd[0], buf, size);

A pipe opened before the fork becomes shared between the two processes.



Reference:
http://people.cis.ksu.edu/~singh/CIS725/Fall99/programs/sock_ipc_tut.html

fork and exec Functions

fork function is the only way in Unix to create a new process:

pid_t fork(void);

fork is called once but it returns twice. It returns once in the calling process with a return value that is the process ID of the newly created process. It also returns once in the child, with a return value of 0.

All descriptors open in the parent before the call to fork are shared with the child after fork returns.

Two typical use of fork:
1) A process make a copy of itself so that one copy can handle one operation while the other copy does another task.
2) A process wants to execute another program. And then one of the copies calls exec to replace itself with the new program. This is typical for program such as shells.

The only way in which an executable program file on the disk can be executed by Unix is for an existing process to call one of the six exec functions. exec replace the current process image with the new program file, and this new program normally starts at the main function.

exec functions return to the caller only if an error occurs. Otherwise, control pass to the start of the new program, normally the main function.

SCP

If you are in a UNIX environment and need a quick and secure way to copy files I suggest using scp from the command-line. SSH and OpenSSH provide the scp application, which allows you to connect and transfer files on servers running an encrypted FTP service. (ssh daemon)

If you are looking for an X windows GUI application, you may want to try out http://gftp.seul.org/.

Scp is already installed on the UW Dante and Homer servers and if you know the right directories it will even work with Windows secure FTP servers.

Here is how scp works:(Note the use of periods; they are essential!!)

1) To copy a file from hostB to hostA, while logged in to hostB:

bash$ scp filename username@hostA.com:.
(copies file to user's home dir on hostA machine, because of the period after the colon)

2) To copy a folder from hostB to hostA, while logged in to hostB first cd to the directory on HostB which contains the folder you want to copy:

bash$ scp -r folder username@hostA.com:.(creates new folder if the folder does not exist, again in user's home directory)

3) To copy a file from hostA to hostB, while logged in to hostB:

bash$ scp username@hostA.com:filename .
(copies into current directory on hostB)

bash$ scp -r username@hostA.com:folder .
(copies whole folder into current directory on hostB)

4) To copy a file or folder within a folder from hostA to hostB, while logged in to hostB:

bash$ scp username@hostA.com:/folder/dir/filename .
(copies into current directory on hostB)

bash$ scp -r username@hostA.com:/folder/dir .
(copies whole folder into current directory on hostB)

5) Instead of using the '.' to designate files to be copied to a home directory or the current working directory you can give scp an actual path (assuming you have write permissions):

bash$ scp -r folder username@hostA:/home/httpd/

bash$ scp username@hostA.com:/folder/dir/filename /home/tmp/

Reference:

http://support.hostingplex.com/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=155

Tuesday, February 12, 2008

BISON

Bison is a general-purpose parser generator that converts a grammar description (Bison Grammar Files) for an LALR(1) context-free grammar into a C program to parse that grammar. The Bison parser is a bottom-up parser. It tries, by shifts and reductions, to reduce the entire input down to a single grouping whose symbol is the grammar's start-symbol (REDUCE THE ENTIRE INPUT DOWN TO THE START SYMBOL).

Stages in Using Bison

The actual language-design process using Bison, from grammar specification to a working compiler or interpreter, has these parts:

1. Formally specify the grammar in a form recognized by Bison (see Bison Grammar Files). For each grammatical rule in the language, describe the action that is to be taken when an instance of that rule is recognized. The action is described by a sequence of C statements.
2. Write a lexical analyzer to process input and pass tokens to the parser. The lexical analyzer may be written by hand in C (see The Lexical Analyzer Function yylex). It could also be produced using Lex, but the use of Lex is not discussed in this manual.
3. Write a controlling function that calls the Bison-produced parser.
4. Write error-reporting routines.

To turn this source code as written into a runnable program, you must follow these steps:

1. Run Bison on the grammar to produce the parser.
2. Compile the code output by Bison, as well as any other source files.
3. Link the object files to produce the finished product.

We can use single quoted characters as tokens without declaring them.

FAQ

1) context-dependent rule such as unary operator + and -

2) How to resolve the conflicts:
IF (expr) stmt (ELSE stmt)?

3)

References:

http://www.gnu.org/software/bison/

Friday, February 8, 2008

Putty终端乱码解决方案

Putty終端亂碼問題

export LANG=C

in the putty window before launching your app, it will set your locale to "C". Strings will then be displayed correctly in putty. The default locale assumes that your terminal application can handle UTF-8, which is causing the problem in putty.


問題描述﹕
當打開一個終端后﹐執行某些命令﹐如"setup" , "man一些指令"﹐"一些文字界面下的圖形工具"
的時候﹐出面亂碼的情況.


解決辦法: 設置一下環境變量 "LANG"
1. #locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_M
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
2. #export LANG=C
3. #locale
LANG=C
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_M
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=

Flex

flex for lexical analysis

http://flex.sourceforge.net/

http://www.gnu.org/software/flex/manual/

In the rules section, any indented or %{ %} enclosed text appearing before the first rule may be used to declare variables which are local to the scanning routine and (after the declarations) code which is to be executed whenever the scanning routine is entered. Other indented or %{ %} text in the rule section is still copied to the output, but its meaning is not well-defined and it may well cause compile-time errors (this feature is present for POSIX compliance. See Lex and Posix, for other such features).

Any indented text or text enclosed in `%{' and `%}' is copied verbatim to the output (with the %{ and %} symbols removed). The %{ and %} symbols must appear unindented on lines by themselves.

When specifying a lexer, Place the patterns from the more restrictive to the more general.

`\X'
if X is `a', `b', `f', `n', `r', `t', or `v', then the ANSI-C interpretation of `\x'. Otherwise, a literal `X' (used to escape operators such as `*')

Inside of a character class, all regular expression operators lose their special meaning except escape (`\') and the character class operators, `-', `]]', and, at the beginning of the class, `^'.


How do I expand backslash-escape sequences in C-style quoted strings?

A key point when scanning quoted strings is that you cannot (easily) write a single rule that will precisely match the string if you allow things like embedded escape sequences and newlines. If you try to match strings with a single rule then you'll wind up having to rescan the string anyway to find any escape sequences.

Instead you can use exclusive start conditions and a set of rules, one for matching non-escaped text, one for matching a single escape, one for matching an embedded newline, and one for recognizing the end of the string. Each of these rules is then faced with the question of where to put its intermediary results. The best solution is for the rules to append their local value of yytext to the end of a “string literal” buffer. A rule like the escape-matcher will append to the buffer the meaning of the escape sequence rather than the literal text in yytext. In this way, yytext does not need to be modified at all.