CS Unix (Sun) CS 361 Concurrent programming • Reference system for programming assignments – Drexel University submitted assignments must work on Solaris. Fall 2004 • Use a makefile to provide directions for compiling and executing programs on tests. Lecture 2 • Get account from https://accounts.cs.drexel.edu/MakeStudentAuthA ©Bruce Char and Vera Zaychik. All rights reserved by the author. ccount or Gaylord Holder Permission is given to students enrolled in CS361 Fall 2004 to ([email protected]). reproduce these notes for their own use.

page 1 page 2

Assigned work for the first week Schedule for the next week

• Prepare for quiz (Tuesday, April 13): • Our class assistants are Ashley Steigerwalt and be able to write class definitions, simple Lecent Teng. We will be setting up office hours Java programs. Be able to answer basic for them shortly. Look for an announcement on the webct webcite. questions about Java programming. • Assignment 1 due Thursday April 8, 11:45pm via webCT. Write a Java program, compile it. Create tests and a test driver for it. Make a makefile and README file for the directory. Tar/gzip it and submit the file to webct.

page 3 page 4

Introduction to Java Java is object oriented

• This introduction assumes you’ve had • Everything belongs to a class (no global considerable experience programming in variables, directly). another object oriented language, C++ • Main program is a static method in the class (from CS171, 172, etc.). that you run with the java command.

page 5 page 6

1 Java is syntactically similar to Locations of Java code C++ • Built-in types: int, float, double, char. • Writing the code. Suppose we have a program • Control flow: if-then-else, for, switch. that consists of just one class foo and a main procedure that uses it. Write the main procedure as • Comments: /* .. */ a static method in that class. – but also // …. (to end of line) • The source code must be in a file named foo.java • Exceptions: throw …; try {} catch{}. in your current . • Compilation. Create the file foo.class via the command foo.java page 7 page 8

Running java code (default) Multi-file definitions

• To execute the main procedure in foo.class, • If you use several classes foo1, foo2, foo3, … give the command create several files in the same directory foo1.java, foo2.java, foo3.java. Compile each one. The java foo compiler will automatically look for other classes in the same directory. • Sometimes the compiler can figure out that foo1 requires foo2, so that compiling foo1.java will automatically cause foo2.java to be compiled… but explicit compilation means you can be sure that a file has been compiled or recompiled. page 9 page 10

Classes assembled at More multi-file definitions compilation, and at run time • import bar; • Contrast with C or C++ which tends to means that bar.class should be consulted collect all classes needed into a single to find definitions of classes/definitions executable (binary) file, before execution. mentioned in the current file’s programming, such as the class bar.

page 11 page 12

2 Classpath command line option Classpath specified on the for javac or java specifies a list of command line directories • Java will look in other directories, and Java archive files (. files) besides the standard • Separated by semi-colons on Windows Java system directories and your current javac -classpath .;E:\MyPrograms\MathBeans.jar;E:\utilities; foo1.java working directory (Windows) • Separated by colons on Solaris

java -classpath .:/homeb/bchar/MathBeans.jar:/homeb/bchar/utilities: foo1 (Unix)

page 13 page 14

Experienced Java programmers CLASSPATH environment use classpath command option variable rather than • Value is used if there is no classpath command option. • Software engineering considerations: • setenv CLASSPATH .:/pse/:/pse1/corejini: (on – explicit invocation of the classpath on the command Unix) line means it’s easier for programmers to remember or javac example1.java readers to discover what you set the classpath to. With fewer mistakes or misunderstandings, one has easier to maintain code Compilation will look in current working directory – Can put compilation directions into a make file, will /pse and /pse1/corejini directories for other class work for anyone regardless of how they set their definitions. classpath variables. This also avoids the problem of • set CLASSPATH .;E:\PSE\; E:\jini1_0\lib\jini- typing in a long classpath repeatedly during core.jar; (on Windows) development: just type “make asmt1” or whatever. page 15 page 16

Many similarities to C++ in built- Array example in data types, syntax import java.io.*;

• if, for, while, switch as in C++ /* Example of use of integer arrays*/

