0% found this document useful (0 votes)
15 views38 pages

Aos Slips

Uploaded by

Snehal Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views38 pages

Aos Slips

Uploaded by

Snehal Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Q.1 Create a file with hole in it.

Code:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd;
char buffer[10]="Hello";
fd=open("hole_file.txt",O_RDWR|O_CREAT,0644);
if(fd==-1){
perror("open");
return 1;
}
write(fd,buffer,5);
lseek(fd,1000,SEEK_CUR);
write(fd,buffer,5);
close(fd);
return 0;
}
Output:

Teacher’s Signature
Q.2 Take multiple files as Command Line Arguments and print their inode number.
Flowchart:

Code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void print_inode(const char *filename) {
struct stat file_stat;
if (stat(filename, &file_stat) == 0) {
printf("Inode number of %s: %lu\n", filename, file_stat.st_ino);
} else {
perror(filename);
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <file1> [file2] ...\n", argv[0]);
return 1;
}
for (int i = 1; i < argc; i++) {
print_inode(argv[i]);
}
return 0;
}
Output:

Teacher’s Signature
Q.3 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 using stat()
system call
Code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
void print_file_properties(const char *filename) {
struct stat file_stat;
if (stat(filename, &file_stat) == -1) {
perror("stat");
return;
}
printf("File: %s\n", filename);
printf("Inode Number: %lu\n", (unsigned long) file_stat.st_ino);
printf("Number of Hard Links: %lu\n", (unsigned long) file_stat.st_nlink);

printf("File Permissions: ");


printf((file_stat.st_mode & S_IRUSR) ? "r" : "-");
printf((file_stat.st_mode & S_IWUSR) ? "w" : "-");
printf((file_stat.st_mode & S_IXUSR) ? "x" : "-");
printf("\n");
printf("File Size (bytes): %lu\n", (unsigned long) file_stat.st_size);
printf("File Size (KB): %.2f\n", (double)file_stat.st_size / 1024);

printf("Owner UID: %d\n", file_stat.st_uid);


printf("Group GID: %d\n", file_stat.st_gid);
printf("Last Access Time: %s", ctime(&file_stat.st_atime));
printf("Last Modification Time: %s", ctime(&file_stat.st_mtime));
printf("Last Status Change Time: %s", ctime(&file_stat.st_ctime));
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
print_file_properties(argv[1]);
return 0;
}

Output:

Teacher’s Signature
Q.4 Print the type of file where file name accepted through Command Line
Flowchart:

Code:
#include <stdio.h>
#include <sys/stat.h>
void print_file_type(const char *filename) {
struct stat file_stat;
if (stat(filename, &file_stat) == -1) {
perror("stat");
return;
}
printf("File: %s\n", filename);
switch (file_stat.st_mode & S_IFMT) {
case S_IFREG:
printf("File type: Regular file\n");
break;
case S_IFDIR:
printf("File type: Directory\n");
break;
case S_IFIFO:
printf("File type: FIFO (Named pipe)\n");
break;
case S_IFCHR:
printf("File type: Character device\n");
break;
case S_IFBLK:
printf("File type: Block device\n");
break;
default:
printf("File type: Unknown\n");
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
print_file_type(argv[1]);
return 0;
}
Output:

Teacher’s Signature
Q.5 Read the current directory and display the name of the files, no of files in current
directory.
Flowchart:

Code:
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
int main(){
DIR *dir;
struct dirent *ent;
int file_count=0;
dir=opendir(".");
if(dir==NULL){
perror("opendir");
exit(EXIT_FAILURE);
}
while((ent=readdir(dir))!=NULL){
if(ent->d_type==DT_REG){
printf("File %d:%s\n",++file_count,ent->d_name);
}
}
printf("\nTotal files:%d\n",file_count);
closedir(dir);
return 0;
}
Output:

Teacher’s Signature
Q.6 Write a C program which receives file names as command line arguments and display
those filenames in ascending order according to their sizes. I) (e.g. $ a.out a.txt b.txt c.txt,
…)
Flowchart:

