Create A File With Hole in It
Create A File With Hole in It
Create A File With Hole in It
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
if (fd == -1) {
return 1;
close(fd);
return 1;
if (write(fd, "", 1) != 1) {
close(fd);
return 1;
}
close(fd);
return 0;
Take multiple files as Command Line Arguments and print their inode number
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
if (argc < 2) {
return EXIT_FAILURE;
perror("stat");
}
// Print the inode number
return EXIT_SUCCESS;
Write a C program to find file properties such as inode number, number of hard link,
File permissions, File size, File access and modification time and so on of a given file
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
printf("\n");
if (argc != 2) {
return EXIT_FAILURE;
perror("stat");
return EXIT_FAILURE;
print_file_permissions(fileStat.st_mode);
return EXIT_SUCCESS;
4. Print the type of file where file name accepted through Command Line
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
void print_file_type(mode_t mode) {
if (S_ISREG(mode)) {
} else if (S_ISDIR(mode)) {
} else if (S_ISCHR(mode)) {
} else if (S_ISBLK(mode)) {
} else if (S_ISFIFO(mode)) {
} else if (S_ISLNK(mode)) {
} else if (S_ISSOCK(mode)) {
} else {
if (argc != 2) {
return EXIT_FAILURE;
}
struct stat fileStat;
perror("stat");
return EXIT_FAILURE;
print_file_type(fileStat.st_mode);
return EXIT_SUCCESS;
IMP
S_ISDIR(mode): Directory
S_ISSOCK(mode): Socket
5. Write a C program to find whether a given file is present in current directory or not.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
if (argc != 2) {
return EXIT_FAILURE;
if (stat(argv[1], &fileStat) == 0) {
} else {
perror("Error");
}
return EXIT_SUCCESS;
6. Write a C program that a string as an argument and return all the files that begins with that name
in the current directory. For example > ./a.out foo will return all file names that begins with foo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
if (argc != 2) {
return EXIT_FAILURE;
DIR *dir;
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return EXIT_FAILURE;
printf("%s\n", entry->d_name);
closedir(dir);
return EXIT_SUCCESS;
}
7. Read the current directory and display the name of the files, no of files in current directory
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *entry;
int fileCount = 0;
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return EXIT_FAILURE;
if (entry->d_type == DT_REG) {
printf("%s\n", entry->d_name);
fileCount++;
closedir(dir);
return EXIT_SUCCESS;
8. Write a C program which receives file names as command line arguments and display
#include <stdio.h>
#include <stdlib.h>
if (argc < 2) {
printf("No file names provided. Usage: %s <file1> <file2> ... <fileN>\n", argv[0]);
return EXIT_FAILURE;
printf("%s\n", argv[i]);
return EXIT_SUCCESS;
}
9. Display all the files from current directory which are created in particular month
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
DIR *dir;
struct tm *timeinfo;
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return;
if (stat(entry->d_name, &fileStat) == 0) {
timeinfo = localtime(&fileStat.st_mtime);
printf("%s\n", entry->d_name);
closedir(dir);
if (argc != 2) {
return EXIT_FAILURE;
return EXIT_FAILURE;
display_files_in_month(month);
return EXIT_SUCCESS;
10. Display all the files from current directory whose size is greater that n Bytes Where n is accept
from user.
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
DIR *dir;
if (dir == NULL) {
perror("opendir");
return;
if (stat(entry->d_name, &fileStat) == 0) {
closedir(dir);
int main() {
long size_limit;
// Prompt user for the size limit
if (scanf("%ld", &size_limit) != 1) {
return EXIT_FAILURE;
display_large_files(size_limit);
return EXIT_SUCCESS;
11
#include <stdio.h>
#include <stdlib.h>
int main() {
if (file == NULL) {
return EXIT_FAILURE;
fclose(file);
// Optionally, restore stdout to the terminal (not necessary here, but good practice)
return EXIT_SUCCESS;
}
12. Write a C program that will only list all subdirectories in alphabetical order from current directory
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
int main() {
DIR *dir;
char *subdirs[MAX_DIRS];
int count = 0;
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return EXIT_FAILURE;
}
if (entry->d_type == DT_DIR) {
if (subdirs[count] == NULL) {
perror("malloc");
closedir(dir);
return EXIT_FAILURE;
strcpy(subdirs[count], entry->d_name);
count++;
closedir(dir);
printf("%s\n", subdirs[i]);
return EXIT_SUCCESS;
IMP= MAX_DIRS is defined to limit the number of subdirectories that can be stored
14. Write a C program to Identify the type (Directory, character device, Block device, Regular file,
FIFO or pipe, symbolic link or socket) of given file using stat() system call.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
if (S_ISREG(mode)) {
printf("Regular file\n");
} else if (S_ISDIR(mode)) {
printf("Directory\n");
} else if (S_ISCHR(mode)) {
printf("Character device\n");
} else if (S_ISBLK(mode)) {
printf("Block device\n");
} else if (S_ISFIFO(mode)) {
printf("FIFO or pipe\n");
} else if (S_ISLNK(mode)) {
printf("Symbolic link\n");
} else if (S_ISSOCK(mode)) {
printf("Socket\n");
} else {
if (argc != 2) {
return EXIT_FAILURE;
perror("stat");
return EXIT_FAILURE;
}
// Print file type
print_file_type(fileStat.st_mode);
return EXIT_SUCCESS;
IMP stat() system call, which retrieves information about the file
15. Generate parent process to write unnamed pipe and will read from it
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main() {
int pipefd[2]; // Array to hold the read and write file descriptors
pid_t pid;
char buffer[BUFFER_SIZE];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
return EXIT_SUCCESS;
IMP
} An unnamed pipe is created using the pipe() system call, which populates the pipefd array
with two file descriptors: pipefd[0] for reading and pipefd[1] for writing.
#include <stdio.h>
#include <stdlib.h>
void cleanup1() {
void cleanup2() {
void cleanup3() {
int main() {
atexit(cleanup1);
atexit(cleanup2);
atexit(cleanup3);
IMP
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
jmp_buf buffer;
// Global variable
int global_var = 0;
// Function to demonstrate variable behaviors
void test_variables() {
// Automatic variable
int auto_var = 0;
// Register variable
// Static variable
// Volatile variable
// Incrementing variables
global_var++;
auto_var++;
reg_var++;
static_var++;
volatile_var++;
printf("In test_variables():\n");
longjmp(buffer, 1);
int main() {
if (setjmp(buffer) != 0) {
test_variables();
IMP auto_var is defined within the function and is automatically allocated on the stack
reg_var is also defined within the function and is suggested to be stored in a register for faster access
static_var is defined as static, meaning it retains its value between function calls.
19.Implement the following unix/linux command (use fork, pipe and exec system call) ls –l | wc –l
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int pipefd[2]; // Array to hold the read and write file descriptors
// Create a pipe
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
pid1 = fork();
if (pid1 == -1) {
perror("fork");
exit(EXIT_FAILURE);
close(pipefd[0]);
// Redirect stdout to the write end of the pipe
dup2(pipefd[1], STDOUT_FILENO);
// If execlp fails
perror("execlp");
exit(EXIT_FAILURE);
pid2 = fork();
if (pid2 == -1) {
perror("fork");
exit(EXIT_FAILURE);
close(pipefd[1]);
dup2(pipefd[0], STDIN_FILENO);
// If execlp fails
perror("execlp");
exit(EXIT_FAILURE);
close(pipefd[0]);
close(pipefd[1]);
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <time.h>
int main() {
int n, i;
scanf("%d", &n);
pid_t pids[n];
pids[i] = fork();
if (pids[i] < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pids[i] == 0) {
// Child process
times(&end_times[i]);
clock_t total_user_time = 0;
clock_t total_system_time = 0;
printf("Total cumulative time spent in user mode: %ld clock ticks\n", total_user_time);
printf("Total cumulative time spent in kernel mode: %ld clock ticks\n", total_system_time);
return 0;
IMP parent process waits for all child processes to terminate using waitpid()
The user time (tms_utime) and system time (tms_stime) are printed
22.Write a C program to get and set the resource limits such as files, memory associated with a
process
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <unistd.h>
printf("%s:\n", description);
int main() {
if (getrlimit(RLIMIT_NOFILE, &limit) == 0) {
} else {
perror("getrlimit");
exit(EXIT_FAILURE);
if (setrlimit(RLIMIT_NOFILE, &limit) == 0) {
} else {
perror("setrlimit");
exit(EXIT_FAILURE);
if (getrlimit(RLIMIT_NOFILE, &limit) == 0) {
} else {
perror("getrlimit");
exit(EXIT_FAILURE);
if (getrlimit(RLIMIT_AS, &limit) == 0) {
} else {
perror("getrlimit");
exit(EXIT_FAILURE);
}
// Set new limits for the maximum virtual memory size
if (setrlimit(RLIMIT_AS, &limit) == 0) {
} else {
perror("setrlimit");
exit(EXIT_FAILURE);
// Get and print the updated limits for virtual memory size
if (getrlimit(RLIMIT_AS, &limit) == 0) {
} else {
perror("getrlimit");
exit(EXIT_FAILURE);
return 0;
IMP print_limits is defined to print the soft and hard limits of the specified resource.
23.Write a program that illustrates how to execute two commands concurrently with a pipe.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int pipefd[2]; // Array to hold the read and write file descriptors
// Create a pipe
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
pid1 = fork();
if (pid1 == -1) {
perror("fork");
exit(EXIT_FAILURE);
dup2(pipefd[1], STDOUT_FILENO);
// Execute 'ls'
// If execlp fails
perror("execlp");
exit(EXIT_FAILURE);
pid2 = fork();
if (pid2 == -1) {
perror("fork");
exit(EXIT_FAILURE);
close(pipefd[1]);
dup2(pipefd[0], STDIN_FILENO);
close(pipefd[0]); // Close the original read end
// If execlp fails
perror("execlp");
exit(EXIT_FAILURE);
close(pipefd[0]);
close(pipefd[1]);
return 0;
}
24.Write a C program that print the exit status of a terminated child process
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
pid = fork();
if (pid < 0) {
// Fork failed
perror("fork");
exit(EXIT_FAILURE);
if (pid == 0) {
// Child process
} else {
// Parent process
if (WIFEXITED(status)) {
} else {
return 0;
}
25.Write a C program that catches the ctrl-c (SIGINT) signal for the first time and display the
appropriate message and exits on pressing ctrl-c again.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
flag = 1;
int main() {
signal(SIGINT, sigint_handler);
while (1){
// Keep the main process running in an infinite loop
return 0;
1.
flag variable is a global variable that tracks whether SIGINT has been received for the
first time.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
int main() {
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
signal(SIGHUP, sighup_handler);
signal(SIGINT, sigint_handler);
signal(SIGQUIT, sigquit_handler);
while (1) {
if (i % 2 == 0) {
} else {
wait(NULL);
return 0;
IMP
27.Write a C program to send SIGALRM signal by child process to parent process and parent
process make a provision to catch the signal and display alarm is fired.(Use Kill, fork, signal and
sleep system call)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
sleep(5);
// Send SIGALRM signal to the parent process
kill(getppid(), SIGALRM);
signal(SIGALRM, sigalrm_handler);
wait(NULL);
return 0;
}
28. Write a C program that illustrates suspending and resuming processes using signals.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <wait.h>
raise(SIGSTOP);
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
signal(SIGUSR1, suspend_handler);
signal(SIGUSR2, resume_handler);
while (1) {
kill(pid, SIGUSR1);
kill(pid, SIGUSR2);
kill(pid, SIGTERM);
return 0;
IMP SIGSTOP signal to suspend a process and the SIGCONT signal to resume it
SIGUSR2 signal, which is used to indicate that the child should resume its execution.
29.Write a C program which create a child process which catch a signal sighup, sigint and sigquit.
The Parent process send a sighup or sigint signal after every 3 seconds, at the end of 30 second
parent send sigquit signal to child and child terminates my displaying message “My DADDY has
Killed me!!!”.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
int main() {
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
signal(SIGHUP, sighup_handler);
signal(SIGINT, sigint_handler);
signal(SIGQUIT, sigquit_handler);
while (1) {
if (i % 2 == 0) {
} else {
wait(NULL);
return 0;
IMP
sigquit_handler: Handles SIGQUIT, prints the termination message, and exits the child
process.
30. Write a C program to implement the following unix/linux command (use fork, pipe
and exec system call). Your program should block the signal Ctrl-C and Ctrl-\ signal
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
void block_signals() {
sigset_t set;
sigemptyset(&set);
void unblock_signals() {
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigaddset(&set, SIGQUIT);
}
int main() {
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
// Block signals
block_signals();
pid1 = fork();
if (pid1 < 0) {
perror("fork");
exit(EXIT_FAILURE);
dup2(pipefd[1], STDOUT_FILENO);
exit(EXIT_FAILURE);
pid2 = fork();
if (pid2 < 0) {
perror("fork");
exit(EXIT_FAILURE);
dup2(pipefd[0], STDIN_FILENO);
exit(EXIT_FAILURE);
// Parent process
close(pipefd[0]); // Close read end of the pipe
wait(NULL);
wait(NULL);
// Unblock signals
unblock_signals();
return 0;