3

I want to use system locks to avoid race conditions. One process being an opencv program saving captured image to a .jpg file. The other process being browser fetching the same image file from the server. I want to avoid race condition between these two processes. Am i using the flock function right?

My opencv code is:

#include<sys/file.h> 
#include<fcntl.h>
#include<string.h>
#include "cv.h"
#include "highgui.h"
#include <stdio.h>

int main() 
{
    int fd1,fd2;
    struct flock lock;
    fd1=open("a1.jpg",O_WRONLY);
    fd2=open("a2.jpg",O_WRONLY);
     lock.l_type=F_UNLCK;
     fcntl(fd1,F_SETLKW,&lock);

    CvCapture* capture = cvCaptureFromCAM(-1);
    if ( !capture )
    {

            fprintf( stderr, "ERROR: capture is NULL \n" );
            getchar();
            return -1;
    }

    cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
    while ( 1 )
    {
            IplImage* frame = cvQueryFrame( capture );
            if ( !frame )

                    fprintf( stderr, "ERROR: frame is null...\n" );
                    getchar();
                    break;
            }
            cvShowImage( "mywindow", frame );
            if(flock(fd1,LOCK_EX)==0)
            {
            printf("A1 is locked");
            cvSaveImage("a1.jpg",frame,0);
            flock(fd1,LOCK_UN);
            printf("A1 unlocked");
            }
            else
            {
            flock(fd2,LOCK_EX);
            cvSaveImage("a2.jpg",frame,0);
            flock(fd2,LOCK_UN);
            printf("A2 is unlocked");
            }
            if ( (cvWaitKey(500) & 255) == 27 )
                    break;
    }
    cvReleaseCapture( &capture );
    cvDestroyWindow( "mywindow" );
    close(fd1);
    close(fd2);
    return 0;

}

And my .php file is

<?php
$dest='a.jpg'; 
$src1='a1.jpg';
$src2='a2.jpg';
if(flock($fp1,LOCK_EX))
{
    echo "a1";
    $img=imagecreatefromjpeg($src1);
    $copy=imagejpeg($img,$dest);
    imagedestroy($img);
    flock($fp1,LOCK_UN);
}
else
{
    echo "A2";
    flock($fp2,LOCK_EX);
    $img=imagecreatefromjpeg($src2);
    $copy=imagejpeg($img,$dest);
    imagedestroy($img);

    flock($fp2,LOCK_UN);
}
?>

This php is called by the client side using AJAX.

hakre
  • 193,403
  • 52
  • 435
  • 836
user927774
  • 43
  • 1
  • 4
  • In which sense are you uncertain about the rightness of the usage? Have you tested your code and you get wired results? Can you elaborate a bit? – hakre Feb 26 '12 at 11:38
  • If flock is successful it should return 0 right, jowever it is not returning any value. Also when i try to access the file by some other process i can access it. So it means that i'm not getting a lock on that file. – user927774 Feb 26 '12 at 11:43
  • Is that inside the C part or the PHP part? http://php.net/flock – hakre Feb 26 '12 at 11:49
  • You do realize that locks created with flock are *advisory* locks? A lock won't block another process unless that process also tries to lock the file. – John Feb 26 '12 at 13:14
  • Also, if it does not return 0, what does it return? The return type for flock is int so it will always return an integer. – John Feb 26 '12 at 13:22
  • @user927774 your code doesn't check all the the return values of flock. How do you know it's not returning any value ? You also seem to have a fcntl call in there, fcntl locks and flock locks does not interact with eachother. – nos Feb 27 '12 at 20:52

1 Answers1

0

flock(2) blocks if you are asking for an exclusive lock and it is unavailable. It returns 0 when all is behaving normally -- it will return -1 if there is an error, which you can then examine using perror(3) or the errno variable.

It appears that your C code is at least vaguely following the correct protocol -- getting an exclusive lock on a shared file before writing it and then unlocking it -- but I cannot understand what your code is trying to do overall, so it is difficult to understand whether or not your code is doing the right thing.

Perry
  • 4,363
  • 1
  • 17
  • 20