Code:
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include<string.h>
typedef struct {
char name[256];
off_t size;
} FileInfo;
int compare(const void *a, const void *b) {
FileInfo *f1 = (FileInfo *)a;
FileInfo *f2 = (FileInfo *)b;
return (f1->size - f2->size);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename1> <filename2> ...\n", argv[0]);
return 1;
}
FileInfo *files = (FileInfo *)malloc((argc - 1) * sizeof(FileInfo));
for (int i = 1; i < argc; i++) {
struct stat file_stat;
if (stat(argv[i], &file_stat) == -1) {
perror(argv[i]);
continue;
}
strcpy(files[i - 1].name, argv[i]);
files[i - 1].size = file_stat.st_size;
}
qsort(files, argc - 1, sizeof(FileInfo), compare);
printf("Files in ascending order of size:\n");
for (int i = 0; i < argc - 1; i++) {
printf("%s: %lu bytes\n", files[i].name, (unsigned long)files[i].size);
}
free(files);
return 0;
}
Output:

Teacher’s Signature
Q.7 Display all the files from current directory which are created in particular month.
Flowchart:

Code:
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<time.h>
void ass9(int month){
DIR *dir;
struct dirent *ent;
struct stat file_stat;
struct tm *time_info;
dir=opendir(".");
if(dir==NULL){
perror("opendir");
return;
}
printf("Files created in month %d:\n",month);
while((ent=readdir(dir))!=NULL){
if(ent->d_type==DT_REG){
if(stat(ent->d_name,&file_stat)==-1){
perror("stat");
continue;
}
time_info=localtime(&file_stat.st_ctime);
if(time_info->tm_mon==month-1){
printf("%s\n",ent->d_name);
}
}
}
closedir(dir);
}
int main(int argc,char *argv[]){
if(argc!=2){
printf("Usage:%s<month>\n",argv[0]);
printf("Month should be in range 1-12.\n");
return 1;
}
int month=atoi(argv[1]);
if(month<1||month>12){
printf("Invalid month.Month should be in range 1-12.\n");
return 1;
}
ass9(month);
return 0;
}
Output:

Teacher’s Signature
Q.8 Write a C program that will only list all subdirectories in alphabetical order from current
directory.
Code:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
int main() {
DIR *dir;
struct dirent *ent;
char **subdirs = NULL;
int subdir_count = 0;
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
exit(1);
}
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_DIR && strcmp(ent->d_name, ".") != 0 && strcmp(ent-
>d_name, "..") != 0) {
subdirs = realloc(subdirs, (subdir_count + 1) * sizeof(char *));
subdirs[subdir_count] = strdup(ent->d_name);
subdir_count++;
}
}
closedir(dir);
qsort(subdirs, subdir_count, sizeof(char *), compare);
printf("Subdirectories in alphabetical order:\n");
for (int i = 0; i < subdir_count; i++) {
printf("%s\n", subdirs[i]);
free(subdirs[i]);
}
free(subdirs);
return 0;
}
Output:

