Presentation is loading. Please wait.

Presentation is loading. Please wait.

תכנות מערכות תרגול 1 נושאי התרגול: - חשבון משתמש Linux - Threads.

Similar presentations


Presentation on theme: "תכנות מערכות תרגול 1 נושאי התרגול: - חשבון משתמש Linux - Threads."— Presentation transcript:

1 תכנות מערכות תרגול 1 נושאי התרגול: - חשבון משתמש Linux - Threads

2 פתיחת חשבון (Home Work 0) - Linux 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

3 הגדרת חשבון 1.א) ניגשים לאחת המעבדות של מדעי המחשב בבניין 34. (חדרים (302 / 307 / 314 / 316 1. ב ) יש לעבוד על מערכת ההפעלה Linux. אם יש צורך, לבצע restart, ולבחור Ubuntu Linux.

4 הגדרת חשבון 2. -> Run Command -> Konsole 2. -> System -> Konsole - או -

5 הגדרת חשבון פקודות ls רשימת קבצים ls –laרשימת קבצים, כולל מוסתרים du בדיקת גודל הקבצים du -ax ~/ | sort -n du -s ~/ cd שינוי תיקיה cd folder folder דוגמה: כניסה לתיקיה cd.. (up) יציאה מתיקיה פנימית cs.bgu.ac.il -> Resources -> Linux MAN pages

6 הגדרת חשבון 3. du -ax ~/ | sort -n 4. du -s ~/ 5. ~newstd/bin/freespace 6. cd /freespace/stud/YOURUSERNAME 7. du -s

7 Homework 0 - submission Open the Firefox browser by typing firefox& on the command line. Go to your mail and send a message to spl101@cs.bgu.ac.il. 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.

8 Threads Till now, no multithreading class Prog{ public static void main(String args []){ ……. func1(); func2(); }

9 Threads Step1. Implement the interface Runnable: interface Runnabe{ public void run(); } Step 2. option 1: Create Threads option 2: Use Executor

10 Step 1 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); }

11 Step 2. Option 1. example 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(); } Output: 1 1 2 2 2 1 2 1 2 2 1 1 1 1 2 2 2 2 1 1

12 Step 2. Option 2 - ExecutorService. example 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(); }

13 Threads can be Dangerous Example: Two teaching assistants and one electronic Announcements page. Time Announcments A B C TA1 downloads page TA2 downloads pageAdds msg Y Adds msg Xupload Announcments A B C Y

14 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(); }

15 Example 1 continued class SimpleAsynchronousTask implements Runnable { Printer m_p; String m_name; SimpleAsynchronousTask(String name, Printer p) { m_p = p; m_name = name; } public void run() { for (int i = 0; i<50; i++) { m_p.printALine(i, m_name); }

16 Example 1 continued 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 }

17 Example 2: Even counter import java.util.concurrent.*; class EvenTask implements Runnable { Even m_even; 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 }

18 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); } }


Download ppt "תכנות מערכות תרגול 1 נושאי התרגול: - חשבון משתמש Linux - Threads."

Similar presentations


Ads by Google