Thursday, December 29, 2005

 

context switch

When one process stops running and another replaces it, this is known as a
context switch. Generally, the overhead for this is high, and kernel
programmers and application programmers try to minimize the number of
context switches performed by the system. Processes can stop running
voluntarily because they are waiting for some event or resource, or
involuntarily if the system decides it is time to give the CPU to another
process. In the first case, the CPU may actually become idle if no other
process is waiting to run. In the second case, either the process is
replaced with another that has been waiting, or the process is given a new
timeslice, or period of time in which to run, and is allowed to continue.


 

linux hard disk performance

To squeeze the most performance out of the disk I/O subsystem, make sure
that DMA and 32-bit transfers are enabled. This can be done via the hdparm
utility, as follows (all commands are examples only):

Verify that DMA is enabled:

hdparm –d /dev/hda
If DMS is not enabled, enable it by issuing the following command:

hdparm –dl /dev/hda
Verify that 32-bit transfers are enabled:

hdparm –c /dev/hda
If 32-bit transfers are not enabled, enable them by issuing the following
command:

hdparm –cl /dev/hda

Verify the effectiveness of the options by running simple disk read tests
as follows:

hdparm –Tt /dev/hda


 

watch traffic linux

watch 'ip -s link show up | tail -6'

Use the watch command to continually loop over a given command.
Use the ip command with -s to get the statistics for TX and RX
TX is transmit
RX is recieve
Narrow the ip command down to only link interfaces that are active (up)
Pipe it through tail -6 because the first 6 lines are the loop back (lo)


Wednesday, December 28, 2005

 

MLA & APA links

MLA & APA links

http://www.cc.mie-u.ac.jp/~lq20106/eg5000/templates/

http://owl.english.purdue.edu/handouts/research/r_apa.html
http://owl.english.purdue.edu/handouts/research/r_mla.html


Friday, December 23, 2005

 

169.254.0.0/16 rfc3330

169.254.0.0/16 - This is the "link local" block. It is allocated for
communication between hosts on a single link. Hosts obtain these
addresses by auto-configuration, such as when a DHCP server may not
be found.

rfc3330

http://www.faqs.org/rfcs/rfc3330.html


Thursday, December 22, 2005

 

xi

#!/bin/sh

if [ -x /usr/bin/konsole ]; then
TERMINAL=konsole
elif [ -x /usr/X11R6/bin/xterm ]; then
TERMINAL=xterm
else
echo "Could not determin TERMINAL"
exit 0
fi

$TERMINAL -e vi $@


Monday, December 05, 2005

 

Perl: html form post

<form action="/cgi-bin/mailform" method=GET>
<input name="to" type="hidden" value="johnsonb">
<b>To:</b> <code>name@domain.com</code>
<br>
<b>Subject:</b> <input name="subject" size=40>
<br>
<b>Comments:</b>
<br>
<textarea name="comments" ROWS=8 COLS=60></textarea>
<br><input type="submit" value="Send">
</form>


 

Perl: example mailfrom script

################################################################
# construct the headers
################################################################
print(MAIL "To: recipient\n");
if ( ! $cgiVals{'subject'} ) {
$cgiVals{'subject'} = "(no subject)";
}
print(MAIL "Subject: $cgiVals{subject}\n");
if ( ! $cgiVals{'from'} ) {
$cgiVals{'from'} = "nobody";
}
print( MAIL "From: $cgiVals{from}\n");
# done with the headers. Add the blank line.
print( MAIL "\n");

################################################################
# construct the body
################################################################
foreach $key ( keys(%cgiVals) ) {
if ( $key eq "to" || $key eq "subject" || $key eq "from" ) {
next;
}
print( MAIL "$key: $cgiVals{$key}\n");
}
################################################################
# All done. Clean up.
################################################################
close(MAIL);
# the response.
print("Form submitted successfully. Thanks!");
exit(0);


 

Perl: File management

Perl has very robust mechanisms for dealing with file input/output. The
operators are similar to the forms of redirection you use on the command
line, including > < >> | etc. The syntax is

open(FILEHANDLE,FILENAME);
close(FILEHANDLE);

