0% found this document useful (0 votes)
67 views47 pages

Aos Final

Uploaded by

bhosaletejas43
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)
67 views47 pages

Aos Final

Uploaded by

bhosaletejas43
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/ 47

==========================================================================

Write a C program to find whether a given file is present in current directory or not.
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
char fn[50];
int fd;
printf("\nEnter file name=>");
gets(fn);
fd=open(fn,O_RDONLY);
if(fd<0)
printf("\nfile not exist");
else
{
printf("\nFile exist");
close(fd);
}
return 0;
}
==========================================================================
Write a C program which blocks SIGOUIT signal for 5 seconds. After 5 second process checks any
occurrence of quit signal during this period, if so, it unblock the signal. Now another occurrence of quit
signal terminates the program. (Use sigprocmask() and sigpending() )
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>

void sh(int n){


if(n==SIGQUIT){
printf("\nCtrl+/ is pressed");

}
fflush(stdout);
}

int main(){
sigset_t res, old, pen;
signal(SIGQUIT, sh);
sigemptyset(&res);
sigaddset(&res,SIGQUIT);
sigprocmask(SIG_BLOCK, &res,&old);
sleep(5);
sigpending(&pen);
if(__sigismember(&pen,SIGQUIT)){
printf("SIGQUIT is pending");
}
sigprocmask(SIG_SETMASK, &old,NULL);
printf("SIGQUIT is unblock");
fflush(stdout);
sleep(15);
return 0;
}
==========================================================================
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<fcntl.h>
#include <sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
DIR *d;
struct dirent *dr;
char s[50],s1[50];
if(argc<2)
{
printf("insufficient argument enterb string on command line");
exit(0);
}
d=opendir(".");
while((dr=readdir(d))!=NULL)
{
strncpy(s,dr->d_name,strlen(argv[1]));
if(strcmp(s,argv[1])==0)
printf("%s\n",dr->d_name);
}
return 0;
}
==========================================================================
Write a C program to demonstrates the different behavior that can be seen with automatic, global,
register, static and volatile variables (Use setjmp() and longjmp() system call).
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<setjmp.h>
static void f1(int,int,int,int);
static void f2();
static int gint;
static jmp_buf jmp;
int main()
{

int aint;
register int rint;
volatile int vint;
static int sint;
gint=1; aint=2; rint=3; vint=4; sint=5;
if(setjmp(jmp)!=0)
{
printf("\nAfter Long jump=>");
printf("\ngint=%d\taint=%d\trint=%d\tvint=%d\tsint=%d",gint,aint,rint,vint,sint);
exit(0);
}
printf("\n updating values");
gint=91; aint=92; rint=93; vint=94; sint=95;
f1(aint,rint,vint,sint);
return 0;
}
static void f1(int a,int b,int c,int d)
{
printf("\n in f1=>");
printf("\ngint=%d\taint=%d\trint=%d\tvint=%d\tsint=%d",gint,a,b,c,d);
f2();
}
static void f2()
{
longjmp(jmp,1);
}
==========================================================================
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.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
int main(int args, char *argv[])
{
struct stat s;
struct passwd *ps;
struct group *gs;
char t[50];

if(args<2){
printf("No file name found");
exit(0);
}

stat(argv[1], &s);
printf("\n");

if(S_ISREG(s.st_mode)){
printf("-");
}
if(S_ISDIR(s.st_mode)){
printf("d");
}
if(S_ISCHR(s.st_mode)){
printf("c");
}
if(S_ISFIFO(s.st_mode)){
printf("f");
}
if(S_ISBLK(s.st_mode)){
printf("b");
}
if(S_ISLNK(s.st_mode)){
printf("l");
}
if(S_ISSOCK(s.st_mode)){
printf("s");
}

printf((s.st_mode & S_IRUSR)?"r":"-");


printf((s.st_mode & S_IWUSR)?"w":"-");
printf((s.st_mode & S_IXUSR)?"x":"-");
printf((s.st_mode & S_IRGRP)?"r":"-");
printf((s.st_mode & S_IWGRP)?"w":"-");
printf((s.st_mode & S_IXGRP)?"x":"-");
printf((s.st_mode & S_IROTH)?"r":"-");
printf((s.st_mode & S_IWOTH)?"w":"-");
printf((s.st_mode & S_IXOTH)?"x":"-");

printf(" %d",s.st_nlink);
ps = getpwuid(s.st_uid);
printf(" %s",ps->pw_name);
gs = getgrgid(s.st_gid);
printf(" %s",gs->gr_name);

printf(" %d",s.st_size);
strcpy(t,ctime(&s.st_atim));
printf(" %s",t);
printf(" %s",argv[1]);

return 0;
}
==========================================================================
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.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/times.h>
#include<time.h>

