Technical blog

June 8, 2008

Making glchess work

Filed under: linux — Tags: — paawak @ 11:35

I am using Fedora 7. To begin with, glchess used to work fine for me. Then suddenly it stopped working. It was giving a strange error. After googling a bit, I realised that it stopped working because I upgraded my Python from 2.0 to 2.5. Lucky for me though, I had python2 in my /usr/bin. So, this is the fix: I opened /usr/bin/glchess, and changed the header to

! /usr/bin/python2

And voila, it started working again!

The moral of the story is, glchess does not work with Python 2.5. It needs Python 2.

June 1, 2008

Magic of ps

Filed under: linux — Tags: — paawak @ 11:34

The command

$ps -eF

lists all process along-with the full command which started the process. When I ran this, I could not see the entire command due the width restriction of my terminal. This is the solution to set unlimited width:

$ps -eF -w -w

Now, suppose I want to know a specific process by its name:

$ps -F -C java -w -w

The -C option accepts a list of process name like:

$ps -F -C “java xmms” -w -w

For a list of files opened by a process, use

$/usr/sbin/lsof -p 3456 | grep XXXX

[Courtesy Prakash]

Producer/Consumer

Filed under: java, thread — Tags: , — paawak @ 11:29

I have tried to come-up with a thread safe and a non thread safe version of a typical producer/consumer scenario.

 
 
package com.swayam.exp;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
 
/**
 * 
 * @author paawak
 */
public class ProducerConsumerSimulator implements Runnable {
 
    private final int sleepInterval;
    private final List<String> list;
    private final boolean producer;
    private final boolean threadSafe;
 
    public ProducerConsumerSimulator(boolean producer, boolean threadSafe,
            int sleepInterval, List<String> list) {
        this.producer = producer;
        this.sleepInterval = sleepInterval;
        this.list = list;
        this.threadSafe = threadSafe;
    }
 
    public void run() {
 
        while (true) {
 
            try {
                Thread.sleep(sleepInterval);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
 
            if (producer) {
 
                produce();
 
                if (threadSafe) {
                    synchronized (list) {
                        System.out.println("Consumer, wake-up!!!");
                        list.notify();
                    }
                }
 
            } else {
 
                if (threadSafe) {
                    synchronized (list) {
                        try {
                            System.out.println("Waiting for producer...");
                            list.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
 
                consume();
 
            }
 
        }
 
    }
 
    private void produce() {
 
        Calendar cal = Calendar.getInstance();
 
        String str = "Time Now -- " + cal.get(Calendar.MINUTE) + ":"
                + cal.get(Calendar.SECOND) + ":"
                + cal.get(Calendar.MILLISECOND);
 
        list.add(str);
 
        System.out.println("ADDED:: " + str);
 
    }
 
    private void consume() {
 
        try {
            String str = list.remove(0);
            System.out.println("REMOVED:: " + str);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
 
    }
 
    /**
     * For testing
     * 
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
 
        int PRODUCER_SLEEP_INTERVAL = 1000;
        int CONSUMER_SLEEP_INTERVAL = 30;
        boolean THREAD_SAFE_EXECUTION = true;
 
        List<String> list = new ArrayList<String>();
        Thread producer = new Thread(new ProducerConsumerSimulator(true,
                THREAD_SAFE_EXECUTION, PRODUCER_SLEEP_INTERVAL, list));
        producer.start();
 
        Thread consumer = new Thread(new ProducerConsumerSimulator(false,
                THREAD_SAFE_EXECUTION, CONSUMER_SLEEP_INTERVAL, list));
        consumer.start();
 
    }
 
}

Run this with

boolean THREAD_SAFE_EXECUTION = true;

and

boolean THREAD_SAFE_EXECUTION = false;

And see the difference yourself :).

Powered by WordPress