Upgrade to Pro — share decks privately, control downloads, hide ads and more …

go doSomeThing()

go doSomeThing()

Jérémy Courtial

February 24, 2016
Tweet

More Decks by Jérémy Courtial

Other Decks in Programming

Transcript

  1. func NewCipher(key []byte) (cipher.Block, error) type Block interface {
 


    BlockSize() int
 
 Encrypt(dst, src []byte)
 
 Decrypt(dst, src []byte)
 }
  2. func main() { http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request ) {


    msg := map[string]string {"msg": "Hello, World!"}
 j, _ := json.Marshal(msg)
 w.Write(j)
 })
 
 http.ListenAndServe(":8080", nil) }
  3. c <- "Hello World!" time.Sleep(5) c <- "Have a nice

    day!" time.Sleep(5) c <- "Bye bye!"
  4. func work(quit, c chan int) { select { case n

    := <- c : process(n) case <- quit : } }
  5. func work(quit, c chan int) { select { case n

    := <- c : process(n) case <- quit : return } }
  6. #BEGIN JAVA #END JAVA while (true) { new Thread(() ->

    { sleep(Long.MAX_VALUE); }); } // Quick & dirty POC
  7. #BEGIN JAVA #END JAVA while (true) { new Thread(() ->

    { sleep(Long.MAX_VALUE); }); } // What will happen ?
  8. Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native

    thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:714) at Test.main(Test.java:19) $ javac Test.java && java Test
  9. /* - Each thread has is own stack - Stack's

    size is hard to predict => alloc ~1 Mo per thread */
  10. |- — — — — — — - - -

    - -| | Thread Stack 1 | |- - - - - - - - - - - -| | . | | . | | . | | . | | . | | . | | . | | . | | . | |_ _ _ _ _ _ _ _ _ _ _ _|
  11. |- — — — — — — - - -

    - -| | Thread Stack 1 | |- - - - - - - - - - - -| | Guard Page | | — — — — — — — - - - - | | . | | . | | . | | . | | . | | . | | . | |_ _ _ _ _ _ _ _ _ _ _ _|
  12. |- — — — — — — - - -

    - -| | Thread Stack 1 | |- - - - - - - - - - - -| | Guard Page | | — — — — — — — - - - - | | Thread Stack 2 | |- - - - - - - - - - - -| | . | | . | | . | | . | | . | |_ _ _ _ _ _ _ _ _ _ _ _|
  13. |- — — — — — — - - -

    - -| | Thread Stack 1 | |- - - - - - - - - - - -| | Guard Page | | — — — — — — — - - - - | | Thread Stack 2 | |- - - - - - - - - - - -| | Guard Page | |- - - - - - - - - - - -| | . | | . | | . | |_ _ _ _ _ _ _ _ _ _ _ _|
  14. // Event Driven // Always use async I/O // Considers

    I/O as events producers // Events are gathered by a loop
  15. |- — — — — — — - - -

    - -| | | | | | | | | | | |- - - - - - - - - - - -| | | | | | | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| // User space // Kernel Space
  16. |- — — — — — — - - -

    - -| | | | | | | | | | | |- - - - - - - - - - - -| | | | | | | | | | Process/Mem management| |_ _ _ _ _ _ _ _ _ _ _ _| // User space // Kernel Space
  17. |- — — — — — — - - -

    - -| | | | | | | | | | | |- - - - - - - - - - - -| | | | | | Drivers | | | | Process/Mem management| |_ _ _ _ _ _ _ _ _ _ _ _| // User space // Kernel Space
  18. |- — — — — — — - - -

    - -| | | | | | | | | | | |- - - - - - - - - - - -| | SysCall Interface | | | | Drivers | | | | Process/Mem management| |_ _ _ _ _ _ _ _ _ _ _ _| // User space // Kernel Space
  19. |- — — — — — — - - -

    - -| | Applications | | | | | | | | | |- - - - - - - - - - - -| | SysCall Interface | | | | Drivers | | | | Process/Mem management| |_ _ _ _ _ _ _ _ _ _ _ _| // User space // Kernel Space
  20. |- — — — — — — - - -

    - -| | Applications | | | | Daemons | | | | | |- - - - - - - - - - - -| | SysCall Interface | | | | Drivers | | | | Process/Mem management| |_ _ _ _ _ _ _ _ _ _ _ _| // User space // Kernel Space
  21. |- — — — — — — - - -

    - -| | Applications | | | | Daemons | | | | Librairies | |- - - - - - - - - - - -| | SysCall Interface | | | | Drivers | | | | Process/Mem management| |_ _ _ _ _ _ _ _ _ _ _ _| // User space // Kernel Space
  22. |- — — — — — — - - -

    - -| | | | | | NewOSThread() | | | | | |- — — — — — — - - - - -| | | | | | | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| // Kernel threads
  23. |- — — — — — — - - -

    - -| | | | | | NewOSThread() | | | | | | |- - - - - | - - - - - -| | ∨ | | | | kernel_thread(…) | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| // Kernel threads
  24. |- — — — — — — - - -

    - -| | | | User Thread 1 | | | | | | | |- — — — — — — - - - - -| | | | | | | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| /* Users threads */
  25. |- — — — — — — - - -

    - -| | | | User Thread 1 | | User Thread 2 | | | | | |- — — — — — — - - - - -| | | | | | | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| /* Users threads */
  26. |- — — — — — — - - -

    - -| | | | User Thread 1 | | User Thread 2 | | User Thread 3 | | | |- — — — — — — - - - - -| | | | | | | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| /* Users threads */
  27. |- — — — — — — - - -

    - -| | — — — — — — — — | | | User Thread 1 | | | | User Thread 2 | | | | User Thread 3 | | | - - - - - - - - | |- — — — — — — - - - - -| | | | | | | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| /* Users threads */
  28. |- — — — — — — - - -

    - -| | — — — — — — — — | | | User Thread 1 | | | | User Thread 2 | | | | User Thread 3 | | | - - - | - - - - | |- - - - - | - - - - - -| | ∨ | | | | kernel_thread(…) | | | | | |_ _ _ _ _ _ _ _ _ _ _ _| /* Users threads N:M */
  29. // User Threads // Cheaper than kernel threads // Backed

    by one or more K threads // App is responsible for scheduling
  30. // Goroutines are // Cheap in memory : 2 Ko

    only // Cheap to create/destroy // Cheap to switch
  31. // Goroutines scheduling // Scheduling is cooperative // Blocking IOs

    used as switch point // When a gorountine blocks another // is wake up