Department of Physics AstroLab

Examples of file Locking in GCC and Python

Beej's guide for file locking in GCC


GCC: putting a pair of floats into a file


#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
    int  fd;
    int  ret_status;
    char  buffy[128];

    float ra_deg  = 292.4344;
    float dec_deg = 28.0004; 
    sprintf(buffy, "%8.4f  %8.4f \n", ra_deg, dec_deg);

    struct flock fl;
    fl.l_type   = F_WRLCK;  /* F_RDLCK, F_WRLCK, F_UNLCK    */
    fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
    fl.l_start  = 0;        /* Offset from l_whence         */
    fl.l_len    = 0;        /* length, 0 = to EOF           */
    fl.l_pid    = getpid(); /* our PID                      */

    if (access("tel_position", F_OK) == 0) {
       fd = open("tel_position",O_RDWR|O_TRUNC);}
    else { 
       fd = creat("tel_position",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);}

    ret_status = fcntl(fd, F_SETLKW, &fl);
    write(fd,buffy,strlen(buffy));
//    sleep(5);
    fl.l_type   = F_UNLCK;
    fcntl(fd, F_SETLK, &fl); /* set the region to unlocked */
    close(fd);
}

GCC: getting a pair of floats from a file


#include <stdio.h> 
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main()
{
    float ra_deg;
    float de_deg;
    int  fd;
    char  buffy[32];
    int   nchar =32;
    struct flock fl;

    fl.l_type   = F_RDLCK;  /* F_RDLCK, F_WRLCK, F_UNLCK    */
    fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
    fl.l_start  = 0;        /* Offset from l_whence         */
    fl.l_len    = 0;        /* length, 0 = to EOF           */
    fl.l_pid    = getpid(); /* our PID                      */

    fd = open("tel_position",O_RDONLY); /* get the file descriptor */
    fcntl(fd, F_SETLKW, &fl);
    read(fd, buffy, 32);
    fl.l_type   = F_UNLCK;   /* tell it to unlock the region */
    fcntl(fd, F_SETLK, &fl); /* set the region to unlocked */
    close(fd);

    sscanf (buffy, "%f %f", &ra_deg, &de_deg);
    printf("telescope position: %f %f \n",ra_deg, de_deg);
}

python: putting a pair of floats into a file


import time
import fcntl, struct

ra_deg = '192.0'
dec_deg = '29.0'
tel_pos_out = open('tel_position','w')
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
while 1:
    try:
        fcntl.fcntl(tel_pos_out.fileno(),fcntl.F_SETLKW, lockdata)
        tel_pos_out.write(ra_deg+'  '+dec_deg +"\n")
#        time.sleep(15)
        tel_pos_out.close()
        break
    except:
        time.sleep(0.01)

python: getting a pair of floats from a file


import time
import fcntl, struct

tel_pos_out = open('tel_position','r')
lockdata = struct.pack('hhllhh', fcntl.F_RDLCK, 0, 0, 0, 0, 0)
while 1:
    try:
        fcntl.fcntl(tel_pos_out.fileno(),fcntl.F_SETLKW, lockdata)
        line = tel_pos_out.readline()
        tel_pos_out.close()
        print line.split()
        break
    except:
        time.sleep(0.01)

Back to the AstroLab Home Page jrl 2013-Dec-22 23:30:49 UTC