Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use flock()

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.

like image 287
user927774 Avatar asked Feb 22 '26 16:02

user927774


1 Answers

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.

like image 66
Perry Avatar answered Feb 24 '26 04:02

Perry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!