int main(){
int c,n,s;
int i,j;
struct tms t2;
printf("\n Enter no of child");
scanf("%d",&n);
while(n-->0){
c =fork();
if(c==0){
printf("\nchildid=%d",getpid());
for(i=0,j=0;i<100000000;i++){
j=j+1;
}
exit(0);

}
else{
wait(&s);

}
}
times(&t2);
double d= sysconf(_SC_CLK_TCK);
double ct= t2.tms_utime/d;
double st= t2.tms_stime/d;
double cct= t2.tms_cutime/d;
double cst= t2.tms_cstime/d;

printf("\nTime ct:%lf ",ct);


printf("\nTime st:%lf ",st);
printf("\nTime cct:%lf ",cct);
printf("\nTime cst:%lf ",cst);
return 0;
}
==========================================================================
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 fstat() system call.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
int main(int args, char *argv[])
{
struct stat s;
struct passwd *ps;
struct group *gs;
char t[50];
int fd;

if(args<2){
printf("No file name found");
exit(0);
}

fd=open(argv[1],O_RDONLY);
fstat(fd, &s);
printf("\n");

if(S_ISREG(s.st_mode)){
printf("-");
}
if(S_ISDIR(s.st_mode)){
printf("d");
}
if(S_ISCHR(s.st_mode)){
printf("c");
}
if(S_ISFIFO(s.st_mode)){
printf("f");
}
if(S_ISBLK(s.st_mode)){
printf("b");
}
if(S_ISLNK(s.st_mode)){
printf("l");
}
if(S_ISSOCK(s.st_mode)){
printf("s");
}

printf((s.st_mode & S_IRUSR)?"r":"-");


printf((s.st_mode & S_IWUSR)?"w":"-");
printf((s.st_mode & S_IXUSR)?"x":"-");
printf((s.st_mode & S_IRGRP)?"r":"-");
printf((s.st_mode & S_IWGRP)?"w":"-");
printf((s.st_mode & S_IXGRP)?"x":"-");
printf((s.st_mode & S_IROTH)?"r":"-");
printf((s.st_mode & S_IWOTH)?"w":"-");
printf((s.st_mode & S_IXOTH)?"x":"-");

printf(" %d",s.st_nlink);
ps = getpwuid(s.st_uid);
printf(" %s",ps->pw_name);
gs = getgrgid(s.st_gid);
printf(" %s",gs->gr_name);

printf(" %d",s.st_size);
strcpy(t,ctime(&s.st_atim));
printf(" %s",t);
printf(" %s",argv[1]);

return 0;
}
==========================================================================
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 during the execution. ls –l | wc–l
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<signal.h>
#include<unistd.h>
void sh(int);
int main()
{
int fd[2],n;
pipe(fd);
signal(SIGQUIT,sh);
signal(SIGINT,sh);
n=fork();
if(n==0)
{
close (fd[0]);
close(1);
dup(fd[1]);
close(fd[1]);
execlp("ls","ls","-l",NULL);
}
else
{
wait();
sleep(20);
close (fd[1]);
close(0);
dup(fd[0]);
close(fd[0]);
execlp("wc","wc","-l",NULL);
}
return 0;
}
void sh(int n)
{
if(n==SIGQUIT)
printf("\n ctl+\\ cought");
if(n==SIGINT)
printf("\n ctl+c cought");
fflush(stdout);
}
==========================================================================
Write a C program to create an unnamed pipe. The child process will write following three messages to
pipe and parent process display it.
Message1 = “Hello World”
Message2 = “Hello SPPU”
Message3 = “Linux is Funny”
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
int p[2],i,n;
char buf[70];
char *m1="Message1 = Hello World";
char *m2="Message2 = Hello SPPU";
char *m3="Message3 = LINUX is Funny";
if(pipe(p)<0)
exit(0);
n=fork();
if(n==0)
{
//printf("CHILD WRITTEN:\n");
write(p[1],m1,70);
write(p[1],m2,70);
write(p[1],m3,70);
}
else{
printf("PARENT READS:\n");
for(i=0;i<3;i++)
{
read(p[0],buf,70);
printf("%s\n",buf);
}
}
return 0;
}
==========================================================================
Write a C program to read all txt files (that is files that ends with .txt) in the current directory and merge
them all to one txt file and returns a file descriptor for the new file.
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<time.h>
#include<pwd.h>
#include<string.h>
#include<grp.h>
#include<dirent.h>
void main()
{
int fd1,fd2;
char b[100],d[100];
int n;
char str[100];
char *str1,*str2;
struct dirent *dir;
getcwd(b,100);
fd1=open("output.txt",O_RDWR,O_CREAT|O_TRUNC,S_IRWXU);
DIR *dr = opendir(b);
if (dr == NULL)
{
printf("Could not open current directory" );
return 0;
}
while ((dir = readdir(dr)))
{
//printf("\nfilename:%s",dir->d_name);
strcpy(str,dir->d_name);
str1=strtok(str,".");
str2=strtok(NULL,".");
if(str2!=NULL)
{
n=strcmp(str2,"txt");
if(n==0)
{
printf("\ntxt file :%s",dir->d_name);
fd2=open(dir->d_name,O_RDONLY);
read(fd2,d,100);
write(fd1,d,strlen(d));
close(fd2);
}
}
}
close(fd1);
return 0;
}
==========================================================================
Write a C program to map a given file in memory and display the contain of mapped file in reverse.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<fcntl.h>
int main(int argc,char*argv[])
{
int fd;
int size;
char *f,ch;
int i;
struct stat s;
fd=open(argv[1],O_RDONLY);
fstat(fd,&s);
size=s.st_size;
f=mmap(0,size,PROT_READ,MAP_PRIVATE,fd,0);
for(i=size-1;i>=0;i--)
{
ch=f[i];
putchar(ch);
}
close(fd);
return 0;
}
==========================================================================
Write a C program that behaves like a shell (command interpreter). It has its own prompt say
“NewShell$”. Any normal shell command is executed from your shell by starting a child process to
execute the system program corresponding to the command. It should additionally interpret the
following command.
i) list f<dirname> - print name of all files in directory
ii) list n <dirname> - print number of all entries
iii) list i<dirname> - print name and inode of all files

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<dirent.h>
void list(char ch,char *di);
int main()
{
char comm[80],t1[20],t2[20],t3[20],t4[20];
int n;
system("clear");
while(1)
{
printf("MyShell$");
fflush(stdin);
fgets(comm,80,stdin);
n=sscanf(comm,"%s%s%s%s",t1,t2,t3,t4);
switch(n)
{
case 1:
if(!fork())
{
execlp(t1,t1,NULL);
perror(t1);
}
break;
case 2:
if(!fork())
{
execlp(t1,t1,t2,NULL);
perror(t1);
}
break;
case 3:
if(strcmp(t1,"list")==0)
list(t2[0],t3);
if(!fork())
{
execlp(t1,t1,t2,t3,NULL);
perror(t1);
}
break;
case 4:
if(!fork())
{
execlp(t1,t1,t2,t3,t4,NULL);
perror(t1);
}
break;
}
}
return 0;
}
void list(char ch,char*di)
{
DIR *d;
struct dirent *dr;
struct stat s;
char temp[50];
int cnt=0;
d=opendir(di);
if(d==NULL)
{
printf("unable to open dir");
exit(0);
}
switch(ch)
{
case 'f':
while((dr=readdir(d))!=NULL)
printf("%s\n",dr->d_name);
break;
case 'n':
while((dr=readdir(d))!=NULL)
cnt++;
printf("\nNumber of entries=>%d",cnt);
break;
case 'i':
while((dr=readdir(d))!=NULL)
{
getcwd(temp,50);
strcat(temp,"/");
strcat(temp,di);
strcat(temp,"/");
strcat(temp,dr->d_name);
printf("%s\n",temp);
stat(temp,&s);
printf("%s\t%d\n",dr->d_name,s.st_ino);
}
break;
}
}
==========================================================================
Write a C program to create a file with hole in it.
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int fd;
char buf1[]="bhushan";
char buf2[]="shah";
fd=open("b1.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
write(fd,buf1,7);
lseek(fd,100,SEEK_CUR);
write(fd,buf2,4);
close(fd);
return 0;
}
==========================================================================
Write a C program which display the information of a given file similar to given by the unix /linux
command on current directory (l.e File Access permission, file name, file type, User id, group id, file
size, file access and modified time and so on)
ls –l <filename>
DO NOT simply exec ls -l <filename> or system command from the program
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
int main(int args, char *argv[])
{
struct stat s;
struct passwd *ps;
struct group *gs;
char t[50];

if(args<2){
printf("No file name found");
exit(0);
}

stat(argv[1], &s);
printf("\n");

if(S_ISREG(s.st_mode)){
printf("-");
}
if(S_ISDIR(s.st_mode)){
printf("d");
}
if(S_ISCHR(s.st_mode)){
printf("c");
}
if(S_ISFIFO(s.st_mode)){
printf("f");
}
if(S_ISBLK(s.st_mode)){
printf("b");
}
if(S_ISLNK(s.st_mode)){
printf("l");
}
if(S_ISSOCK(s.st_mode)){
printf("s");
}

printf((s.st_mode & S_IRUSR)?"r":"-");


printf((s.st_mode & S_IWUSR)?"w":"-");
printf((s.st_mode & S_IXUSR)?"x":"-");
printf((s.st_mode & S_IRGRP)?"r":"-");
printf((s.st_mode & S_IWGRP)?"w":"-");
printf((s.st_mode & S_IXGRP)?"x":"-");
printf((s.st_mode & S_IROTH)?"r":"-");
printf((s.st_mode & S_IWOTH)?"w":"-");
printf((s.st_mode & S_IXOTH)?"x":"-");

printf(" %d",s.st_nlink);
ps = getpwuid(s.st_uid);
printf(" %s",ps->pw_name);
gs = getgrgid(s.st_gid);
printf(" %s",gs->gr_name);

printf(" %d",s.st_size);
strcpy(t,ctime(&s.st_atim));
printf(" %s",t);
printf(" %s",argv[1]);

return 0;
}
==========================================================================
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<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/resource.h>
int main()
{
struct rlimit a,b,c;
getrlimit(RLIMIT_DATA,&a);
printf("\nrlim_cur=%d",a.rlim_cur);
printf("\nrlim_max=%d",a.rlim_max);
b.rlim_cur=10;
b.rlim_max=1000;
setrlimit(RLIMIT_DATA,&b);
getrlimit(RLIMIT_DATA,&c);
printf("\nrlim_cur=%d",c.rlim_cur);
printf("\nrlim_max=%d",c.rlim_max);
printf("\nfilesize=>");
getrlimit(RLIMIT_STACK,&a);
printf("\nrlim_cur=%d",a.rlim_cur);
printf("\nrlim_max=%d",a.rlim_max);
b.rlim_cur=20;
b.rlim_max=2000;
setrlimit(RLIMIT_STACK,&b);
getrlimit(RLIMIT_STACK,&c);
printf("\nrlim_cur=%d",c.rlim_cur);
printf("\nrlim_max=%d",c.rlim_max);
return 0;
}
==========================================================================
Write a C program which receives file names as command line arguments and display those filenames in
ascending order according to their sizes. (e.g $ a.out a.txt b.txt c.txt, …)
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
int main(int argc,char *argv[])
{
char s[100];
struct stat s1,s2;
int i,j;
if(argc<2)
{
printf("Insufficient argument at command prompt");
exit(0);
}
for(i=1;i<argc;i++)
for(j=i+1;j<argc;j++)
{
stat(argv[i],&s1);
stat(argv[j],&s2);
if(s1.st_size <s2.st_size)
{
strcpy(s,argv[i]);
strcpy(argv[i],argv[j]);
strcpy(argv[j],s);
}
}
printf("\nFile in descending order of there size=>\n");
for(i=1;i<argc;i++)
{
stat(argv[i],&s1);
printf("%s\t%d\n",argv[i],s1.st_size);
}
return 0;
}
==========================================================================
Write a C program to display as well as resets the environment variable such as path, home, root etc
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
int main(int argc,char* argv[],char* envp[])
{
int i;
printf("\nEnvironmental variable pass in program=>");
for(i=0;envp[i]!=NULL;i++)
printf("\n%s",envp[i]);

printf("\nChanging environment variable=>");


setenv("ROOT","bhushan",1);
setenv("PATH","pqr",1);
setenv("HOME","abc",1);
printf("\n updated values of variables=>");
printf("\nROOT=%s",getenv("ROOT"));
printf("\nPATH=%s",getenv("PATH"));
printf("\nHOME=%s",getenv("HOME"));

return 0;
}
==========================================================================
Write a C program that will only list all subdirectories in alphabetical order from current directory
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include <sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<dirent.h>
int main()
{
DIR *d;
struct dirent *dr;
char s[35][50],s1[50],s5;
struct stat s2;
int i,j,n=0;
d=opendir(".");
if(d==NULL)
{
printf("unable to open directory");
exit(0);
}
while((dr=readdir(d))!=NULL)
{
stat(dr->d_name,&s2);
if(S_ISDIR(s2.st_mode))
{
strcpy(s[n],dr->d_name);
n++;
}
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(strcmp(s[i],s[j])>0)
{
strcpy(s1,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],s1);
}
}
printf("\n subDirectory in alphabetical order=>\n");
for(i=0;i<n;i++)
printf("%s\n",s[i]);
return 0;
}
==========================================================================
Write a C program to display statistics related to memory allocation system. (Use mallinfo() system
call)
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<malloc.h>
int main()
{
char *s;
struct mallinfo m;
m=mallinfo();
printf("\nsize of data segment=%d",m.arena);
printf("\nno of free chunk=%d",m.ordblks);
printf("\nno of fast bin=%d",m.smblks);
printf("\nno of anonymous mapping=%d",m.hblks);
printf("\nsize of anonymous mapping=%d",m.hblkhd);
printf("\nmaximum total allocated size=%d",m.usmblks);
printf("\nsize of available fastbin=%d",m.fsmblks);
printf("\nsize of total allocated space=%d",m.uordblks);
printf("\nsize of available chunks=%d",m.fordblks);
printf("\nsize of trimmable space=%d",m.keepcost);
s=(char*)malloc(sizeof(char)*1000);
munmap(0,500);
m=mallinfo();
printf("\nsize of data segment=%d",m.arena);
printf("\nno of free chunk=%d",m.ordblks);
printf("\nno of fast bin=%d",m.smblks);
printf("\nno of anonymous mapping=%d",m.hblks);
printf("\nsize of anonymous mapping=%d",m.hblkhd);
printf("\nmaximum total allocated size=%d",m.usmblks);
printf("\nsize of available fastbin=%d",m.fsmblks);
printf("\nsize of total allocated space=%d",m.uordblks);
printf("\nsize of available chunks=%d",m.fordblks);
printf("\nsize of trimmable space=%d",m.keepcost);
return 0;
}
==========================================================================
Write a C program which creates two files. The first file should have read and write permission to
owner, group of owner and other users whereas second file has read and write permission to owner(use
umask() function). Now turn on group-id and turn off group execute permission of first file. Set the read
permission to all user for second file (use chmod() function)
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>
int main()
{
umask(0);
if(creat("file1",S_IRUSR|S_IWUSR|S_IWGRP|S_IROTH|S_IWOTH))
printf("create file1");
umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
if(creat("file 2",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH))
printf("\ncreate file2");
struct stat s;
if(stat("file1" ,&s)<0)
printf("\n stat error file1");
if(chmod("file1",(s.st_mode & ~S_IXGRP)|S_ISGID)<0)
printf("\n chmod error file 1");
if(chmod("file2",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)<0)
printf("\n chmod error file 2");
return 0;
}
==========================================================================
Write a C program to create variable length arrays using alloca() system call.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
#include<alloca.h>
int main()
{
int **a;
int r,c[20],i,j;
printf("\n no of rows=> ");
scanf("%d",&r);
a=(int**)alloca(sizeof(int)*r);
for(int i=0;i<r;i++)
{
printf("Enter no of colums=>",i+1);
scanf("%d",&c[i]);
for(int i=0;i<r;i++)
{
printf("\n ENTER ELEMENTS IN ARRAY=> ");
for(i=0;i<r;i++)
for(j=0;j<c[i];j++)
{
printf("\n ENTER ELEMENT OF a[%d][%d]=> ",i,j);
scanf("%d",&a[i][j]);
}
printf("\n Element in Array=> ");
for(i=0;i<s;i++)
printf("\n");
for(j=0;j<c[i];j++);
}
return 0;
}
==========================================================================
Write a C program that behaves like a shell (command interpreter). It has its own prompt say
“NewShell$”. Any normal shell command is executed from your shell by starting a child process
toexecute the system program corresponding to the command. It should additionally interpret the
following command.
i) count c <filename> - print number of characters in file
ii) count w <filename> - print number of words in file
iii) count l <filename> - print number of lines in file

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<dirent.h>
void count(char ch,char *fn);
int main()
{
char comm[80],t1[20],t2[20],t3[20],t4[20];
int n;
system("clear");
while(1)
{
printf("MyShell$");
fflush(stdin);
fgets(comm,80,stdin);
n=sscanf(comm,"%s%s%s%s",t1,t2,t3,t4);
switch(n)
{
case 1:
if(!fork())
{
execlp(t1,t1,NULL);
perror(t1);
}
break;
case 2:
if(!fork())
{
execlp(t1,t1,t2,NULL);
perror(t1);
}
break;
case 3:
if(strcmp(t1,"count")==0)
count(t2[0],t3);
if(!fork())
{
execlp(t1,t1,t2,t3,NULL);
perror(t1);
}
break;
case 4:
if(!fork())
{
execlp(t1,t1,t2,t3,t4,NULL);
perror(t1);
}
break;
}
}
return 0;
}

