> Hi, > > I am trying to lock a file ( using jni and fcntl( ) ) from java on Linux, and > i am not able to do it. > > When I write pure C++ code to achieve the same functionality, the locking > happens fine - ie, when i run the program it locks the file, and when I run > the same instance of the program again ( when the file is already locked and > one instance of the program is already running ), the program returns as > expected, bcos the file is already locked. > > When I write a JNI wrapper ( at the end of this mail ) around this C++ code, > and run the program in java - the first instance locks the file successfully; > however the 2nd instance does not return!! ( It seems to be blocking waiting > for the lock to be released !) > > Can someone tell me how to make the 2nd instance return without blocking ?? > > Thanks in advance, > > Nandakumar. > > > JNIEXPORT jint JNICALL Java_Lock_LockFile(JNIEnv *env, jobject obj, jstring > filename) { > int fd; > const char *file_name ; > > struct flock lock_it; > > lock_it.l_start = 0; /* -"- */ > lock_it.l_whence = 0; /* from current point */ > lock_it.l_len = 0; /* until end of file */ > lock_it.l_type = F_WRLCK; /* set exclusive/write lock */ > lock_it.l_pid = 0; /* pid not actually interesting */ > > file_name = env->GetStringUTFChars(filename,0); > > if((fd = open(file_name,O_WRONLY))<0) > { > cout<<"Cannot open the file "<<"["<<errno<<"]"<<endl; > return -1; > } > > cout<<"opened the file " << file_name <<endl; > > if (fcntl(fd,F_SETLK, &lock_it)<0) > { > cout<<"Cannot Lock the file"<<"["<<errno<<"]"<<endl; > return -1; > } > cout<<"locked the file " << file_name <<endl; > return 1; > > } ---------------------------------------------------------------------- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]