College of Technology and Engineering, MPUAT, Udaipur (Raj.
)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Index
Date of Sign/
Sr. Page
Topic
No No. Remarks
Done Checked
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -1
Aim -Write a program to display a file page wise assuming a page has 10 lines and
each line has 80 characters.
Code -
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
char buff[10][80];
int i, j;
char k;
FILE *fp;
if(argc!=2)
{
fprintf(stderr, "Usage: ./[Link] file name\n");
exit(1);
}
fp=fopen(argv[1], "r");
while(!feof(fp))
{
for(i=0;i<10;i++)
for(j=0;j<80;j++)
buff[i][j]='\0';
for(i=0;i<10;i++)
fgets(buff[i],80,fp);
for(i=0;i<10;i++)
printf("%s", buff[i]);
scanf("%c", &k);
}
fclose(fp);
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -2
Aim -Write a Program which converts all the small case letters in a file into
appropriate capital letters.
Code -
#include<stdio.h>
#include<stdlib.h>
int main ( int argc, char *argv[] )
{
FILE *fp, *ft;
char ch;
if(argc!=2)
{
fprintf(stderr, "Usage: ./[Link] file name\n");
exit(1);
}
fp = fopen(argv[1], "r");
if(fp == NULL )
{
printf("Can't open file");
exit(1);
}
ft = fopen("temp", "w");
while(!feof(fp))
{
ch = fgetc(fp);
if(ch>=97 && ch<=122)
ch = ch + 'A' - 'a';
fputc(ch, ft);
}
fclose(ft);
fclose(fp);
ft = fopen("temp", "r");
fp = fopen(argv[1], "w");
if(ft != NULL)
{
while(feof(ft))
{
ch = fgetc(ft);
fputc(ch,fp);
}
fclose(ft);
fclose(fp);
}
Else
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
printf("Error in opening file");
return 0;
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -3
Aim -Write a program to print the details of the system (use uname sys call).
Code -
#include<stdio.h>
#include<sys/utsname.h>
int main()
{
struct utsname u;
if(uname(&u) !=0 )
fprintf(stderr, "Uname Error");
printf("\n %s \n %s \n %s \n %s \n %s \n", [Link], [Link], [Link], [Link],
[Link]);
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -4
Aim - Write a program which will print the list of environment variable and also print
the value of the PATH system variable.
Code -
#include<stdio.h>
#include<stdlib.h>
extern char **environ; //the external var points to proces env list when process execute.
int main(void)
{
int i;
char *path;
printf("The environment list follows: \n");
for(i=0; environ[i] != NULL; i++)
printf("environ[%d]: %s \n", i, environ[i]);
if((path = getenv("PATH")) == NULL)
printf("PATH environment variable not set\n");
else
printf("The value of PATH variable = %s\n", path);
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -5
Aim - Write a program to print current (soft) limit and maximum (Hard) limits of all
resources.
Code -
#include<stdio.h>
#include<stdlib.h>
main()
{
struct rlimit rl;
int i;
printf("\n Resources Name \t Current Limit \tMax Limit \t");
for(i=0;i<=10;i++)
{
if(getrlimit(i, &rl)<0)
{
printf("Error in grelimit\n");
exit(1);
}
switch(i)
{
case RLIMIT_CPU :
printf("\nRLIMIT_CPU\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
case RLIMIT_DATA:
printf("\nRLIMIT_DATA\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
case RLIMIT_FSIZE:
printf("\nRLIMIT_FSIZE\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
case RLIMIT_MEMLOCK:
printf("\nRLIMIT_MEMLOCK\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
case RLIMIT_NOFILE:
printf("\nRLIMIT_NOFILE\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
case RLIMIT_NPROC:
printf("\nRLIMIT_NPROC\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
/*case RLIMIT_OFILE:
printf(Â"\nRLIMIT_OFILE\t%d\t\t%dÂ",rl.rlim_cur,rl.rlim_max);
break;*/
case RLIMIT_RSS:
printf("\nRLIMIT_RSS\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
case RLIMIT_STACK:
printf("\nRLIMIT_STACK\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
case RLIMIT_LOCKS:
printf("\nRLIMIT_LOCKS\t%d\t\t%d",rl.rlim_cur,rl.rlim_max);
break;
}
}
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -6
Aim -Write a program with an exit handler that outputs CPU usage.
Code -
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/times.h>
#include<time.h>
static void showtimes(void)
{
time_t time1, time2;
time_t time_dif;
time1 = time(NULL);
printf("time1 : %ld", time1);
sleep(5);
time2 = time(NULL);
printf("\ntime2 : %ld", time2);
time_dif = difftime(time2, time1);
printf("\nThe showtime slept for: %ld seconds\n", time_dif);
}
int main(void)
{
if( atexit(showtimes) )
{
fprintf(stderr, "Failed to install showtime exit handler\n");
return 1;
}
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -7
Aim -Write a program that prints it’s & it’s parent’s process ID.
Code -
#include<stdio.h>
#include<unistd.h>
int main(void)
{
printf("I am process %ld\n", (long)getpid());
printf("My parent is %ld\n", (long)getppid());
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -8
Aim -Write a program that prints out various user & group ID’s.
Code –
#include<stdio.h>
#include<unistd.h>
int main(void)
{
printf("My real user ID is %5ld\n", (long)getuid());
printf("My effective user ID is %5ld\n", (long)getuid());
printf("My real group ID is %5ld\n", (long)getuid());
printf("My effective group ID is %5ld\n", (long)getuid());
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -9
Aim -Write a program which uses fork to create a child process& then parent & child
print their respective process ID’s
Code -
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main(void)
{
pid_t childpid;
childpid = fork();
if(childpid == -1)
{
perror("Failed to fork");
return 1;
}
if(childpid == 0)
printf("I am child %ld\n", (long)getpid());
else
printf("I am parent %ld\n", (long)getpid());
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -10
Aim -Write a program that creates a chain of n processes, where n is a command line
argument.
Code -
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main( int argc, char *argv[])
{
pid_t childpid =0;
int i, n;
if(argc != 2)
{
fprintf(stderr, "Usage: %s processs\n", argv[0]);
return 1;
}
n = atoi(argv[1]);
for(i =1; i<n; i++)
{
if( childpid = fork())
break;
}
fprintf(stderr, "i:%d process ID:%ld parent ID;%ld child ID:%ld\n", i, (long)getpid(),
(long)getppid(), (long)childpid );
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -11
Aim -Write a program that creates a fan of n processes where n is passed as a
command line argument.
Code -
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main ( int argc, char * argv[])
{
pid_t childpid=0;
int i, n;
if(argc !=2)
{
fprintf(stderr, "Usage: %s processes\n", argv[0]);
return 1;
}
n = atoi(argv[1]);
for (i=1;i<n;i++)
if( (childpid = fork() ) <=0)
break;
fprintf(stderr, "i;%d process ID:%ld parent ID:%ld child ID:%ld\n", i, (long)getpid(),
(long)getppid(), (long)childpid );
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -12
Aim -Write a program to show that same opened file can be shared by both parent
and child processes.
Code -
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
FILE *fp;
int fd;
char ch;
fp = fopen("test", "w");
fprintf( fp, "%s\n", "This line is written by PARENT PROCESS");
fflush(NULL);
fd = fork();
if(fd < 0)
{
printf("Fork Error");
exit(1);
}
if(fd == 0)
{
fprintf(fp, "%s", "This line is written by CHILD PROCESS\n");
fclose(fp);
fp = fopen("test", "r");
while(!feof(fp))
printf("%c", getc(fp));
}
if(fd > 0)
{
if(fd != wait(NULL) );
{
perror("Parent failed to wait due to signal or error");
return 1;
}
}
fclose(fp);
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -13
Aim -Write a program that creates a child process to run ls – l.
Code -
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main(void)
{
pid_t childpid;
childpid = fork();
if(childpid == -1)
{
perror("Failed to fork");
return 1;
}
if(childpid == 0)
{
execl("/bin/ls", "ls", "-l", NULL);
perror("Child failed to exec ls");
return 1;
}
if(childpid != wait(NULL))
{
perror("Parent failed to wait due to signal or error");
return 1;
}
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -14
Aim -Write a program to create a zombie child and find its status using system (ps)
command.
Code -
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
int main()
{
int fd;
if( (fd = fork()) < 0)
{
printf("Error in creating child");
exit(1);
}
if( fd == 0)
kill(getpid(), SIGKILL);
else
sleep(2);
system("ps -f");
return 0;
}
Output: -
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
Practical -15
Aim -Write a program to copy a file.
Code -
// the copyfile.c function copies a file fromfd to tofd
#include<errno.h>
#include<unistd.h>
#define BLKSIZE 1024
int copyfile( int fromfd, int tofd)
{
char *bp;
char buf[BLKSIZE];
int bytesread;
int byteswritten = 0;
int totalbytes = 0;
for( ; ; )
{
while( ( (bytesread = read(fromfd, buf, BLKSIZE)) == -1 ) && (errno == EINTR) );
if( bytesread <=0)
break;
bp = buf;
while(bytesread > 0)
{
while(((byteswritten = write(tofd, bp, bytesread) ) == -1) && (errno ==
EINTR));
if(byteswritten < 0)
break;
totalbytes += byteswritten;
bytesread -= byteswritten;
bp += byteswritten;
}
if( byteswritten == -1)
break;
}
return totalbytes;
}
//the main program to copy a file
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
College of Technology and Engineering, MPUAT, Udaipur (Raj.)
Name – JINESH PRAJAPAT Class - B. Tech, III Year Branch - AI & DS Sem – V
Subject – Principle of Operating System (AI 352)
#include <sys/stat.h>
#define READ_FLAGS O_RDONLY
#define WRITE_FLAGS (O_WRONLY | O_CREAT | O_EXCL)
#define WRITE_PERMS (S_IRUSR | S_IWUSR)
int main(int argc, char *argv[])
{
int bytes;
int fromfd, tofd;
if (argc != 3)
{
fprintf(stderr, "Usage: %s from_file to_file\n", argv[0]);
return 1;
}
if ((fromfd = open(argv[1], READ_FLAGS)) == -1)
{
perror("Failed to open input file");
return 1;
}
if ((tofd = open(argv[2], WRITE_FLAGS, WRITE_PERMS)) == -1)
{
perror("Failed to create output file");
return 1;
}
bytes = copyfile(fromfd, tofd);
printf("%d bytes copied from %s to %s\n", bytes, argv[1],
argv[2]);
return 0;
}
Output: -