void count (char ch,char *fn)


{
int fd, w=0,l=0,c=0;
char ch1;
fd=open(fn,O_RDONLY);
if(fd<0)
{
printf("unable to open file");
exit(0);
}

while(read(fd,&ch1,1)!=0)
{
c++;
if(ch1==' '||ch1=='\n')
w++;
if(ch1=='\n')
l++;
}
close (fd);
switch(ch)
{
case 'c':
printf("\nNo of character=%d",c);
break;
case 'w':
printf("\nNo of word=%d",w);
break;
case 'l':
printf("\nNo of lines=%d",l);
break;
}
}
==========================================================================
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<signal.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>

void sh(int n){


if(n==SIGALRM){
printf("\nAlarm is fired");
fflush(stdout);
}
}

int main(){
int n;
n = fork();
if(n==0){
printf("\nChild Process");
sleep(2);
printf("\nChild Sending SIGALRM");
fflush(stdout);
kill(getppid(),SIGALRM);
sleep(5);
printf("\nChild exist");
return 0;
}
else{
signal(SIGALRM,sh);
wait();
printf("\nParent exist");
return 0;
}
return 0;
}
==========================================================================
Write a C program to display all the files from current directory and its subdirectory whose size is
greater than ’n’ Bytes Where n is accepted from user through command line
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<time.h>
#include<pwd.h>
#include<string.h>
#include<grp.h>
#include<dirent.h>
int main(int argc,char * argv[])
{

if(argc<2)
{
printf("\n enter the no of bytes\n");
exit(0);
}
char b[100];
int c=0,t=0,n,i,a;
char str[100];
char *str1,*str2;
struct dirent *dir;
struct stat s;
getcwd(b,100);
n=atoi(argv[1]);
DIR *dr = opendir(b);
if (dr == NULL)
{
printf("Could not open current directory" );
return 0;
}
while ((dir = readdir(dr)))
{ //printf("\nfilename:%s",dir->d_name);
strcpy(str,dir->d_name);
stat(str,&s);

if(!(S_ISDIR(s.st_mode))&&((long)s.st_size>n))
{
printf("\n %s",str);
}

}
==========================================================================
Write a C program that redirects standard output to a file output.txt. (use of dup and open system call).

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd1,fd2;
fd1=open("output.txt",O_RDWR|O_CREAT,0666);
if(fd1<0)
{
printf("file creation error");
exit(0);
}
fd2=dup(1);
if(dup2(fd1,1)<0)
{
printf("\nunable to duplicate");
exit(0);
}
printf("1:pen\n");
printf("2:pencil\n");
close(fd1);
fflush(stdout);
dup2(fd2,1);
close(fd2);
printf("\nNormal output");
return 0;
}
==========================================================================
Write a C program that behaves like a shell (command interpreter). It has its own prompt say
“NewShell$”. Any normal shell command is executed from your shell by starting a child process to
execute the system program corresponding to the command. It should additionally interpret the
following command.
i) typeline +10 <filename> - print first 10 lines of file
ii) typeline -20 <filename> - print last 20 lines of file
iii) typeline a <filename> - print all lines of file

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

