Posts de ‘Francisco Souza’

[Francisco Souza] Go solution for the Dining philosophers problem

Sunday, October 30th, 2011

I spent part of the sunday solving the Dining Philosophers using Go. The given solution is based in the description for the problem present in The Little Book of Semaphores:

The Dining Philosophers Problem was proposed by Dijkstra in 1965, when dinosaurs ruled the earth. It appears in a number of variations, but the standard features are a table with five plates, five forks (or chopsticks) and a big bowl of spaghetti.

There are some constraints:

  • Only one philosopher can hold a fork at a time
  • It must be impossible for a deadlock to occur
  • It must be impossible for a philosopher to starve waiting for a fork
  • It must be possible for more than one philosopher to eat at the same time

No more talk, here is my solution for the problem:

package main

import (    "fmt"    "sync"    "time")

type Fork struct {    sync.Mutex}

type Table struct {    philosophers chan Philosopher    forks []*Fork}

func NewTable(forks int) *Table {    t := new(Table)    t.philosophers = make(chan Philosopher, forks - 1)    t.forks = make([]*Fork, forks)    for i := 0; i < forks; i++ {        t.forks[i] = new(Fork)    }    return t}

func (t *Table) PushPhilosopher(p Philosopher) {    p.table = t    t.philosophers <- p}

func (t *Table) PopPhilosopher() Philosopher {    p := <-t.philosophers    p.table = nil    return p}

func (t *Table) RightFork(philosopherIndex int) *Fork {    f := t.forks[philosopherIndex]    return f}

func (t *Table) LeftFork(philosopherIndex int) *Fork {    f := t.forks[(philosopherIndex + 1) % len(t.forks)]    return f}

type Philosopher struct {    name string    index int    table *Table    fed chan int}

func (p Philosopher) Think() {    fmt.Printf("%s is thinking...\n", p.name)    time.Sleep(3e9)    p.table.PushPhilosopher(p)}

func (p Philosopher) Eat() {    p.GetForks()    fmt.Printf("%s is eating...\n", p.name)    time.Sleep(3e9)    p.PutForks()    p.table.PopPhilosopher()    p.fed <- 1}

func (p Philosopher) GetForks() {    rightFork := p.table.RightFork(p.index)    rightFork.Lock()

    leftFork := p.table.LeftFork(p.index)    leftFork.Lock()}

func (p Philosopher) PutForks() {    rightFork := p.table.RightFork(p.index)    rightFork.Unlock()

    leftFork := p.table.LeftFork(p.index)    leftFork.Unlock()}

func main() {    table := NewTable(5)    philosophers := []Philosopher{        Philosopher{"Thomas Nagel", 0, table, make(chan int)},        Philosopher{"Elizabeth Anscombe", 1, table, make(chan int)},        Philosopher{"Martin Heidegger", 2, table, make(chan int)},        Philosopher{"Peter Lombard", 3, table, make(chan int)},        Philosopher{"Gottfried Leibniz", 4, table, make(chan int)},    }

    for {        for _, p := range philosophers {            go func(p Philosopher){                p.Think()                p.Eat()            }(p)        }

        for _, p := range philosophers {            <-p.fed            fmt.Printf("%s was fed.\n", p.name)        }    }

}

Any feedback is very welcome.

[Francisco Souza] Go solution for the Dining philosophers problem

Sunday, October 30th, 2011

I spent part of the sunday solving the Dining Philosophers using Go. The given solution is based in the description for the problem present in The Little Book of Semaphores:

The Dining Philosophers Problem was proposed by Dijkstra in 1965, when dinosaurs ruled the earth. It appears in a number of variations, but the standard features are a table with five plates, five forks (or chopsticks) and a big bowl of spaghetti.

There are some constraints:

  • Only one philosopher can hold a fork at a time
  • It must be impossible for a deadlock to occur
  • It must be impossible for a philosopher to starve waiting for a fork
  • It must be possible for more than one philosopher to eat at the same time

No more talk, here is my solution for the problem:

package main

import (    "fmt"    "sync"    "time")

type Fork struct {    sync.Mutex}

type Table struct {    philosophers chan Philosopher    forks []*Fork}

func NewTable(forks int) *Table {    t := new(Table)    t.philosophers = make(chan Philosopher, forks - 1)    t.forks = make([]*Fork, forks)    for i := 0; i < forks; i++ {        t.forks[i] = new(Fork)    }    return t}

func (t *Table) PushPhilosopher(p Philosopher) {    p.table = t    t.philosophers <- p}