Teacher’s Signature
Q.9 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.
Code:
#include <stdio.h>
#include <sys/stat.h>
void print_file_type(const char *filename) {
struct stat file_stat;
if (stat(filename, &file_stat) == -1) {
perror("stat");
return;
}
printf("File: %s\n", filename);
switch (file_stat.st_mode & S_IFMT) {
case S_IFREG:
printf("File type: Regular file\n");
break;
case S_IFDIR:
printf("File type: Directory\n");
break;
case S_IFIFO:
printf("File type: FIFO (Named pipe)\n");
break;
case S_IFCHR:
printf("File type: Character device\n");
break;
case S_IFBLK:
printf("File type: Block device\n");
break;
default:
printf("File type: Unknown\n");
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
print_file_type(argv[1]);
return 0;
}
Output:

Teacher’s Signature
Q.10 Demonstrate the use of atexit () function.
Code:
#include<stdio.h>
#include<stdlib.h>
void cleanup(){
printf(“Cleaning…\n”);
}
void closing_message(){
printf(“Program terminated”);
}
int main(){
atexit(cleanup);
atexit(closing_message);
printf(“Program started.\n”);
for(int i=0;i<5;i++){
printf(“%d\n”,i);
}
return 0;
}
Output:

Teacher’s Signature
Q.11 Write a C program to demonstrates the different behaviour that can be seen with
automatic, global, register, static and volatile variables (Use setjmp() and longjmp() system
call)
Code:
#include<stdio.h>
#include<stdlib.h>
#include<setjmp.h>
jmp_buf buffer;
int global_var=10;
static int static_var=20;
volatile int volatile_var=30;
int auto_var=40;
int register_var=50;
void display_values(){
printf("Automatic var:%d\n",auto_var);
printf("Global var:%d\n",global_var);
printf("Register var:%d\n",register_var);
printf("Static var:%d\n",static_var);
printf("volatile var:%d\n",volatile_var);
}
int main(){
int auto_var=1;
register int register_var=2;
printf("Before setjmp:\n");
display_values();
if(setjmp(buffer)==0){
auto_var=11;
global_var=21;
register_var=22;
static_var=31;
volatile_var=41;
printf("After modifying variables:\n");
display_values();
longjmp(buffer,1);
}
else{
printf("After longjmp:\n");
display_values();
}
return 0;
}
Output:

Teacher’s Signature
Q.12 Write a C program to create „n‟ child processes. When all „n‟ child processes
terminates, Display total cumulative time children spent in user and kernel mode.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<windows.h>
DWORD WINAPI childProcess(LPVOID id){
int thread_id=(int)id;
for(int j=0;j<100000000;j++);
printf("Child process %d terminated.\n",thread_id);
return 0;
}
int main(){
int n;
printf("Enter number of child processes:");
scanf("%d",&n);
for(int i=0;i<n;i++){
int*thread_id=malloc(sizeof(int));
*thread_id=i;
HANDLE thread=CreateThread(NULL,0,childProcess,thread_id,0,NULL);
CloseHandle(thread);
}
Sleep(n*1000);
printf("All child processes terminated.\n");
return 0;
}
Output:

Teacher’s Signature
Q.13 Write a program that illustrates how to execute two commands concurrently with a pipe.
Code:
#include<stdio.h>
#include<stdlib.h>
int main(){
system("echo @echo off>run_commands.bat");
system("echo dir>output.txt>>run_commands.bat");
system("echo timeout /t 1>>run_commands.bat");
system("echo finstr keyword output.txt>>run_commands.bat");
system("run_commands.bat");
return 0;
}
Output:

Teacher’s Signature
Q.14 Write a C program that print the exit status of a terminated child process.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
volatile sig_atomic_t suspended=0;
void sigpipe_handler(int sig){
suspended=1;
printf("Process suspended.Press Ctrl+C to terminate or send SIGALRM to resume\n");
}
void sigint_handler(int sig){
printf("Process terminated.\n");
exit(0);
}
void sigalrm_handler(int sig){
suspended=0;
printf("Process resumed.\n");
}
int main(){
signal(SIGINT,sigint_handler);
while(1){
if(!suspended){
printf("Running...\n");}
sleep(1);
}
return 0;
}
Output:

Teacher’s Signature
Q.15 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.
Code:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
void sigfun(int sig)
{
printf("You have pressed Ctrl-C,please press again to exit");
(void)signal(SIGINT,SIG_DFL);
}
int main()
{
(void)signal(SIGINT,sigfun);
while(1){
printf("Hello World!");
sleep(1);
}
return(0);
}
Output:

Teacher’s Signature
Q.16 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.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
void signal_handler(int sig){
printf("Signal received\n");
}
int main(){
signal(SIGTERM,signal_handler);
printf("Simulating signals sending in 5 seconds...\n");
sleep(5);
raise(SIGTERM);
printf("Signal sent to parent\n");
while(1){
sleep(1);
}
return 0;
}
Output:

Teacher’s Signature
Q.17 Handle the two-way communication between parent and child processes using pipe
Code:
parent.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define BUFFER_SIZE 128
int main(){
HANDLE pipe_read1,pipe_write1;
HANDLE pipe_read2,pipe_write2;
SECURITY_ATTRIBUTES sa={sizeof(SECURITY_ATTRIBUTES),NULL,TRUE);
if(!CreatePipe(&pipe_read1,&pipe_write1,&sa,0)){
fprintf(stderr,”Failed to create pipe1.\n”);
return 1;
}
if(!CreatePipe(&pipe_read2,&pipe_write2,&sa,0)){
fprintf(stderr,”Failed to create pipe2.\n”);
return 1;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
si.hStdInput=pipe_read1;
si.hStdOutput=pipe_write2;
si.dwFlags=STARTF_USESTDHANDLES;
ZeroMemory(&pi,sizeof(pi));
if(!createProcess(NULL,”child.exe”,NULL,NULL,TRUE,0,NULL,&pi,&si)){
fprintf(stderr,”Failed to create child process.\n”);
return 1;
}
char parent_msg[]=”Hello from parent!”;
char child_msg[BUFFER_SIZE];
DWORD written;
WriteFile(pipe_write1,parent_msg,strlen(parent_msg)+1,&written,NULL);
DWORD read;
ReadFile(pipe_read2,child_msg,BUFFER_SIZE,&read,NULL);
printf(“Parent received:%s\n”,child_msg);
CloseHandle(pipe_read1);
CloseHandle(pipe_write1);
CloseHandle(pipe_read2);
CloseHandle(pipe_write2);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
child.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define BUFFER_SIZE 128
int main(){
char buffer[BUFFER_SIZE];
DWORD read;
ReadFile(GetStdHandle(STD_INPUT_HANDLE),buffer,BUFFER_SIZE,&read,NULL);
printf(“Child received:%s\n”,buffer);
char child_msg[]=”Hello from child!”;
DWORD written;
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),child_msg,strlen(child_msg)+1,&writte
n,NULL);
return 0;
}
Output:

Teacher’s Signature
Q.18 Write a C program to find whether a given file is present in current directory or not.
Code:
#include<stdio.h>
#include<dirent.h>
#include<string.h>
int main(int argc,char *argv[]){
DIR *dir;
struct dirent *entry;
int found=0;
if(argc !=2){
printf(“Usage:%s<file_name>\n”,argv[0]);
return 1;
}
dir=opendir(“.”);
if(dir==NULL){
perror(“opendir”);
return 1;
}
while((entry=readdir(dir))!=NULL){
if(strcmp(entry->d_name,argv[1]==0){
found=1;
break;
}
}
closedir(dir);
if(found)
printf(“File:%s is present in the current directory.\n”,argv[1]);
else
printf(“File:%s is not found in the directory.\n”,argv[1]);
return 0;
}
Output:

Teacher’s Signature
Q.19 Display all the files from current directory whose size is greater that n Bytes Where n is
accept from user.
Code:
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
int main(){
long n;
printf(“Enter the file size(in bytes):”);
scanf(“%id”,&n);
DIR *dir=opendir(“.”);
if(dir==NULL){
perror(“opendir(“.”);
return 1;
}
struct dirent *entry;
struct stat fileStat;
while ((entry->d_name,&fileStat)==0){
if(fileStat.st_size>n){
printf(“%s:%id bytes\n”,entry->d_name,fileStat.st_size);
}
}
}
closedir(dir);
return 0;
}
Output:

Teacher’s Signature
Q.20 Write a C Program that demonstrates redirection of standard output to a file.
Code:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main(){
int fd=open(“output.txt”,O_WRONGLY|O_CREAT|O_TRUNC,0644);
if(fd<0){
perror(“open”);
return 1;
}
dup2(fd,STDOUT_FILENO);
printf(“This will be written to output.txt]n”);
close(fd);
return 0;
}
Output:

Teacher’s Signature

You might also like