Giter Club home page Giter Club logo

linux-file-io-systems-locking's Introduction

Linux-File-IO-Systems-locking

Ex07-Linux File-IO Systems-locking

Name: Roopak C S

Register Number: 212223220088

AIM:

To Write a C program that illustrates files copying and locking

DESIGN STEPS:

Step 1:

Navigate to any Linux environment installed on the system or installed inside a virtual environment like virtual box/vmware or online linux JSLinux (https://bellard.org/jslinux/vm.html?url=alpine-x86.cfg&mem=192) or docker.

Step 2:

Write the C Program using Linux IO Systems locking

Step 3:

Execute the C Program for the desired output.

PROGRAM:

1.To Write a C program that illustrates files copying

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main() {
    char block[1024];
    int in, out;
    int nread;
    in = open("filecopy.c", O_RDONLY);
    out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
    while((nread = read(in,block,sizeof(block))) > 0)
    write(out,block,nread);
    exit(0);
}

2.To Write a C program that illustrates files locking

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

int main (int argc, char* argv[]) { 
    char* file = argv[1];
    int fd;
    struct flock lock;
    printf ("opening %s\n", file);
    /* Open a file descriptor to the file. */
    fd = open (file, O_WRONLY);
    // acquire shared lock
    if (flock(fd, LOCK_SH) == -1) {
        printf("error");
    }
    else {
        printf("Acquiring shared lock using flock");
    }
    getchar();
    // non-atomically upgrade to exclusive lock
    // do it in non-blocking mode, i.e. fail if can't upgrade immediately
    if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
        printf("error");
    }
    else {
        printf("Acquiring exclusive lock using flock");
    }
    getchar();
    // release lock
    // lock is also released automatically when close() is called or process exits
    if (flock(fd, LOCK_UN) == -1) {
        printf("error");
    }
    else {
        printf("unlocking");
    }
    getchar();
    close (fd);
    return 0;
}

OUTPUT

image

image

RESULT:

The programs are executed successfully.

linux-file-io-systems-locking's People

Contributors

roopakcs avatar gowriganeshns avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.