system programming web site: spl101/mainspl101/main things to know weekly homework each practical...

22
System Programming Web site: http://www.cs.bgu.ac.il/~spl101/Main Things to know Weekly Homework Each practical session will have a small homework assignment (submit 10 of 12) Assignments (all mandatory) * There will be 4 big programming assignments. * The weight of each assignment in the final grade is 5% * Assignments must be submitted in pairs. Final Exam The weight of the final exam in the final grade is 80%. In order to pass the course, you must: * Submit at least 10 weekly homework * Get a grade >= 1 in each of the four assignments * Final exam grade >= 56. * Get an overall average grade >= 56.

Upload: audra-ford

Post on 27-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

System ProgrammingWeb site: http://www.cs.bgu.ac.il/~spl101/Main

Things to know

Weekly Homework

Each practical session will have a small homework assignment (submit 10 of 12)

Assignments (all mandatory)

* There will be 4 big programming assignments. * The weight of each assignment in the final grade is 5% * Assignments must be submitted in pairs.

Final Exam

The weight of the final exam in the final grade is 80%.

In order to pass the course, you must:

* Submit at least 10 weekly homework * Get a grade >= 1 in each of the four assignments * Final exam grade >= 56. * Get an overall average grade >= 56.

More things to know

Lab Meetings

- Some of the practical sessions will take part at labs.

- In lab session, students will run and experiment with some of the code presented in the classroom

- For Lab sessions, students need to come at their registered time and place

Workload

- The course workload is very heavy.

- We intend the workload to be heavy just for sake of it - this is part of the programming experience we intend to convey.

- Practical sessions precedes the lectures (it is by choice)

Web site: http://www.cs.bgu.ac.il/~spl101/Main

- Read announcements (check everyday)

- CS mail (use forward if not your regular mail account)

- RSS (also possible)

Help Desk

http://www.cs.bgu.ac.il/help/index.html (use F.A.Q)

Linux

- We are going to use it, so try to learn the basics

- Linux in CS labs is already configured with all you will need

- But, don't be afraid to try it at home (could be efficient in those long white nights)

Office Hours

Feel free to visit (if you have problems related to SPL) all TA's at their office hours.

Linux( - Home Work 0)פתיחת חשבון

1. cs.bgu.ac.il -> Help Desk -> Unix Account

2. Fill BGU user name and password.

3. Sign form – near lab secretary, 007/37. Put in box 98.

4. Check account. cs.bgu.ac.il -> Resources -> CS Webmail

HW0 - Leonid‘s presentation (file on the web)

הגדרת חשבון

. 34א( ניגשים לאחת המעבדות של מדעי המחשב בבניין 1.

316 / 314 / 307 / 302 ))חדרים

