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

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

Multithreading Horstmann ch.9. Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Practice Session 7 Synchronization Liveness Guarded Methods Model
System Programming Practical Session 5 Liveness, Guarded Methods, and Thread Timing.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Multithreading Why multi-threads Defining and creating threads An example with two threads Life cycle of a thread An example with user interface Problem.
1 CS2200 Software Development Lecture: Multi-threading II A. O’Riordan, 2009.
Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Threads Just Java: C10–pages 251- C11–pages 275-
Java Threads CS Introduction to Operating Systems.
Multithreading.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
System Programming Web site: Things to know Weekly Homework Each practical session.
Lecture 4 : JAVA Thread Programming
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Nachos Project 1 Start-up and System call
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
Multithreading. Chapter Goals To understand how multiple threads can execute in parallel To learn how to implement threads To understand race conditions.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Chapter 2: Java Fundamentals
1 Web Based Programming Section 8 James King 12 August 2003.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Practice Session 8 Blocking queues Producers-Consumers pattern Semaphore Futures and Callables Advanced Thread Synchronization Methods CountDownLatch Thread.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
MIDAS How to use Midas for posting and checking your grades.
Multithreading in JAVA
Threads. Story so far  Our programs have consisted of single flows of control Flow of control started in the first statement of method main() and worked.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
System Programming Practical Session 4: Concurrency / Safety.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
System Programming Practical Session 4: Concurrency / Safety.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
…in java DThread Pools x. Thread Pools: Why? Source code updates – copy/paste of run/runnable method for each thread is exhausting There is overhead to.
Multithreaded applets
Practice Session 8 Lockfree LinkedList Blocking queues
Java Programming Language
Section 5.7 Concurrency, Interference, and Synchronization.
System Programming Practical Session 7
CS Week 10 Jim Williams, PhD.
Threads Chate Patanothai.
Distributed Systems - Comp 655
Condition Variables and Producer/Consumer
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
CS18000: Problem Solving and Object-Oriented Programming
Condition Variables and Producer/Consumer
Multithreading 2 Lec 24.
Principles of Software Development
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
class PrintOnetoTen { public static void main(String args[]) {
Threads in Java James Brucker.
Using threads for long running tasks.
Representation and Management of Data on the Internet
Multiplexing/Demux.
Array Review Selection Sort
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

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

פתיחת חשבון (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 Check account. cs.bgu.ac.il -> Resources -> CS Webmail

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

הגדרת חשבון 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 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.

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

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

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

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:

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

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

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. i line number 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 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); }

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 }

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 }

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