1 / 22

System Programming

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.

kblaze
Download Presentation

System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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)

  3. 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)

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

  5. HW0 - Leonid‘s presentation (file on the web) פתיחת חשבון (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

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

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

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

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

  10. Homework 0 - submission - Open the Firefox browser by typing firefox& on thecommand line. - Go to your mail and send a message to spl0101@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.

  11. 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

  12. 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

  13. 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); } } }

  14. 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. } }

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

  16. 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.

  17. Threads can be Dangerous 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(); } } Code Example 1: One printer and two threads

  18. Example 1 continued class 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); } } }

  19. 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(newSimpleAsynchronousTask("b", p) ); t1.start(); // prints some lines of aaaa t2.start(); // prints some lines of bbbb } }

  20. 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

  21. Example 2: Even counter 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 } } import 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(); } }

  22. Solution (Trial) 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 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 NotEvenException extends Exception { public NotEvenException (String message){ super(message); } }

More Related