FILEHANDLEs aren't denoted a special character like $ @ %, and are not
strings, so that's one reason to quote all your strings in a program.
the FILENAME is a string, special formats are below.
open returns 1 on success, 0 on failure
Conventions for opening files
"FILE" open FILE for input also "<FILE"
"> FILE" open FILE for output, creating if necessary
">> FILE" open FILE for appending
"+> FILE" open FILE with read/write access
"| CMD" open a pipe to CMD
"CMD |" open a pipe from CMD
Note the CMD can be an arbitrary bourne shell command, including its own
pipes, redirection, etc.
Now that we have a filehandle, we need to read or write to it. The read
operator is < >, as in

# in a scalar context, read one line
$line = <FILEHANDLE>;
# in an array context, read the entire file (careful - this can take
# lots of memory!)
@lines = <FILEHANDLE>;

To print to a FILEHANDLE, print takes an optional first argument which is
the filehandle. How does it distinguish the first argument from the list
of scalars to print? (warning - loss of beauty ahead) print takes a list
as an argument (which as you'll remember is denoted by commas), so the
first argument does not have a comma.

print(FILEHANDLE "your","list","here");

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

Example

# get ready to send the mail
if ( ! open(MAIL,"| $sendmail $recipient") ) {
&error("Could not start mail program");
}


 

Perl: File test operators

So far, we haven't seen a lot of the power that shells incorporate. One
feature perl borrows is simple file test operators. Generally, the test
operations are used as part of an expression for a conditional. Each
operator takes a scalar value as the filename to test. Note that these are
operators and not functions, ie no paranthesis.

# procedure to check if the setuid bit is on
sub checkSUID {
return( -u $_[0] );
}

Some file test operators
-r readable
-x executable
-e exists
-d is a directory
-t is a tty
-T text file

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

Example

################################################################
# Check Security
################################################################
# Users who wish to use this program must
# put the file ".allowmailform" in their home directory on the
# machine running this script. We don't want people creating
# forms that are abused to send "anonymous" mail.
$homedir = (getpwnam($recipient))[7];
if (! -e "$homedir/.allowmailform") {
# this user does not permit the server to send mail to him. Quit.
&error("The recipient of the form has not enabled access for this form");
exit(0);
}

Note the interface to the system through getpwnam. One reason perl is so
widely used among the system administration community is that it offers
builtins to access things like password files (possibly through NIS or
similar) just as if it was C.
Note the clever subscripting of the list that getpwnam returns. getpwnam
returns all the information in the password file for a user. We take that
result in a list context and get the eighth element.


 

Perl: Subroutines

What's a language without subroutines? Perl subroutine definitions take
the following form:
sub NAME {
STATEMENTS;
}

Unlike C, subroutines are not typed. All subroutines take a list as
arguments. A subroutine can return values values in either a list or
scalar context.
Unlike Pascal, subroutine definitions cannot be nested. Actually, they can
be defined anywhere that is not in a block. This will be used below.
Unlike Fortran, there is no distinction between subroutines and functions.
I'll try to stick to using "subroutine", but the terms are
interchangeable.
Arguments to the subroutine are contained in the special array @_ (at
underscore)
Oh gee, now we need to worry about variable scoping...
For the purposes of this tutorial, let's consider all variables global.
Recursion is possible - but beyond this tutorial.
In perl, subroutines are invoked differently than the builtins
&NAME(arg1,arg2,etc);
# - or - (if the subroutine takes no arguments.
&NAME;
# return values are used just like anyother value
$loudestInstrument = &measureLoudest(@allInstruments);

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

Example
# define an error routine:
sub error {
($message) = @_;
print("<b>ERROR:<b>",
$message,
"<p>Contact the author of the previous page for assistance\n");
exit(0);
}
if ( ! $recipient ) {
# the form did not have a to field
# modify this text appropriately
&error("No Return Address");
}

Note how the definition of the subroutine doesn't affect the flow of our
program.


 

Perl: CGI example

Continuing with our script, remeber that we had an array of strings that
contained the CGI name/value pairs. Now we'll iterate through them and
create an associative array for easy manipulation later.

foreach $pair ( @cgiPairs ) {
($var,$val) = split("=",$pair);
#trust me on this magic.
$val =~ s/\+/ /g;
$val =~ s/%(..)/pack("c",hex($1))/ge;
$cgiVals{"$var"} = "$val";
}
$recipient = $cgiVals{"to"};

The magic included in the fourth line is to decode the special characters
that aren't allowed to be in a URL. For now, please take it on trust.


 

Perl: FOREACH loops

The FOREACH construct takes advantage of perl's builtin list features by
easily iterating over an entire array. The syntax is

foreach SCALAR ( LIST ) {
STATEMENTS;
}

Each time through the loop, SCALAR is assigned the next element in the
LIST, and the STATEMENTS are executed.
The loop flow can be modified using next and last, as in the DO loop.
The braces are mandatory.
Consider this version of join:

foreach $instrument ("violin","viola","cello","bass") {
print($instrument," is lighter than a \n");
}
print("harp\n");


 

Perl: FOR loops

The next control flow construct up the complexity line is the FOR loop. It
is nearly identical to the C for loop.

for ( INITIAL_EXPR ; COND_EXPR ; LOOP_EXPR ) {
STATEMENTS;
}

The loop is initialized by evaluating INITIAL_EXPR, iterates while
COND_EXPR is true, and evaluates LOOP_EXPR before beginning each
subsequent loop.

# an example countdown
for ( $i=10 ; $i>=0 ; $i-- ) {
print("$i\n");
}

The loop flow can be modified using next and last, as in the DO loop.
Unlike C, the braces around the block of STATEMETNTS are mandatory


 

Perl: WHILE/DO loops

A language must have some form of flow control to be something better than
a batch processor. The simplest form of flow control, beyond IF-ELSE is
the DO loop. The first construct is the WHILE / DO loop:

while ( EXPRESSION ) {
STATEMENTS;
}

Note that the DO is implied. If the condition is initially false, the body
of the loop will never execute. In the next form, the loop will execute at
least once.

do {
STATEMENTS;
} while (EXPRESSION);

All loops support the following two control statements:
last
Declare that this is the last statement in the loop; completely exit the
loop even if the condition is still true, ignoring all statements up to
the loop's closing brace.
next
Start a new iteration of the loop

# one way to count to 10
$number = 0;
while(1) {
$number++;
if ($number >= 10 ) {
last;
}
}


 

Perl: IF ELSE

Conditionals are put to use in IF THEN ELSE. The following are acceptable
uses of IF:

if (EXPRESSION ) {
STATEMENTS;
}

if (EXPRESSION ) {
# executed if true
STATEMENTS;
} ELSE {
# executed if false
STATEMENTS;
}

if (EXPRESSION ) {
STATEMENTS;
} elsif {
STATEMENTS;
# optional additional ELSIF's
} else {
STATEMENTS;
}

This is the first time we've seen the { } (braces) used outside the
context of associative arrays. In addition to denoting the key for an
associative array, they are used to mark of blocks of expressions. Any
number of statements can be part of the true or false portions of the IF
ELSE, and the braces are to enclose all of them. Unlike C, the braces are
not optional - requiring braces avoids the dangling else problem
Perl has no SWITCH statement - it can be imitated several ways, including
using ELSIF for each possible case.


 

Perl: Conditionals

To do comparisons on different values, perl provides two sets of
conditionals - one for numbers and one for strings.
Comparison Strings Numbers
Equality eq ==
Inequality ne !=
Greater than gt >
Greater than or equal to ge >=
Less than lt <
Less than or equal to le <=
Comparison
returns -1,0,1 cmp <=>

It's important to use the right conditional - there are no "syntax"
errors, so if you use a variable in the wrong context, there could be
unexpected results.
You can build up conditionals using && (AND) and || (OR) operations.

Truth is a very flexible value in perl:

#### Some examples of truth:
1; # traditionally
("a","b"); # the list has elements
" "; # whitespace is true
"hello"; # strings are true
"00" # a string
#### Expressions that evaluate to false
0; # traditionally
(); # the empty list
""; # the null string
"0"; # The one string of non-zero length that is false.


 

Perl: CGI Env

That's the basics of perl's builtin data types. We'll through them all
together in the continuation of our script. First, a bit about how CGI
scripts work. Each form element has a name, and when the form is
submitted, the value is associated with its name by make a url of the form

http://hostname/progname?entry1=value1&entry2=value2&textarea=blahblah

If the web server is configured to have "progname" as a CGI script, it
will run that program. CGI is nothing more than a specification of some
environmental variables that the program can use to know the "environment"
it was launched. Values such as the server version, last url referenced,
and authorization information are included in this specification.
Perl offers quick access to the environmental variables through a special
associative array, %ENV.

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

Example
This line takes the form query (the meat of what we're interested in),
separates into elements dividing on "&", and assigns the list to
@cgiPairs.

################################################################
# Process Query
################################################################
@cgiPairs = split("&",$ENV{'QUERY_STRING'});


 

Perl: Associative Arrays (Hashes)

You're wasting perl if you don't use associative arrays. Associative
arrays, sometimes called "hashes", are arrays that are indexed not on
ordered integers, but on arbitrary string values. Typically, elements of
an associative array are refered to as "key" and "value" pairs - the "key"
is used to find the element of the array that has the "value". Basic
operators are:
% (percent sign)
Refers to the entire array
{ } (braces)
Used to denote the key
$
When used with { }, this is the value of the array element indexed on the
key.

$principal{"clarinet"} = "Susan Bartlett";
$principal{"basson"} = "Andrew Vandesteeg";
$principal{"flute"} = "Heidi Lawson";
$principal{"oboe"} = "Jeanine Hassel";
@woodwinds = keys(%principal);
@woodwindPrincipals = values(%principal);

Some Associative Array functions
keys(%ARRAY) Return a list of all the keys in %ARRAY. The list is
"unordered" - it depends on the hash function used internally.
values(%ARRAY) Return a list of all the values in %ARRAY
each(%ARRAY) Each time this is called on an %ARRAY, it will return a 2
element list consisting of the next key/value pair in the array.
delete($ARRAY{KEY}) remove the pair associated with KEY from ARRAY.


 

Perl: Arrays

The lists on the previous page weren't very useful - we need some way of
saving entire lists for later use and modification.
An array is a named list. As with lists, its space is dynamically
allocated and removed, it is 0-indexed, and shares all the operators, and
some new accessors

@ (at sign)
Refers to the entire array or slice of an array (when used in conjuction
with [ ]).
$ (dollar sign)
Refers to one element of the array, used in conjunction with [ ]

@stringInstruments = ("violin","viola","cello","bass");
@brass = ("trumpet","horn","trombone","euphonium","tuba");
$biggestInstrument = $stringInstruments[3];
print("orchestral brass: ",
join(" ",@brass[0,1,2,4] ),
"\n");

Some Array functions
in addtion to list functions
push(@ARRAY,LIST) add LIST to the end of @ARRAY
pop(@ARRAY) remove and return the last element of @ARRAY
unshift(@ARRAY,LIST) add LIST to the front of @ARRAY
shift(@ARRAY) remove and return the first element of @ARRAY
scalar(@ARRAY) return the number of elements in the array


 

Perl: Lists

Perl's power is assisted by it's builtin data types and the builtins that
deal with them. Lists move us up the ladder of complexity from scalars.
A list is an ordered collection of scalars. Space for lists are
dynamically allocated and removed from the program's memory. Each element
can be addressed by its integer position in the list. Lists are 0-indexed;
the first element is called "0". Typical operators include

( ) (parenthesis)
The list constructor.
, (comma)
The comma is used to separate elements of the list.
[ ] (brackets)
Brackets are used to take slices of the list.

$biggestInstrument = ("violin","viola","cello","bass")[3];
print("orchestral brass: ",
join("
",("trumpet","horn","trombone","euphonium","tuba")[0,1,2,4]),
"\n");

Some list functions
sort(LIST) return a new list, the sorted from LIST
reverse(LIST) return a new list, the reverse of LIST
join(EXPR,LIST) return a string formed by concatenating each element of
LIST joined by EXPR
split(PATTERN,EXPR) return a list formed from each substring of EXPR
bordered by PATTERN.


 

Perl: Functions

Perl has many builtin functions, and instead of trying to describe them
all, I'll cover their syntax. Refer to other sources for full
documentation.
Perl functions are identified by their unique names (print,chop,close,
etc). For clarity's sake, the function's arguments are supplied as a comma
separated list in parenthesis. The commas are necessary, the parentheses
are often not.

print("length: " ,length("hello world"));
# prints the same thing as
print "length: ",1, length "a";

The latter example hints at perl's beastiness. For our purposes, we'll
always use parenthesis.
--------------------------------------------------------------------------------

Example
For our script, we wan't to save the date the form was filled out more
clearly than the date in the mail headers.

...
$date = `date`;
chop($date);
# begin CGI output
print("Content-type: text/html\n\n");

The first line executes the UNIX command date and puts the output in the
the variable $date. Sine the date has a newline on it, we want to chop
that off. Also, we'll initialize the output of the CGI script - necessary
for successful presentation of the results


 

Perl: Strings

There are several ways of quoting strings in perl, corresponding to the
three quote characters on the keyboard.
' (apostrophe)
The simplest quote, text placed between a pair of apostrophes is
interpreted literally - no variable expansion takes place.
To include an apostrophe in the string, you must escape it with a
backslash '\'

$instrument = 'saxophone';
$littleInstrument = 'soprano $instrument';
# the string value of $littleInstrument includes the
# text "$instrument" - probably an error in this case.

" (double quotes)
The double quote "interpolates" variables between the pair of quotes.

$instrument = "saxophone";
$littleInstrument = "soprano $instrument";
# the string value of $littleInstrument is "soprano saxophone"

To include an apostrophe in the string, you must escape it with a
backslash '\'.
` (backtick)
This quote performs as it does in the UNIX shells - the text inside the
backticks is executed as a separate process, and the standard output of
the command is returned as the value of the string.
Backticks perform variable interpolation, and to include a backtick in the
string, you must escape it with a backslash '\'.

$memberList = "/usr/people/conductor/roster";
$memberCount = `wc -l $memberList`;
# $memberCount is the number of members in the conductor's roster,
# assuming that each member is listed on a separate line.

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

Example

# the sendmail binary.
$sendmail = "/usr/lib/sendmail";

# base of your httpd installation.
$basedir = '/www';
# log file
$logfile = "$basedir/etc/logs/$progname.log";

# today
$date = `date`;


 

Perl: Scalars

Scalars are accessed by prefixing an identifier with '$'.
scalar:
A single value, either numeric or a character string.
identifier:
A "variable name." It is composed of upper or lower case letters, numbers,
and the underscore '_'. Identifiers are case sensitive (like all of perl).
Scalars are assigned to using '='

$scalar = expression;

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

Example

...
################################################################
# Configuration parameters
################################################################
# this program's name
$progname = "mailform";

This is read as "the scalar progname is assigned the string mailform".
The '$' determines that progname is a scalar.
the '=' determines that this an assignment.
The double quotes (") define the string.
All statements end with a semi-colon ';'.


 

Perl: Variables

To do anything useful, you need to be able to store values in temporary
locations and perform manipulations on them. We all know these as
"variables." Perl actually has several kinds of variables, known more
generically as "data structures." We'll use word "variable" to talk about
a specific instance of a data structure (or "data type"). Some data types
you may be familiar with:
C: int, float, char, struct
C++: class
Fortran: integer, real, character
All of the above are "strongly typed," which means you must explicitly
declare variables before you use them. Languages such as lisp or smalltalk
do not work this way - data types are determined dynamically, and if a
variable holds a number, the programmer is responsible for making sure
that the program doesn't try to pull substrings out of it. All languages
have internal representations for the different data types, they just vary
in how much the programmer assists in making sure the variable is in the
right form.
Perl falls in the middle. Which data type you use explicit in how you
access it, but you don't need to declare it before you use it.


 

Perl: Basic Syntax

Here's some information that applies to all of perl.
Perl is free form - whitespace doesn't matter. Most people like to use
liberal indentation, but you could write all on one line, if you're a
masochist.
All perl statements end in a ; (semicolon), like C.
Comments
begin with # (pound sign)
everything after the #, and up to the end of the line is ignored.
the # needn't be at the beginning of the line.
There is no way to comment out blocks of code other than putting a # at
the beginning of each line. It's a convention taken from the shell
utilities, sorry C folks.

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

Example
Now our program looks like this:

#!/usr/local/bin/perl
# mailform - a simple CGI program to email the contents of an HTML
# form to a specified user.
#


 

Starting Perl

Typically, perl is used to interpret a script. You can run a script
explicitly with perl (the % is your command prompt)

% perl scriptname

or

% cat scriptname | perl

That isn't convenient enough, since you'd rather not have to distinguish
what is a real compiled program and what is a script. Therefore, UNIX
shells have a shortcut. If a text file is executable, and the first line
is of the form

#!program [optional program arguments]

the shell executes

program [optional program arguments] scriptname

Note: While there is some leading whitespace to offset the code examples,
all examples should not use the first <tab> - especially this example of
the shell shortcut.
--------------------------------------------------------------------------------

Example
To start off our program, edit the file mailform, add the first line:

#!/usr/local/bin/perl

(or whatever the location of your perl binary is), save, exit the editor,
and make the program executable:

% chmod +x mailform


Thursday, December 01, 2005

 

Perl Lists

Programming With Perl Lists

Introduction

In order to write really useful scripts, it's important to understand how
to work with lists. Perl has two types of lists, and I will show you how
to use them in this three part series. In this article, I'll discuss the
basic list and the functions designed to help you keep track of your data.
The multidimensional stuff is really the most important, because once you
master that you can add database programming to your web site. So let's
get started. We'll begin by taking a look at basic arrays and their
functions. Please be forewarned that the code examples in this feature are
not exciting examples of code, but are designed so that you can cut and
paste them into your scripts as needed.

There is no great mystery to an array. Every programming language has
them, and some of you may have encountered them before. In Perl they are
also called lists, and it may be easier for you to think of them this way.
They are simply an ordered list of elements, usually a string or a number.
They have a definite order, meaning that item number two is always item
number two until it is explicitly changed by the programmer or the program
itself. An example of a list are the days of the week. This is a case
where the elements of the list are in a fixed order. Another example is a
list of items in your refrigerator. The items are likely to change from
day to day, so you'd expect there to be frequent updates to you list. Both
types of lists are central to many programming tasks you will encounter.

Naming a list
To name a list, precede the variable name with '@', as in @MyList. This is
different than for scalar variables, which use '$'. A list without
elements is called an empty or null list, and is declared as follows:

@NullList = ();

Initializing a list
There are several ways to initialize the list with data. The first is to
provide a series of elements in quotes, separated by commas, and
surrounded by parentheses. It may sound like a lot, but here's what it
looks like.

@names=("Sandy", "Sally", "Sarah", "Sue");

You can also do the same thing by using the the 'qw' (quote word)
construct. It accomplishes the same thing as the example above, but uses a
different syntax. Instead of parentheses, you type 'qw/', then your data
separated by spaces, and then a trailing '/'. Here's an example,
@Animals = qw/zebra yak gnu/;

You can initialize your list using scalar variables.
# Initialize some scalar variables
$One = 1;
$Two = 2;
$Three = 3;

# Initialize the list using scalar variables
@Numbers = ( $One, $Two, $Three );

You can initialize list elements one by one. However, remember that lists
in Perl are zero-based, meaning they start at 0 and not 1. To access an
individual element, surround the index with '[]'. So if you want to
initialize the first element, you'd do it like this,
@MyList[0] = "First Element";

You can also initialize individual elements with scalar variables. We'll
keep adding to the @Numbers list we started above, so that you can see how
to add to a list.
# Initialize another scalar variable to use here
$Four = 4;

# Remember, the index to the fourth element is [3],
# because the list starts at [0]
@Numbers[3] = ($Four);

In Perl, you can also initialize a range of elements. We'll add two more
elements to @Numbers so you can see how it's done.
# Initialize a few more scalars
$Five = 5;
$Six = 6;

Now, let's add them to @Numbers.
@Numbers[4, 5] = ($Five, $Six);

You can also initialize a list by using a range, specified by '(x..y)'. In
the example below, the list will contain the same numbers as @Numbers, but
we'll initialize it in a different way.
@Numbers_2=(1..6);

Finally, you can initialize a list by using another list. Don't worry,
it's not that complicated. Say you have a list,
@FirstList = ('x', 'y', 'z');

Now you want to add it to a new list.
@SecondList = ('a', 'b', 'c', @FirstList);

If you were to print @SecondList, you'd get 'a b c x y z'. Well, actually
that brings me to my next point: how to print a list.


This page is powered by Blogger. Isn't yours?