void make_toks(char *s, char *tok[]){


int i=0;
char *p;
p=strtok(s," ");
while(p!=NULL){
tok[i++]=p;
p=strtok(NULL," ");
}
tok[i]=NULL;
}

void typeline(char *fn,char *op){


int fh,i,j,n;
char c;
fh=open(fn,O_RDONLY);
if(fh==-1){
printf("File %s not found.\n",fn);
return;
}
if(strcmp(op,"a")==0){
while(read(fh,&c,1)>0){
printf("%c",c);
}
close(fh);
return;
}
n=atoi(op);
if(n>0){
i=0;
while(read(fh,&c,1)>0){
printf("%c",c);
if(c=='\n'){
i++;
}
if(i==n) break;
}
}
if(n<0){
i=0;
while(read(fh,&c,1)>0){
if(c=='\n')
i++; }
lseek(fh,0,SEEK_SET);
j=0;
while(read(fh,&c,1)>0){
if(c=='\n') j++;
if(j==i+n) break;
}
while(read(fh,&c,1)>0){
printf("%c",c);
}
}
close(fh);
}
int main(){
char buff[80],*args[10];
int pid;
while(1){
printf("myshell$");
fflush(stdin);
fgets(buff,80,stdin);
buff[strlen(buff)-1]='\0';
make_toks(buff,args);
if(strcmp(args[0],"typeline")==0){
typeline(args[2],args[1]);
}
else{
pid=fork();
if(pid>0){
wait();
}
else{
if(execvp(args[0],args)==-1){
printf("Bad command.\n");
}
}
}
}
return 0;
}
==========================================================================
Write a C program to create an unnamed pipe. Write following three messages to pipe and display it.
Message1 = “Hello World”
Message2 = “Hello SPPU”
Message3 = “Linux is Funny”
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
int p[2],i,n;
char buf[70];
char *m1="Message1 = Hello World";
char *m2="Message2 = Hello SPPU";
char *m3="Message3 = LINUX is Funny";
if(pipe(p)<0)
exit(0);
n=fork();
if(n==0)
{
//printf("CHILD WRITTEN:\n");
write(p[1],m1,70);
write(p[1],m2,70);
write(p[1],m3,70);
}
else{
printf("PARENT READS:\n");
for(i=0;i<3;i++)
{
read(p[0],buf,70);
printf("%s\n",buf);
}
}
return 0;
}
==========================================================================
Write a C program that behaves like a shell (command interpreter). It has its own prompt say
“NewShell$”.Any normal shell command is executed from your shell by starting a child process to
execute the system program corresponding to the command. It should additionally interpret the
following command.
i) search f <pattern><filename> - search first occurrence of pattern in filename
ii) search c <pattern><filename> - count no. of occurrences of pattern in filename
iii) search a <pattern><filename> - search all occurrences of pattern in filename

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<dirent.h>
void search(char,char*,char*);
int main()
{
char comm[80],t1[20],t2[20],t3[20],t4[20];
int n;
system("clear");
while(1)
{
printf("MyShell$");
fflush(stdin);
fgets(comm,80,stdin);
n=sscanf(comm,"%s%s%s%s",t1,t2,t3,t4);
switch(n)
{
case 1:
if(!fork())
{
execlp(t1,t1,NULL);
perror(t1);
}
break;
case 2:
if(!fork())
{
execlp(t1,t1,t2,NULL);
perror(t1);
}
break;
case 3:
if(!fork())
{
execlp(t1,t1,t2,t3,NULL);
perror(t1);
}
break;
case 4:
if(strcmp(t1,"search")==0)
search(t2[0],t3,t4);
else
if(!fork())
{
execlp(t1,t1,t2,t3,t4,NULL);
perror(t1);
}
break;
}
}
return 0;
}

