Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, December 13, 2012

File.setLastModified & File.lastModified

Have observed interesting behavior of File.lastModified file property on Linux. Basically, my problem was that I was incrementing the value of that property by 1 in one thread and monitoring the change in the other thread. And apparently no change in property's value happened, the other thread did not see increment. After some time trying to make it work, I realized that I have to increment it at least by a 1000 to make the change visible.

Wondering why that is happening, I have had a look at JDK source code and that's what I found:

JNIEXPORT jlong JNICALL
Java_java_io_UnixFileSystem_getLastModifiedTime(JNIEnv *env, jobject this,
                                                jobject file)
{
    jlong rv = 0;

    WITH_FIELD_PLATFORM_STRING(env, file, ids.path, path) {
        struct stat64 sb;
        if (stat64(path, &sb) == 0) {
            rv = 1000 * (jlong)sb.st_mtime;
        }
    } END_PLATFORM_STRING(env, path);
    return rv;
}

What happens is that on Linux File.lastModified has 1sec resolution and simply ignores milliseconds. I'm not an expert in Linux programming, so not sure is there any way get that time with millisecond resolution on Linux. Assume it should be possible because 'setLastModified' seems like is working as it is expected to work - sets modification time with millisecond resolution (you can find the source code in 'UnixFileSystem_md.c').

So, just a nice thing to remember: when you work with files on Linux, you may not see change in File.lastModified when it's value updated for less than 1000ms.

Thursday, March 4, 2010

Redirecting or error output to variable in shell

Spent couple of painful hours trying to do that. And eventually, here is the code which will output standard output into the file and error into the variable:

var=`(ls -l > ./file.txt) 2>&1`

bloody shell...

Tuesday, February 23, 2010

Kill all child processes from shell script

Small and simple script. Creates Ctrl-C trap and kills all it's child processes in it. Nice to have when your script has many child processes which execution have to be stopped when main script is interrupted by Ctrl-C or other signal.


kill_child_processes() {
isTopmost=$1
curPid=$2
childPids=`ps -o pid --no-headers --ppid ${curPid}`
for childPid in $childPids
do
kill_child_processes 0 $childPid
done
if [ $isTopmost -eq 0 ]; then
kill -9 $curPid 2> /dev/null
fi
}

# Ctrl-C trap. Catches INT signal
trap "kill_child_processes 1 $$; exit 0" INT

for (( i = 0 ; i <= 5; i++ ))
do
# do something...
sleep 10 &
done

wait

Thursday, August 20, 2009

How to make IntelliJ Idea usable on linux

Which Idea on Linux there are two big problems - memory (which I failed to resolve :)) and overall performance. When I've just installed it, it wasn't usable on may box with four Xeon CPUs and four gigs of memory, and that fact surprised me a lot. After some time spent looking on Internet found several tips, which resolved most of my problems. First one is following java setting, which switches off storing images in pixelmaps:

-Dsun.java2d.pmoffscreen=false

Another thing is one of Linux file system settings, which disables update of accessed timestamp. I'm working with quite big projects, which have several thousand files and updating timestamp of each file access is unnecessary overhead. That feature is something which doesn't have lots of use and can be witched off, specially when you are running Linux as desktop. Just add following to your filesystem mount settings in fstab:

noatime,nodiratime

After that Idea became reasonably fast, but I must admit, on Win XP or Vista it still runs faster and uses less memory.

I'm running Fedora 10 with NVidia graphic drivers.

Udpate 2010.12: I've just installed Idea 10. It looks just awesome and it's much faster! Works brilliantly. Thanks to IntelliJ!