• Strings a first-class data objects. “+” works public class example2{ public static void main(String argv[]) { with Strings, also coerces numbers into int sum = 0; int durations [] = {65, 87, 72, 75}; strings. for (int counter = 0; counter < durations.length; ++counter) { • Arrays are containers. Items can be sum += durations[counter]; }; accessed by subscript. There is also a // Print out overall result. System.out.print("The average of the " + durations.length); length member. System.out.println(" durations is " + sum/durations.length + "."); } } page 17 page 18

3 Static methods, static data members Output • class foo { … e:\My Documents\mcs361\Sp02>javac example2.java e:\My Documents\mcs361\Sp02>java example2 static public int rand(int r) { …}; The average of the 4 durations is 74. // static method static public int i; // static data }

• all objects of that type share a common i and a common version of the method. • Methods can be invoked without creating an instance of the class. page 19 page 20

Example of non-static use of classes: In example3.java In Symphony.java

import java.io.*; /* Definition of symphony class */ /* Simple test program for Java (JDK 1.2) */ public class Symphony { public class example3{ // by default classes are protected (known to “package”) public int music, playing, conducting; public static void main(String argv[]) { public int rating() { Movie m = new Movie(); return music + playing + conducting; m.script = 9; m.acting = 9; m.directing = 6; } Symphony s = new Symphony(); } s. music = 7; s.playing = 8; s.conducting = 5; // Print out overall results. System.out.println("The rating of the movie is " + m.rating()); System.out.println("The rating of the symphony is " + s.rating()); } }

page 21 page 22

In Movie.java Compiling and running on Unix

% javac Movie.java /* Definition of Movie class for example 3.*/

public class Movie { % javac Symphony.java public int script, acting, directing; public int rating() { % javac example3.java return script + acting + directing; } } % java example3 The rating of the movie is 24 The rating of the symphony is 20

page 23 page 24

4 Protection in methods and data Constructors, protection members

• Can be public, protected, private as in C++. /* Definition of Movie2 class: two constructors.*/

public class Movie2{ private int script, acting, directing; public Movie2() { script = 5; acting = 5; directing = 5; } public Movie2(int s, int a, int d) { script =s; acting = a; directing = d; } public int rating() { return script + acting + directing; } // mutator/access methods for script public void setScript(int s) { script = s; } public int getScript() {return script;}; // mutator/access methods for acting, directing here } }

page 25 page 26

Typically, physical memory is A picture of process memory organized as follows allocation (address space) • interrupt vectors • memory-mapped device registers •OS kernel “text” • system software, utilities, daemons, user instructions global heap stack (static (program) variables storage storage programs data)

interrupt system software, utilities, daemons address 0 high end of devices OS kernel vectors user programs memory (address 2^32 address 0 highest physical or whatever) page 27 location of memory. page 28

Multiprogrammed computers: Process table: operating system several processes sharing the accounting same physical memory. • A table entry for each process on the system • The operating system needs to provide a • Contains: map between a process’ logical address, and – state of process (running, waiting, blocked…) an address in the computer’s physical – contents of CPU registers the last time the memory. process was executing, including PC – the process memory allocation – the process identifier, the owner of the process, the owner’s group, a list of open files, and other accounting information.

page 29 page 30

5 Threads versus processes Threads

• Originally, only one program could live in a • Share program code, global/static variables. process’ address space at a time – • Each thread does need a bit of individual processing follows a single thread of memory for itself: execution through the instructions in that –own PC address space. – own CPU register values • Modern systems allow multiple threads of – own run-time stack for procedure calls, local execution in a process, allowing the process variables to perform more than one task at a time. page 31 page 32

Benefits of multithreaded Thread support programming • Responsiveness: in a multithreaded application, • Threads can be created and managed either at the part of the program can continue running even if user level (non-supervisor mode) or within the another portion is temporarioly blocked or operating system at the kernel level (supervisor occupied doing another operation. mode). • Easier to share memory between threads in the • User level threads are usually faster to start and same process than across processes. manage. But kernel threads can be scheduled by • Cheaper to create a new thread in a process than a the operating system (which sometimes leads to new process. better scheduling), and on multiprocessor systems • Multiple threads can be run in a multiprocessor can be scheduled to run simultaneously on computer (such as queen) simultaneously, like multiple processors. multiple processes. page 33 page 34

6