void search(char opt,char *patt,char *fn)


{
int fd,i=1,j=0,cnt=0;
char ch,buff[80],*p;
fd=open(fn,O_RDONLY);
if(fd<0)
{
printf("unable to open file");
exit(0);
}

switch(opt)
{
case 'f':
while(read(fd,&ch,1)!=0)
{
if(ch=='\n')
{
buff[j]='\0';
j=0;
if(strstr(buff,patt)!=NULL)
{
printf("%d\t%s",i,buff);
break;
}
}
else
buff[j++]=ch;
}
close(fd);
break;
case 'c':
while(read(fd,&ch,1)!=0)
{
if(ch=='\n')
{
buff[j]='\0';
j=0;
if(strstr(buff,patt)!=NULL)
{
p=buff;
while((p=strstr(p,patt))!=NULL)
{
cnt++;
p++;
}
}
}
else
buff[j++]=ch;
}
printf("\nOccurances=>%d",cnt);
close(fd);
break;
case 'a':
while(read(fd,&ch,1)!=0)
{
if(ch=='\n')
{
buff[j]='\0';
j=0;
if(strstr(buff,patt)!=NULL)
{
p=buff;
while((p=strstr(p,patt))!=NULL)
{
printf("\n%d\t%s",i,p);
i++;
p++;
}
}
}
else
buff[j++]=ch;
}
close(fd);
break;
}
}
==========================================================================
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<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
int main(int args, char *argv[])
{
struct stat s;
struct passwd *ps;
struct group *gs;
char t[50];

if(args<2){
printf("No file name found");
exit(0);
}

stat(argv[1], &s);
printf("\n");

if(S_ISREG(s.st_mode)){
printf("\n it is Regular file");
}
if(S_ISDIR(s.st_mode)){
printf("\n it is directory");
}
if(S_ISCHR(s.st_mode)){
printf("\n it is charcter file");
}
if(S_ISFIFO(s.st_mode)){
printf("\n it is fifo /pipe");
}
if(S_ISBLK(s.st_mode)){
printf("\nit is Block file");
}
if(S_ISLNK(s.st_mode)){
printf("\n it is symbolic link");
}
if(S_ISSOCK(s.st_mode)){
printf("\n it is socket file");
}
return 0;
}
==========================================================================
Write a C program which creates a child process and child process catches a signal SIGHUP, SIGINT
and SIGQUIT. The Parent process send a SIGHUP or SIGINT signal after every 3 seconds, at the end of
15 second parent send SIGQUIT signal to child and child terminates by displaying message "My Papa
has Killed me!!!”.
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
void sh(){
printf("\nSIGINT is caught");
signal(SIGINT, sh);
}
void sh2(){
printf("\nSIGHUP is caught");
signal(SIGHUP, sh2);
}

