खोज…


वाक्य - विन्यास

  • int fcntl (int fd, int cmd, struct flock *);
  • int lockf (int fd, int cmd, off_t len);

POSIX रिकॉर्ड ताले (fcntl)

यह उदाहरण POSIX रिकॉर्ड ताले (उर्फ प्रक्रिया से जुड़े ताले) के उपयोग को प्रदर्शित करता है, जो fcntl फ़ंक्शन (POSIX आधार मानक) द्वारा प्रदान किया जाता है।

टिप्पणियाँ:

  • विशेष और साझा ताले समर्थित हैं।
  • एक बाइट रेंज पर लागू किया जा सकता है, जब भविष्य में डेटा को जोड़ा जाता है ( struct flock द्वारा नियंत्रित) वैकल्पिक रूप से स्वचालित रूप से विस्तार।
  • फ़ाइल के लिए किसी भी फाइल डिस्क्रिप्टर की लॉकिंग प्रक्रिया, या जब प्रक्रिया समाप्त हो जाती है, तब लॉक को पहले बंद किया जाता है।
#include <stdlib.h>  /* for exit() */
#include <stdio.h>   /* for perror() */
#include <string.h>  /* for memset() */
#include <unistd.h>  /* for close() */
#include <fcntl.h>   /* for open(), fcntl() */

int main(int argc, char **argv) {
    /* open file
     * we need O_RDWR for F_SETLK */
    int fd = open(argv[1], O_RDWR);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    struct flock fl;
    memset(&fl, 0, sizeof(fl));

    /* lock entire file */
    fl.l_type = F_RDLCK;    /* F_RDLCK is shared lock */
    fl.l_whence = SEEK_SET; /* offset base is start of the file */
    fl.l_start = 0;         /* starting offset is zero */
    fl.l_len = 0;           /* len is zero, which is a special value
                               representing end of file (no matter
                               how large the file grows in future) */

    /* F_SETLKW specifies blocking mode */
    if (fcntl(fd, F_SETLKW, &fl) == -1) {
        perror("fcntl(F_SETLKW)");
        exit(EXIT_FAILURE);
    }

    /* atomically upgrade shared lock to exclusive lock, but only
     * for bytes in range [10; 15)
     *
     * after this call, the process will hold three lock regions:
     *   [0; 10)        - shared lock
     *   [10; 15)       - exclusive lock
     *   [15; SEEK_END) - shared lock
     */
    fl.l_type = F_WRLCK;    /* F_WRLCK is exclusive lock */
    fl.l_whence = SEEK_SET;
    fl.l_start = 10;
    fl.l_len= 5;

    /* F_SETLKW specifies non-blocking mode */
    if (fcntl(fd, F_SETLK, &fl) == -1) {
        perror("fcntl(F_SETLK)");
        exit(EXIT_FAILURE);
    }

    /* release lock for bytes in range [10; 15) */
    fl.l_type = F_UNLCK;

    if (fcntl(fd, F_SETLK, &fl) == -1) {
        perror("fcntl(F_SETLK)");
        exit(EXIT_FAILURE);
    }

    /* close file and release locks for all regions
     * note that locks are released when process calls close() on any
     * descriptor for a lock file */
    close(fd);

    return EXIT_SUCCESS;
}

ताला समारोह

यह उदाहरण lockf फ़ंक्शन (POSIX XSI) के उपयोग को lockf करता है।

टिप्पणियाँ:

  • केवल अनन्य ताले समर्थित हैं।
  • एक बाइट रेंज पर लागू किया जा सकता है, जब भविष्य में डेटा को जोड़ा जाता है तो वैकल्पिक रूप से स्वचालित रूप से विस्तार किया जाता है ( len तर्क के साथ निर्धारित और स्थिति द्वारा नियंत्रित किया lseek है)।
  • फ़ाइल के लिए किसी भी फाइल डिस्क्रिप्टर की लॉकिंग प्रक्रिया, या जब प्रक्रिया समाप्त हो जाती है, तब लॉक को पहले बंद किया जाता है।
  • के बीच बातचीत fcntl और lockf ताले अनिर्दिष्ट है। लिनक्स पर, lockf POSIX रिकॉर्ड लॉक के लिए एक आवरण है।
#include <stdlib.h>  /* for exit() */
#include <stdio.h>   /* for perror() */
#include <unistd.h>  /* for lockf(), lseek() */
#include <fcntl.h>   /* for open() */

int main(int argc, char **argv) {
    /* open file
     * we need O_RDWR for lockf */
    int fd = open(argv[1], O_RDWR);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    /* set current position to byte 10 */
    if (lseek(fd, 10, SEEK_SET) == -1) {
        perror("lseek");
        exit(EXIT_FAILURE);
    }

    /* acquire exclusive lock for bytes in range [10; 15)
     * F_LOCK specifies blocking mode */
    if (lockf(fd, F_LOCK, 5) == -1) {
        perror("lockf(LOCK)");
        exit(EXIT_FAILURE);
    }

    /* release lock for bytes in range [10; 15) */
    if (lockf(fd, F_ULOCK, 5) == -1) {
        perror("lockf(ULOCK)");
        exit(EXIT_FAILURE);
    }

    return EXIT_SUCCESS;
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow