Linux Tips #5



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

If you like Tip of the Day please share it with friends and 
co-workers, and encourage them to sign up! It's free. You can
unsubscribe or change your e-mail address at any time.

To subscribe to or unsubscribe from this newsletter:
* Use your browser to visit our Newsletter Subscription Center:
  http://www.winmag.com/subscribe/
* Scroll down to the Tip of the Day section.
* Click "Choose an option," and select Subscribe or Unsubscribe.
* Scroll to the top of the page, and type your e-mail address 
  in the "E-mail" field.
* Click the Submit button below your e-mail address.

Copyright 2000 CMP Media Inc. A service of Winmag.com.
http://www.winmag.com/

Distributed by MessageMedia Inc. - http://www.messagemedia.com

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


*1. THE DIFFERENCES BETWEEN KERNEL MODE AND USER MODE                 
        
Linux has two execution modes: user mode and kernel mode.
 Ordinary programs run in user mode, which means that they are
 protected from damaging other processes or the rest of the
 system. For example, a program that runs in user mode is not
 allowed to access memory outside its address space. Conversely,
 device drivers and file systems run in kernel mode. Code
 running in kernel mode has unrestricted access to the system's
 resources. Applications, which run in user mode, often need to
 perform operations that run in kernel mode such as accessing a
 file or allocating memory. For this purpose, system calls
 are used.


*2. UNDERSTANDING SYSTEM CALLS                 
        
A system call, or syscall, is the mechanism by which an
 application that is running in user mode requests a protected
 code that executes in kernel mode. For example, an application
 that allocates memory invokes a memory allocation function,
 which, ultimately, executes a syscall. Syscalls look very
 similar to ordinary C function calls. However, they differ from
 C functions in several respects. First, all the arguments passed
 to and from a syscall must have the same size (in general, this
 is the size of a machine word). Smaller units, such as short and
 char, are automatically promoted to a larger unit by C before
 they are passed. Second, the return type of a syscall can only
 be a signed word. In particular, structs may not be passed to or
 returned from a syscall. However, you can pass a reference
 to a struct. 


*3. PROCESS ATTRIBUTES                 
        
The two basic attributes of a process are its process id, or pid,
 and the pid of its parent process. A pid is a positive integer
 that serves as the unique identifier of a running process. When
 a process creates a new process (such a process is called a
 child), the original process is the parent process. When a child
 process dies, the parent process is notified, and the kernel
 stores its exit status until the parent process requests it.


*4. WHAT ARE ZOMBIES?                 
        
When a process ends, the kernel stores its exit status in the
 process table. Processes that have ended and are kept in the
 kernel only to preserve their exit status are called "zombies."
 A zombie is removed from the system's process table when its
 status has been read from the process table. Thus, zombies can
 hang around for days and weeks. Often, zombies are a result of a
 power fault or other failures; their exit status is kept in the
 kernel so they can be analyzed afterward.


*5. ENVIRONMENT VARIABLES                 
        
An environment variable is a pair consisting of a name and its
 associated value formed as a single string: 

SYMBOL=VALUE 

The set of environment variables makes up the program's
 environment. The environment is made available to a program at
 startup as a global pointer called environ (defined in the
 header file ). Alternatively, the ANSI C function getenv() can
 be used to read the value of an environment variable. You can
 use environment variables to control and manipulate a program
 without having to modify it. For example, a program can check
 the value of an environment variable "USER" and adjust its
 behavior according to specific values.


*6. USING CHECKER TO DETECT MEMORY LEAKS                 
        
Among other onerous bugs that C and C++ experience rather often
 is memory leaks. To facilitate the task of detecting memory
 leaks, you can use Checker. Checker is a utility that adds extra
 "scaffolding code" to the user's program to detect such leaks.
 First, you compile and link the program with Checker. After
 running the program, Checker invokes a memory-leak detector that
 tells you exactly where the leaked memory was allocated. 

The Checker utility, as well as information on how to use it, is
 available at 

ftp://sunsite.unc.edu/pub/linux/devel/lang/c


*7. WHAT YOU CAN LEARN FROM CORE DUMPS                 
        
Abnormal process termination occurs when the kernel terminates
 the process because of an error or when the process receives a
 kill() message that requests process termination. Usually, a
 process that terminates abnormally dumps its core before dying.
 The core dump listing provides a complete history of the state
 of the program when it died, including the function call
 sequence, the memory address of each function called, signal
 codes, and the code line numbers. The core dump is automatically
 written into the processes working directory in a file named
 "core". Many debugging tools can analyze this file and provide
 you with valuable information about what the process was doing
 when it died.


*8. UNDERSTANDING FILE DESCRIPTORS                 
        
When a process opens a file, the kernel returns a corresponding
 file descriptor that the process refers to in order to process
 the file. A file descriptor is a low positive integer. The first
 three file descriptors of a process--0, 1, and 2--are associated
 with the standard input (stdin), the standard output (stdout),
 and the standard error (stderr), respectively. Other files have
 higher descriptor numbers. Standard C follows this convention
 too. Thus, the functions scanf() and printf() use stdin and
 stdout, respectively. You can override the default setting and
 redirect the processes I/O to different files by using different
 file descriptors.


*9. GARBAGE COLLECTOR FOR LINUX                 
        
Standard C and C++ do not have a garbage collector. If you find
 the burden of explicitly releasing every chunk of dynamically
 allocated memory by your programs too tedious and bug-prone, you
 may want to consider installing an automatic garbage collector
 utility. The Boehm garbage collector for C and C++ is
 available at 

http://reality.sgi.com/boehm_mti/gc.html 

and can be used on most operating systems, including Linux.


*10. ADVANTAGES OF WRITING PORTABLE CODE                 
        
When writing Linux applications, it is advisable to stick to the
 common denominator of Unix and Linux, so that the code can be
 ported easily to other Unix platforms. This way, you ensure that
 improvements made to the code by Unix developers can be ported
 back to Linux and vice versa.