void sh3(){
printf("\nMy papa has killed me");

exit(0);
}

int main(){
int n=fork();
if(n==0){
signal(SIGINT, sh);
signal(SIGHUP,sh2);
signal(SIGQUIT,sh3);
while(1){

}
}
else{
sleep(3);
kill(n,SIGINT);
sleep(3);
kill(n,SIGHUP);
sleep(3);
kill(n,SIGINT);
sleep(3);
kill(n,SIGHUP);
sleep(3);
kill(n,SIGINT);
sleep(3);

kill(n,SIGQUIT);
exit(0);
}
return 0;
}
==========================================================================
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<signal.h>
void sh(int);

int main()
{
signal(SIGINT,sh);
while(1)
{
}
return 0;
}

void sh(int n)
{
if(n==SIGINT)
{
printf("\nctl+c cought");
fflush(stdout);
signal(SIGINT,SIG_DFL);
}
}
==========================================================================
Write a C program to implement the following unix/linux command on current directory ls –l >
output.txt DO NOT simply exec ls -l > output.txt or system command from the program

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<signal.h>
int main()
{
int pid=fork();
int fd=creat("output.txt",0777);
if(pid!=0 &&pid!=-1)
{
close(1);
dup(fd);
execlp("ls","ls","-l",NULL);
close(fd);
}
return 0;
}
==========================================================================
Write a C program to display the given message ‘n’ times. (make a use of setjmp and longjmp system
call)
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<setjmp.h>
static jmp_buf jmp;
int main()
{
char *s;
int n;
int cnt=0;
printf("\nEnter message=>");
gets(s);
printf("\nenter n values=>");
scanf("%d",&n);
setjmp(jmp);
printf("\n%s",s);
cnt++;
if(cnt<n)
longjmp(jmp,1);
return 0;
}
==========================================================================
Write a C program to display all the files from current directory which are created in a particular month.
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<time.h>
#include<pwd.h>
#include<string.h>
#include<grp.h>
#include<dirent.h>
void main()
{
char b[100];
struct stat s;
char month[10];
char *t1,m[50];
getcwd(b,100);
printf("\n enter month :");
gets(m);
struct dirent *dir;
getcwd(b,100);
DIR *dr = opendir(b);
if (dr == NULL)
{
printf("Could not open current directory" );
return 0;
}
while ((dir = readdir(dr)))
{
stat(dir->d_name,&s);
t1=ctime(&s.st_ctime);
if(strstr(t1,m))
{
printf("\nfilename:%s",dir->d_name);
}
}
}
==========================================================================
Write a C program to display the last access and modified time of a given file.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<time.h>
int main(int argc,char *argv[])
{
char s[100],t1[100],t2[100];
struct stat s1,s2;
printf("\nEnter file name=>");
gets(s);
stat(s,&s1);
strcpy(t1,ctime(&s1.st_atim));
strcpy(t2,ctime(&s1.st_mtim));
printf("\nAccess time=>%s",t1);
printf("\nmodification time=>%s",t2);
return 0;
}
==========================================================================
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 during the execution. ls –l | wc -l
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<signal.h>
#include<unistd.h>
void sh(int);
int main()
{
int fd[2],n;
pipe(fd);
signal(SIGQUIT,sh);
signal(SIGINT,sh);
n=fork();
if(n==0)
{
close (fd[0]);
close(1);
dup(fd[1]);
close(fd[1]);
execlp("ls","ls","-l",NULL);
}
else
{
wait();
sleep(20);
close (fd[1]);
close(0);
dup(fd[0]);
close(fd[0]);
execlp("wc","wc","-l",NULL);
}
return 0;
}
void sh(int n)
{
if(n==SIGQUIT)
printf("\n ctl+\\ cought");
if(n==SIGINT)
printf("\n ctl+c cought");
fflush(stdout);
}
==========================================================================
Write a C program to move the content of file1.txt to file2.txt and remove the file1.txt from directory
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<time.h>
int main(int argc,char *argv[])
{
char *s;
struct stat s1;
int fd1,fd2;
fd1=open("b1.txt",O_RDONLY);
fd2=open("b4.txt",O_WRONLY|O_CREAT|O_TRUNC,0777);
fstat(fd1,&s1);
s=(char*)malloc(sizeof(char)*s1.st_size);
read(fd1,s,s1.st_size);
write(fd2,s,s1.st_size);
close(fd1);
close(fd2);
if(unlink("b1.txt")<0)
printf("\n unsuccessful unlink");
else
printf("\nSuccessful");
return 0;
}
==========================================================================
Write a C program which creates a child process to run linux/ unix command or any user defined
program. The parent process set the signal handler for death of child signal and Alarm signal. If a child
process does not complete its execution in 5 second then parent process kills child process.

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<signal.h>
void sh(int signo)
{
printf("\n child process is being killed by parent");
exit(0);
}
int main()
{
int n;
signal(SIGCHLD,sh);
n=fork();
if(n==0)
{
execlp("ls","ls","-l",NULL);
}
else
{
sleep(5);
printf("\nchild has exceed time");
kill(n,SIGINT);
}
return 0;
}
==========================================================================
Write a C program that print the exit status of a terminated child process.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int n,s;
n=fork();
if(n==0)
{
execlp("date","date",NULL);
}
else
{
waitpid(n,&s,WNOHANG);
if(WIFEXITED(s))
{
printf("exit status=>%d",WEXITSTATUS(s));
}
}
return 0;
}

You might also like