0% found this document useful (0 votes)
50 views8 pages

Ospp-Tute-File Management

The document provides 5 examples of using file locking and file attributes in C programming. Example 1 demonstrates locking a region of bytes in a file with a read lock. Example 2 shows applying and releasing a write lock on a file. Example 3 applies a read lock and reads from the locked file. Example 4 allows the user to choose a read or write lock, and locks and unlocks a file accordingly. Example 5 displays attributes of a file like type, owner, permissions, size and timestamps using the stat structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views8 pages

Ospp-Tute-File Management

The document provides 5 examples of using file locking and file attributes in C programming. Example 1 demonstrates locking a region of bytes in a file with a read lock. Example 2 shows applying and releasing a write lock on a file. Example 3 applies a read lock and reads from the locked file. Example 4 allows the user to choose a read or write lock, and locks and unlocks a file accordingly. Example 5 displays attributes of a file like type, owner, permissions, size and timestamps using the stat structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

File management programs

Practice session

Program Example-1: locks number of bytes

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

int main()
{
// Buffer to read file data
char buf[100];

// Data type of lock


struct flock fl;

// Type specifies the type of the lock; one of F_RDLCK, F_WRLCK,


// or F_UNLCK
fl.l_type = F_RDLCK;

// This corresponds to the whence argument to fseek or lseek, and specifies what


// the offset is relative to. Its value can be one of SEEK_SET, SEEK_CUR,
// or SEEK_END.
// SEEK_SET moves the pointer to beginning of the file
fl.l_whence = SEEK_SET;

// This specifies the offset of the start of the region to which the lock applies,
// and is given in bytes relative to the point specified by the l_whence member.
fl.l_start = 0;

// This specifies the length of the region to be locked. A value of 0 is treated
// specially; it means the region extends to the end of the file.
fl.l_len= 10;

// This field is the process ID of the process holding the lock


fl.l_pid = getpid();

// Open the file to be locked


int fd = open("file.txt", O_RDONLY);

// Unable to open file


if (fd == -1)
{
perror("File error ");
exit(EXIT_FAILURE);
}

// Apply the read lock


printf("Lock will be applied\n");

// The first parameter is file descriptor


// Second parameter is the ‘command’ for locking. F_SETLKW Sets or clears a
// files segment lock according to the lock description pointed to by ‘l_type’,
// F_RDLCK in this case. If a shared or exclusive lock is blocked by other locks,
// the thread will wait until the request can be satisfied
// Third is argument to the ‘command’
fcntl(fd, F_SETLKW, &fl);

printf("Locked\n");

// Read the contents of the file


read(fd, buf, 100);
printf(“File content read:\n");
printf("%s\n",buf);

// Release the applied lock


fl.l_type = F_UNLCK;
fcntl(fd, F_SETLK, &fl)
printf("Unlocked.\n");

close(fd);
return 0;
}

Example 2: demonstrate the write lock

This program will write lock a sample.txt file. As it is write lock, when try
to execute the same program in another window, it wont lock. Only when
the first program (first window) releases lock, the second window will
apply lock

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char buf[100];
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len= 0;
fl.l_pid = getpid();

printf("P1 Applying\nwrite lock");


int fd = open("sample.txt", O_WRONLY|O_APPEND);
if (fd == -1)
{
perror("Unable to open file: ");
exit(EXIT_FAILURE);
}
printf("Press Enter to try to get lock -");
getchar();
printf("Locking...\n");
if (fcntl(fd, F_SETLKW, &fl) == -1)
{
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}
printf("Locked\n");
strcpy(buf,"we are appending file\n");
if (write(fd,buf,strlen(buf))<0)
{
perror("Problem in writing to file");
exit(1);
}
printf("Press Enter to release lock -");
getchar();
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) == -1)
{
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}
printf("Unlocked.\n");
close(fd);
return 0;
}

Example 3: demonstration of read lock

This program will read lock a sample.txt file. As it is read lock, when try to
execute the same program in another window, it will lock. This is because
two reader shall operate on file simultaneously

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

int main()
{
char buf[100];
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len= 0;
fl.l_pid = getpid();
printf("Applying\nread lock");
int fd = open("sample.txt", O_RDONLY);
if (fd == -1)
{
perror("Unable to open file: ");
exit(EXIT_FAILURE);
}

printf("Press Enter to try to get lock -");


getchar();
printf("Locking...\n");

if (fcntl(fd, F_SETLKW, &fl) == -1)


{
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}
printf("Locked\n");

int flag=read(fd,buf,100);
if(flag<0){
printf("Error occured while reading\n");
exit(1);
}
printf("Read file content:\n");
printf("%s\n",buf);

printf("Press Enter to release lock -");


getchar();
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) == -1)
{
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}
printf("Unlocked.\n");

close(fd);

return 0;
}

Example 4: demonstration of read lock and write lock

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
//Create an variable of type struct flock to define the properties of locking

struct flock fl;


fl.l_type = F_WRLCK; //type is write lock
fl.l_whence = SEEK_SET; //starting from the beginning of the file
fl.l_start = 0; //no offset
fl.l_len= 0; //to the end of the file. Note that 0 means to the EOF.
fl.l_pid = getpid(); //current process id.

//Ask user for the type of lock


printf("What type of lock do you want?\n1.write/exclusive lock\n2.read/shared lock\
nAnswer: ");
int choice;
scanf("%d",&choice);
getchar();

if(choice==2)
fl.l_type = F_RDLCK;

//Open the file


int fd = open("lockdemo.c", O_RDWR); //Here we have opened the file with O_RDWR so
that both types of locking will work.
if (fd == -1)
{
perror("Unable to open file: ");
exit(EXIT_FAILURE);
}

//Acquire the lock using F_SETLW to wait for lock.


printf("Press <RETURN> to try to get lock: ");
getchar();
printf("Trying to get lock...\n");

if (fcntl(fd, F_SETLKW, &fl) == -1)


{
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}

printf("Locked\n");

//Release the lock


printf("Press <RETURN> to release lock: ");
getchar();
fl.l_type = F_UNLCK; // set to unlock same region

if (fcntl(fd, F_SETLK, &fl) == -1)


{
perror("fcntl caused some error: ");
exit(EXIT_FAILURE);
}

printf("Unlocked.\n");

//close the open file


close(fd);

return 0;
}

Example 5 : Demonstration of file attributes

// dusplay file attributes


#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
struct stat sb;

if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}

if (stat(argv[1], &sb) == -1) {


perror("stat");
exit(EXIT_FAILURE);
}
printf("File type: ");
//S_IFMT is a bit mask used to extract the file type code from a mode value

switch (sb.st_mode & S_IFMT) {


case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}

printf("I-node number: %ld\n", (long) sb.st_ino);

printf("Mode: %lo (octal)\n",


(unsigned long) sb.st_mode);

printf("Link count: %ld\n", (long) sb.st_nlink);


printf("Ownership: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid);

printf("Preferred I/O block size: %ld bytes\n",


(long) sb.st_blksize);
printf("File size: %lld bytes\n",
(long long) sb.st_size);
printf("Blocks allocated: %lld\n",
(long long) sb.st_blocks);

printf("Last status change: %s", ctime(&sb.st_ctime));


printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime));

exit(EXIT_SUCCESS);
}

You might also like