func (t *Table) PopPhilosopher() Philosopher {    p := <-t.philosophers    p.table = nil    return p}

func (t *Table) RightFork(philosopherIndex int) *Fork {    f := t.forks[philosopherIndex]    return f}

func (t *Table) LeftFork(philosopherIndex int) *Fork {    f := t.forks[(philosopherIndex + 1) % len(t.forks)]    return f}

type Philosopher struct {    name string    index int    table *Table    fed chan int}

func (p Philosopher) Think() {    fmt.Printf("%s is thinking...\n", p.name)    time.Sleep(3e9)    p.table.PushPhilosopher(p)}

func (p Philosopher) Eat() {    p.GetForks()    fmt.Printf("%s is eating...\n", p.name)    time.Sleep(3e9)    p.PutForks()    p.table.PopPhilosopher()    p.fed <- 1}

func (p Philosopher) GetForks() {    rightFork := p.table.RightFork(p.index)    rightFork.Lock()

    leftFork := p.table.LeftFork(p.index)    leftFork.Lock()}

func (p Philosopher) PutForks() {    rightFork := p.table.RightFork(p.index)    rightFork.Unlock()

    leftFork := p.table.LeftFork(p.index)    leftFork.Unlock()}

func main() {    table := NewTable(5)    philosophers := []Philosopher{        Philosopher{"Thomas Nagel", 0, table, make(chan int)},        Philosopher{"Elizabeth Anscombe", 1, table, make(chan int)},        Philosopher{"Martin Heidegger", 2, table, make(chan int)},        Philosopher{"Peter Lombard", 3, table, make(chan int)},        Philosopher{"Gottfried Leibniz", 4, table, make(chan int)},    }

    for {        for _, p := range philosophers {            go func(p Philosopher){                p.Think()                p.Eat()            }(p)        }

        for _, p := range philosophers {            <-p.fed            fmt.Printf("%s was fed.\n", p.name)        }    }

}

Any feedback is very welcome.

[Francisco Souza] Flying with Flask on Google App Engine

Friday, October 14th, 2011

A little late, finally I introduce the third part of using Python frameworks in Google App Engine. I wrote before about web2py and Django, and now is the time of Flask, a Python microframework based on Werkzeug, Jinja2 and good intentions. Unlike Django and web2py, Flask is not a full stack framework, it has not a database abstraction layer or an object relational mapper, Flask is totally decoupled from model layer. It is really good, because we can use the power of SQLAlchemy (which is, IMHO, the best ever data abstraction framework =P) when we are working with relational databases, and when work with non-relational databases, we can use the native API.

[Francisco Souza] Flying with tipfy on Google App Engine

Friday, October 14th, 2011

Hooray, there is a bonus part in the series (after a looooooooooooong wait =P)! On the first blog post, about Django, I received a comment about the use of tipfy, a small Python web framework made specifically for Google App Engine. Like Flask, tipfy is not a full stack framework and we will not use a database abstraction layer, we will use just the Google App Engine Datastore API, but tipfy was designed for Google App Engine, so it is less laborious to work with tipfy on App Engine.

[Francisco Souza] Using an exclusive Firefox profile for Selenium WebDriver

Friday, October 14th, 2011

Use a exclusive Firefox profile for Selenium WebDriver.

[Francisco Souza] Killer Java applications server with nginx and memcached

Friday, October 14th, 2011

Post showing how to configure a Java application server integrated with nginx and memcached.

[Francisco Souza] Splinter: Python tool for acceptance tests on web applications

Friday, October 14th, 2011

Know Splinter, a Python framework for acceptance testing on web applications.

[Francisco Souza] Testing jQuery plugins with Jasmine

Friday, October 14th, 2011

Learn how to test your jQuery plugins using Jasmine, a JavaScript BDD framework.

[Francisco Souza] Splinter sprint on FISL

Friday, October 14th, 2011

We are going to start tomorrow, on FISL, another splinter sprint. “From June 29 through July 2, 2011, fisl12 will be hosted at the PUC Events Center, in Porto Alegre, Rio Grande do Sul, Brazil.”. But don’t worry about the location: anyone in anywhere can join us in this sprint. There is an entry in splinter wiki about this sprint, and I’m just replicating the information here…

[Francisco Souza] Creating HTML 5 slide presentations using landslide

Friday, October 14th, 2011

Recently I found landslide, which is a Python tool for creating HTML 5 slide presentations.It’s based in a famous slide presentation. It’s a simple script that generates HTML from a source file, which can be formatted using reStructuredText, Textile or Markdown.