.Linuxב( יש לעבוד על מערכת ההפעלה

.Ubuntu Linux, ולבחור restartאם יש צורך, לבצע

הגדרת חשבון

2. -> Run Command -> Konsole

2. -> System -> Konsole - או -

הגדרת חשבוןפקודות

ls רשימת קבצים

ls –laרשימת קבצים, כולל מוסתרים

du בדיקת גודל הקבצים

du -ax ~/ | sort -n

du -s ~/

cd שינוי תיקיה

cd folder folder דוגמה: כניסה לתיקיה

cd.. (up) יציאה מתיקיה פנימית

cs.bgu.ac.il -> Resources -> Linux MAN pages

הגדרת חשבון

3. du -ax ~/ | sort -n

4. du -s ~/

5. ~newstd/bin/freespace

6. cd /freespace/stud/YOURUSERNAME

7. du -s

Homework 0 - submission

- Open the Firefox browser by typing firefox& on the command line.

- Go to your mail and send a message to [email protected].

- The message subject must be HW0 (and not hw0) and ONLY HW0.

- Your mail will have 2 lines The first one is your home directory storage size. The second is your freespace storage size.

Using Threads

- All the computer programs you've seen so far were sequential

– only one thing was performed at any given time

- Sometimes it is desirable to do several things simultaneously (when?)

Multitasking vs. Multithreading

- If the tasks to be performed are not related in any way, using a different process for each task is a good solution

- If the tasks are somewhat related, and need to share data between them, it is beneficial to put them inside the same process, so they can share resources. This is achieved using Threads

- Several Threads can run simultaneously on a single process, sharing its resources. Special care should be taken when using those shared resources

how to use Threads in Java

In order to run a task in parallel, two steps should be taken:

1. Define the task: - Defining the task is done by implementing the Runnable interface:

class SomeClass implements Runnable {

public void run() { … some code .... } }

2. Ask "someone" to run it:

- Option 1: Use Executor

- Option 2: Create Threads

Step 1 - (Define the task) example:

class SimpleRunnable implements Runnable { private int m_number; public SimpleRunnable(int i) { this.m_number = i; }

public void run() { for (int i = 0; i < 10; i++) { System.out.print (" " + m_number); } }

}

Step 2 - (Run the task) - example 1 (Use Executor):

public class Threads01e { public static void main(String[] a) { // Create an executor: ExecutorService e = Executors.newFixedThreadPool(3); // create several runnables, and execute them. for (int i=0; i < 10; i++) { SimpleRunnable r = new SimpleRunnable(i); e.execute(r); } e.shutdown(); // this causes the executor not to accept any more //tasks, and to kill all of its threads when all the //submitted tasks are done. }

}

Step 2 - (Run the task) - example 2 (Create Threads):

public class Threads01 { public static void main(String[] a) { SimpleRunnable r1 = new SimpleRunnable(1); Thread t1 = new Thread(r1);

SimpleRunnable r2 = new SimpleRunnable(2); Thread t2 = new Thread(r2);

t1.start(); t2.start();

}

}

Threads can be Dangerous

- Having things run "at the same time, in the same place", can be dangerous.

- The two tasks can interfere with each other, and unexpected results might occur.

Threads can be Dangerous

Code Example 1: One printer and two threads

class Printer { Printer() { … } /** * Print a numbered line of the string 's' 40 times. * @param i line number * @param s the string to concatenate */ public void printALine(int i, String s) { System.out.print(i + ") "); for (int j = 0; j < 40; j++) { System.out.print(s); } System.out.println(); }

}

Example 1 continuedclass SimpleAsynchronousTask implements Runnable {

Printer m_p;

String m_name;

// Create an asynchronious task with a given name and a printer object

SimpleAsynchronousTask(String name, Printer p) {

m_p = p; // printer object

m_name = name; // name task name

}

// Main lifecycle.

// use the priner object to print the name 50 times.

public void run() {

for (int i = 0; i < 50; i++) {

m_p.printALine(i, m_name);

}

}

}

public class Threads02 {

static void main(String[] a) {

Printer p = new Printer();

Thread t1 = new Thread(new SimpleAsynchronousTask("a", p) );

Thread t2 = new Thread(new SimpleAsynchronousTask("b", p) );

t1.start(); // prints some lines of aaaa

t2.start(); // prints some lines of bbbb

}

}

Example 1 continued

Possible output

0) aaaaaaaaaaaa0) bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabba

aaaaaaaaaaaaaaaaaaaaaaaaaa

1) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

2) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

3) aaaaaaaaaaaaaaaaaaaaaa1) bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

aaaaaaaaaaaaaaaaaa

4) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

5) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

6) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

7) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

8) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

9) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2) abbba

bb10) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

11) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Example 2: Even counterimport java.util.concurrent.*;

class EvenTask implements Runnable {

Even m_even;

public EvenTask(Even even) {

m_even = even;

}

public void run() {

for (int i = 0; i < 50; i++) {

System.out.println(m_even.next());

}

}

}

public class Threads03 {

public static void main(String[] args) {

ExecutorService e =

Executors.newFixedThreadPool(10);

Even ev = new Even();

for (int i = 0; i < 10; i++) {

e.execute(new EvenTask(ev));

}

e.shutdown();

}

}

class Even {

private long n = 0;

//@ pre-condition: n is even

public long next() {

n++;

try {

Thread.sleep(30);

} catch (InterruptedException e) {

}

n++;

return n;

//@ post-condition : n is greater by two

}

}

Solution (Trial)

class EvenTask implements Runnable {

Even m_even;

EvenTask(Even even) {

m_even = even;

}

public void run() {

try {

for (int i = 0; i < 50; i++) {

System.out.println(m_even.next());

}

} catch (NotEvenException e) {

System.out.println("Exception in “ +

Thread.currentThread().getName());

e.printStackTrace(System.out);

}

}

}

class Even {

private long n = 0;

//@ pre-condition: n is even

public long next() throws NotEvenException {

if (n%2 != 0) {

throw new NotEvenException("PRE: n is not even!");

}

n++;

try {

Thread.sleep(30);

} catch (InterruptedException e) {

}

n++;

if (n%2 != 0) {

throw new NotEvenException("POST: n is not even!");

}

return n;

}

//@ post-condition : n is greater in two

}class NotEvenException extends Exception {

public NotEvenException (String message){

super(message);

}

}