600 likes | 612 Views
System Programming i n Operating Systems. ITU Informatics Institute. Dr. Serdar Çelebi. Contents of first week. What is System Programing History of Perl Perl Basics First Perl Program Some Common Control Sequences Arithmatic Operators Control Structures I. Introduction.
E N D
System ProgramminginOperating Systems ITU Informatics Institute Dr. Serdar Çelebi
Contents of first week • What is System Programing • History of Perl • Perl Basics • First Perl Program • Some Common Control Sequences • Arithmatic Operators • Control Structures I
Introduction What is System Programming? And what System Programmer do? • ”System Programming" is a fairly loosely defined term which is generally used to refer to those programming tasks requiring direct interaction with the operating system; hardware. It is also used in reference to the implementation and maintenance of systems software e.g. the operating system itself; compilers; system utilities and communications software. • A system programmer produced standalone general purpose utilities (e.g. editors, assemblers) and/or libraries of useful routines (e.g. graphics, mathematical). Often it involved building new services into the operating systems (e.g. new device drivers). The activity often required specialised knowledge of the underlying operating system and employed services not normally used by the applications programmer. • The services and concepts that a real UNIX system programmer might employ are amenable to all programmers and are likely to be an element of an application program which needs to make efficient use of the available resources, i.e. multiple process creation, synchronisation of programs etc..
Introduction • Shell Scripting • tcl & sed • awk & gawk • Python, Perl (Practical Extraction and Reporting Language)
Tcl & sed • tcl (Tool Command Language) • Original design is centered around a macrolike language for helping with shell-based applications (string handling). It is a true interpreter and good for embedding a scripting language inside another language. But it is not a general-purpose scripting language • sed is a stream editing and filtering tool that complements awk
awk & gawk • awk and gawk are functionally subsets of Perl • awk was designed as a reporting language with the emphasis on making the process of reporting via the shell significantly easier • Perl built-in support (i.e. Network socket class), external file manipulation and management functions, many operating systems • awk UNIX, simpler, smaller, more structured, much quicker for small programs, supports advanced regular expressions
Python • Developed as an object-oriented language • General-purpose programming and good at text processing • Unlike Perl, Python does not resemble C, and it does not resemble Unix-style tools like awk. Resembles object oriented languages such as java
Perl (Practical Extraction and Reporting Language) • Perl is particularly strong at: Process, File and Text manipulation • This makes it espacially useful for: System utilities, Software tools, System management tasks, Database access, Graphical programming, Networking, and World Wide Web programming • These strengths make it particularly attractive to: CGI script authors, system administrators, mathematicians, journalists, etc.
History of Perl • Developed by Larry Wall in 1987 • Aimed to integrate features of the UNIX languages awk and sed with framework provided by shell • Gained support in open-source community • Versions of Perl • Perl 3 • Adopted GNU General Public License • Perl 4 • Adopted Artistic License to attract more users, first “Camel” book • Perl 5 • Major reorganization of Perl language
History of Perl • Perl 6 • Development announced on July 18, 2000 • A complete rewrite of internals and externals of Perl • More extensible with features such as threading, signal handling and Unicode (a standard character set that can represent the majority of the world’s spoken language) • Current release 5.8.8 (as of April 2006)
Perl Library • Perl modules • Reusable pieces of software • Defined in library files ending in .pm • Primary distribution center is the Comprehensive Perl Archive Network (CPAN). ( www.cpan.org ) • To find and install modules on Windows (created by ActiveState Tool Corporation) use the Perl Package Manager (PPM)
General Notes About Perl • Perl approach to programming • Qualities of a good programmer • Laziness • Reusable code and applicable in as many places as possible • Impatience • Do not do manually, Functional programs • Hubris • Easy to understand code, readable code • Programs can be written in many different ways. Love of freedom “There’s more than one way to do it” • Highly portable (Operating Systems)
Some Internet Links • http://www.perl.org • http://dev.perl.org • http://www.cpan.org • http://www.perl.com • http://www.activestate.com
Possible Perl application topics • Strings • Numbers • Dates and Times • Arrays • Hashes • Pattern Matching • File Access • File Contents • Directories • Subroutines • References and records • Packages • Libraries and Modules • Classes • Objects and Ties • Database Access • User Interfaces • Process management • Communication • Sockets • Internet services • CGI programming, Web Automation
Unlike Perl 5, Perl 6 will provide • explicit strong typing • proper parameter lists • active metadata on values, variables, subroutines, and types • declarative classes with strong encapsulation • full OO exception handling • support for the concurrent use of multiple versions of a module • extensive introspection facilities (including of POD) • LL and LR grammars (including a built-in grammar for Perl 6 itself) • subroutine overloading • multiple dispatch • named arguments • a built-in switch statement • hierarchical construction and destruction • distributive method dispatch • method delegation • named regexes • overlapping and exhaustive regex matches within a string
Unlike Perl 5, Perl 6 will provide • named captures • parse-tree pruning • incremental regex matching against input streams • macros (that are implemented in Perl itself) • user-definable operators (from the full Unicode set) • chained comparisons • a universally accessible aliasing mechanism • lexical exporting (via a cleaner, declarative syntax) • multimorphic equality tests • state variables • hypothetical variables • hyperoperators (i.e. vector processing) • function currying • junctions (i.e. superpositional values, subroutines, and types) • coroutines
Perl programs can be typed into a text editor program (such as emacs or vi on UNIX, or Notepad on Windows) and save the file as “program_name.pl” Writing a Perl Program
Top of the file #! Shebang construct c:\perl\bin directory on MS #!c:\perl\bin\perl.exe x (executable) file user@machine:~ > ls -al first.pl -rwxr-xr-x 1 user users 56 May 27 14:37 first.pl user@machine:~ > ./first.pl This is my first perl Program user@machine:~ > user@machine:~ >chmod u+x first.pl Extension is not important Running Perl Program #!/usr/bin/perl ...
Running Perl Program • user@machine:~ > ls -al first.pl • -r--r--r-- 1 user users 56 May 27 14:37 first.pl • user@machine:~ > perl first.pl • This is my first perl Program • user@machine:~ > • user@machine:~ > perl –e ‘print “Hello, World!\n”:’ • To compile a program and check for syntax errors, add the –c • command-line option to the perl command (as in perl –c fileName.pl) • The –e option tells Perl that there is a one-liner script coming up. The part just after the –e is the Perl code itself, inside single quotes (‘), or double quotes (“) for Windows. • perl compilation opcodes (operation codes) interpreter execution phase
1 #!/usr/bin/perl 2 # Fig. 2.1: fig02_01.pl 3 # A first program in Perl 4 5 print "Welcome to Perl!\n"; # print greeting The Perl comment character (#) indicates that everything on the current line following the # is a comment. The “shebang” construct (#!) indicates the path to the Perl interpreter on Unix and Linux systems. Function print outputs the string “Welcome to Perl!\n” to the screen. The escape sequence \n represents newline and positions the screen cursor at the beginning of the next line. ; omitting the semicolon at the end of a statement is usually a syntax error Welcome to Perl!
1 #!/usr/bin/perl 2 # Fig. 2.3: fig02_03.pl 3 # Prints a welcome statement in a variety of ways 4 5 print ( "1. Welcome to Perl!\n" ); 6 print "2. Welcome to Perl!\n" ; 7 print "3. Welcome ", "to ", "Perl!\n"; Although these statements are on separate lines, the string Welcome to Perl! is displayed on one line. 8 print "4. Welcome "; 9 print "to Perl!\n"; Arguments to built-in functions like print can be placed in parentheses. 10 print "5. Welcome to Perl!\n"; Whitespace characters are ignored by Perl. 11 print "6. Welcome\n to\n\n Perl!\n"; More than one argument may be passed to function print. 1. Welcome to Perl! 2. Welcome to Perl! 3. Welcome to Perl! 4. Welcome to Perl! 5. Welcome to Perl! 6. Welcome to Perl! Newline characters may be used to print multiple lines of text using one line of code.
1 #!/usr/bin/perl 2 # Fig. 2.4: fig02_04.pl 3 # A simple addition program 4 5 # prompt user for first number 6 print "Please enter first number:\n"; 7 $number1 = <STDIN>; 8 chomp $number1; # remove "\n" from input 9 Prompt to tell the user to enter the first number. 10 print "Please enter second number:\n"; 11 $number2 = <STDIN>; 12 chomp $number2; # remove "\n" from input 13 The expression <STDIN> causes program execution to pause while the computer waits for the user to enter data. Assignment operator =. Declare scalar variable $number1. 14 $sum = $number1 + $number2; Function chomp removes the newline character from the end of the line. 15 16 print "The sum is $sum.\n"; The assignment statement calculates the sum of the variables $number1 and $number2 and assigns the result to variable $sum. Please enter first number: 45 Please enter second number: 72 The sum is 117.
$number1 45\n 45 $number1
45 $number1 $number2 72
$number1 45 72 $number2 117 $sum
String scalar values • Single quoted ‘ ‘ • Double quoted “ “ • It interpolates variables (such as $sum) and escape sequences • (such as \n) in the string. • Dollar sign ($) reminds that the variable refers to a scalar value • In single quote, it interpolates only the escape sequences \’ and • \\, for inserting single-quote characters and backslash characters • in the string. • The following statement would output the string exactly as it • appears between the single quotes: • print ‘The sum is $sum. \nThank you.’;
1 #!/usr/bin/perl 2 # Fig. 2.14: fig02_14.pl 3 # Demonstrates the difference between pre- and postincrement 4 Print the new value of variable $c. Use the postincrement operator to use the current value of $c in the expression and then increment $c by 1. Print variable $c. 5 $c = 5; 6 $d = 5; 7 8 print $c, " "; # print 5 9 print $c++, " "; # print 5 then postincrement 10 print $c, "\n"; # print 6 11 12 print $d, " "; # print 5 13 print ++$d, " "; # preincrement then print 6 14 print $d, "\n"; # print 6 5 5 6 5 6 6 Use the preincrement operator to increment $c by 1 and then use the new value of $c in the expression.
Run-time logic errors Such an error can be tricky to find, unless you run your programs using perl with the –w command line option, as in perl –w filename This commandrequests that perl report additional warning messages. In the case of the accidental use of = = (instead of = ), perl will likely catch the error and return a message like useless use of numeric eq (= =) in void context with the line number of the expression that contains the problem. On UNIX and Linux systems #! /usr/bin/perl -w Forces the program always to execute with warnings enabled
String Operators • Perl has a collection of arithmetic, equality and relational operators for use with numeric scalars • For example:eq, lt, gt, le, ge are relational operators and compares ASCII value of each character if ( “rabbit” gt “dragon” ) • A word beginning with A (65) is less than a word beginning with a (97), • 1 (49) is less than that of 2 (50). • Perl also has special operators for “adding” and “multiplying” strings • The concatenation operator. , and the string repetition operator, x. >$name = “Serdar”; >$greeting = “Hello, “ . $name . “!”; >Hello, Serdar! >$lamp = “wish” x 3; >$lamp = “wishwishwish”;
CONTROL STRUCTURESAlgorithms • Any computing problem can be solved by executing a series of actions in a specific order. A procedure for solving a problem in terms of • the actions to be executed and, • the order in which the actions are to be executed is called an algorithm. Consider the “rise-and-shine algorithm” followed by the one junior executive for getting out of bed and going to work: (1) Get out of bed, (2) take off pajamas, (3) take a shower, (4) get dressed, (5) eat breakfast, (6) car pool to work. Suppose that the same steps are performed in a slightly different order. Specifiying the order in which statements are to be executed in a computer program is called program control.
Control Structures- if - if ( statement satify) { ... } elsif ( statement satisfy) { ... } else { ... }
1 #!/usr/bin/perl 2 # Fig. 2.17: fig02_17.pl 3 # Using if statements with relational and equality operators 4 5 print "Please enter first integer: "; 6 $number1 = <STDIN>; 7 chomp $number1; Prompt the user for two numbers, read in the numbers, and remove the newline characters with chomp. The if structure compares the values of variable $number1 and variable $number2 to test for equality. 8 9 print "Please enter second integer: "; 10 $number2 = <STDIN>; 11 chomp $number2; 12 13 print "The integers satisfy these relationships:\n"; 14 15 if ( $number1 == $number2 ) { The relational operator < tests whether $number1 is less than $number2. 16 print "$number1 is equal to $number2.\n"; 17 } The body of the if structure, enclosed by a pair of braces ({}), executes if the condition evaluates to true. 18 19 if ( $number1 != $number2 ) { 20 print "$number1 is not equal to $number2.\n"; 21 } The relational operator > tests whether $number1 is greater than $number2. 22 The equality operator != tests whether $number1 and $number2 are not equal. 23 if ( $number1 < $number2 ) { 24 print "$number1 is less than $number2.\n"; 25 } 26 27 if ( $number1 > $number2 ) { 28 print "$number1 is greater than $number2.\n"; 29 } 30
31 if ( $number1 <= $number2 ) { 32 print "$number1 is less than or equal to $number2.\n"; 33 } 34 35 if ( $number1 >= $number2 ) { 36 print "$number1 is greater than or equal to $number2.\n"; 37 } The relational operator <= tests whether $number1 is less than or equal to $number2. Please enter first integer: 3 Please enter second integer: 7 The integers satisfy these relationships: 3 is not equal to 7. 3 is less than 7. 3 is less than or equal to 7. The relational operator >= tests whether $number1 is greater than or equal to $number2. Please enter first integer: 22 Please enter second integer: 12 The integers satisfy these relationships: 22 is not equal to 12. 22 is greater than 12. 22 is greater than or equal to 12. Please enter first integer: 7 Please enter second integer: 7 The integers satisfy these relationships: 7 is equal to 7. 7 is less than or equal to 7. 7 is greater than or equal to 7.
1 #!/usr/bin/perl 2 # Fig. 2.19: fig02_19.pl 3 # Program to illustrate numeric and string context, and undef 4 5 $string = "Top 10"; 6 $number = 10.0; 7 print "Number is 10.0 and string is 'Top 10'\n\n"; The concatenation operator evaluates $number in string context, in this case “10”. The binary addition operator evaluates strings in numeric context. If nothing can be interpreted as numeric, the string evaluates to 0. 8 9 $add = $number + $string; # 10 (not 20) 10 print "Adding a number and a string: $add\n"; 11 12 $concatenate = $number . $string; # '10Top 10' 13 # (not '10.0Top 10') 14 print "Concatenating a number and a string: $concatenate\n"; 15 16 $add2 = $concatenate + $add; # 20 (not 30, not 1020) 17 print "Adding the previous two results: $add2\n\n"; When using a string in numeric context, Perl stops conversion at the first character that cannot be used in numeric context. 18 19 $undefAdd = 10 + $undefNumber; 20 print "Adding 10 to an undefined variable: $undefAdd\n"; 21 22 print "Printing an undefined variable: $undefVariable(end)\n"; When an undefined variable is found in numeric context, it evaluates to 0. When an undefined value is interpreted in string context, it evaluates to an empty string (“”). Number is 10.0 and string is 'Top 10' Adding a number and a string: 10 Concatenating a number and a string: 10Top 10 Adding the previous two results: 20 Adding 10 to an undefined variable: 10 Printing an undefined variable: (end)
$total = $total + $grade; add grade to total add 1 to counter $counter = $counter + 1; Flowcharting Perl’s sequence structure
while ( statement) { ... } Control Structureswhile do { ... }while ( statement)
until ( statement) { ... } Control Structuresuntil do { ... }until ( statement)
if selection structure true sales >= 50 print “Earned bonus!” false unless selection structure false sales >= 50 print “You did not earn your bonus.” true Flowcharting the single-selection if (top) and unless (bottom) Control Structures $total = $total + grade; $counter = $counter + 1;
false true $sales >= 50 print “You did not earn your bonus.” print “Earned bonus!” print “You did not earn your bonus.” print “Earned bonus!” Flowcharting the double-selection if/else control structure
true $product <= 1000 $product = 2 * $product $product <= 1000 $product = 2 * $product false Flowcharting the while repetition structure
1 #!/usr/bin/perl 2 # Fig. 3.5: fig03_05.pl 3 # Using the do/while repetition structure 4 The do/while repetition structure repeatedly prints the value of variable $counter while the variable is less than or equal to 10. 5 $counter = 1; 6 7 do { 8 print "$counter "; 9 } while ( ++$counter <= 10 ); 10 11 print "\n"; After the each execution of the body of the loop, the control variable $counter is pre-incremented. 1 2 3 4 5 6 7 8 9 10
1 #!/usr/bin/perl 2 # Fig. 3.6: fig03_06.pl 3 # Using the do/until repetition structure 4 5 $counter = 10; The do/until repetition structure repeatedly prints the value of variable $counter until the variable is equal to 0. 6 7 do { 8 print "$counter "; 9 } until ( --$counter == 0 ); 10 11 print "\n"; After the each execution of the body of the loop, the control variable $counter is pre-decremented. 10 9 8 7 6 5 4 3 2 1