RDoc 38863
RDoc 38863
1. What is purpose of stat() system call? How can we check whether the file is regular file
or directory using stat()? [2 M] [CO2, PO1]
stat() system call retrieves information about a file, such as its size, permissions, and
timestamps. It fills a structure with file details [0.5M].
To check whether a file a regular file or directory st_mode member of the stat structure can
be used. The example is shown below; [0.5M]
if(S_ISREG(st.st_mode)) [1M]
{
printf("\n File is a regular file\n");
}
else if(S_ISDIR(st.st_mode))
{
printf("\n File is a directory\n");
}
3. Explain the life cycle of signal with neat sketch. [2 M] [CO2, PO1]
A signal goes through three stages: [1M]
Generation: A signal can be generated by the kernel or any of the processes.
Delivery: A signal is said to be pending until it's delivered. Normally a signal is delivered to a
process as soon as possible by the kernel.
Processing: Every signal has an associated default action: ignore the signal; or terminate the
process, sometimes with a core dump; or stop/continue the process. For non-default
behaviour, handler function can be called.
Diagram [1M]
Part-C
Answer the following [2*3=6 Marks]
1. What different operations can be done by fcntl() system call ? Write a program to demonstrate
duplicating file descriptor using fcntl() system call. [CO1, PO8]
The fcntl() function can be used to change the properties of a file that is already open.[0.5M]
The five purposes; [0.5M]
duplicating an existing descriptor
getting or setting file descriptor flags
getting or setting file status flags
getting or setting I/O ownership
getting or setting record locks.
Program: [Total 2M]
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("example.txt", O_RDWR); [0.5M]
if (fd == -1) {
perror("Error opening file");
return 1;
}
// Duplicate file descriptor using fcntl
int newfd = fcntl(fd, F_DUPFD, 0); [1M]
if (newfd == -1) {
perror("Error duplicating file descriptor");
close(fd);
return 1;
}
printf("File descriptor %d duplicated to %d.\n", fd, newfd);
close(fd); [0.5M]
close(newfd);
return 0;
}