0% found this document useful (0 votes)
55 views

Lab Exercise

The document provides examples of various Unix commands for working with directories, files, file permissions, processes and terminal operations. It demonstrates how to create, delete, move and list directories and files, set permissions on files, use date/time commands, view who is logged in, get help pages and perform other tasks. The document is serving as a tutorial on basic Unix commands.

Uploaded by

ASD
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Lab Exercise

The document provides examples of various Unix commands for working with directories, files, file permissions, processes and terminal operations. It demonstrates how to create, delete, move and list directories and files, set permissions on files, use date/time commands, view who is logged in, get help pages and perform other tasks. The document is serving as a tutorial on basic Unix commands.

Uploaded by

ASD
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 34

PANIMALAR ENGINEERING COLLEGE

DEPARTMENT OF CSE

REG.NO:

EX.NO:1 BASIC OF UNIX COMMANDS

I.WORKING WITH DIRECTORIES


Create a directory:
[cse414@cc2linux ~]$ mkdir d1
[cse414@cc2linux ~]$ mkdir d2
[cse414@cc2linux ~]$ ls
d1 d2 latest new
Change the working directory:
[cse414@cc2linux ~]$ cd d1
[cse414@cc2linux d1]$ l
[cse414@cc2linux d1]$ cat > d
operating system.
networks.
micro processors.
[cse414@cc2linux d1]$ ls
d
[cse414@cc2linux d1]$ cd ..
[cse414@cc2linux ~]$ cd
Move file within directories:
[cse414@cc2linux ~]$ mv new d2
[cse414@cc2linux ~]$ cd d2 d3
[cse414@cc2linux d2]$ ls
New

.FILE PERMISSIONS: (u-user,g-group,o-others) (r-read,w-write,x-execute)


(octal notation for read-4, write-2 ,execute-1)
Syntax: chmod ugo filename
[cplab2@localhost ~]$ chmod 761 College(user has permission to do all,group has
only read and write permission, others has only execute permission)
[cplab2@localhost ~]$ ls -l College
-rwxrw---x 1 cplab2 cplab2 39 Feb 1 11:44 College
[cplab2@localhost ~]$ chmod 463 College
[cplab2@localhost ~]$ ls -l College
-r--rw--wx 1 cplab2 cplab2 39 Feb 1 11:44 College

________________
II.WORKING WITH FILE RELATED COMMANDS

Creating a file:
[cse414@cc2linux ~]$ cat > basic
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
operating system is used to have interface between system and user.

[cse414@cc2linux ~]$ cat > commands


working with file related commands.
process and status information command
filter command
working with directories.

[cse414@cc2linux ~]$ cat > pipes


if we want to perform multiple tasks we can go pipes and filters.
a pipe is a mechanism which takes the output of a command
its input for the next command.

Display a file:
[cse414@cc2linux ~]$ cat commands
working with file related commands.
process and status information command
filter command
working with directories.

Append a file:
[cse414@cc2linux ~]$ cat >> pipes
filters are used to extract the lines, which contain a specific pattern to arrange
the contents.
[cse414@cc2linux ~]$ cat pipes
if we want to perform multiple tasks we can go pipes and filters. a pipe is a
mechanism which takes the output of a command
its input for the next command.
filters are used to extract the lines,which contain a specific pattern to arrange
the contents.

________________
Concatenation of files:
[cse414@cc2linux ~]$ cat basic commands > pipes
[cse414@cc2linux ~]$ cat pipes
multitasking is the capability of the operating system to peerform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
working with file related commands.
process and status information command
filter command
working with directories.

Copy a file:
[cse414@cc2linux ~]$ cp basic commands
[cse414@cc2linux ~]$ cat commands
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .

Move a file:
[cse414@cc2linux ~]$ mv basic commands
[cse414@cc2linux ~]$ cat basic
cat: basic: No such file or directory
[cse414@cc2linux ~]$ cat commands
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .

Comparing files:
[cse414@cc2linux ~]$ cmp commands pipes
cmp: EOF on commands

Difference between files:


[cse414@cc2linux ~]$ diff commands pipes
6a7,12
> working with file related commands.
> process and status information command
> filter command
> working with directories.

List :
[cse414@cc2linux ~]$ list ls
-bash: list: command not found
[cse414@cc2linux ~]$ ls
commands pipes

Remove:
[cse414@cc2linux ~]$ rm pipes
[cse414@cc2linux ~]$ ls
commands

[cse414@cc2linux ~]$ rm -i commands


rm: remove regular file `commands'? n

[cse414@cc2linux ~]$ rm -r commands

More:
[cse414@cc2linux ~]$ more new old
::::::::::::::
new
::::::::::::::
multitasking is the capability of the operating system to peerform various tasks
.A single user can perform various tasks.
every user can have a login name and a password .
os is used to have interface between system and user.
old: No such file or directory

Additional file name:


[cse414@cc2linux ~]$ ln new latest
[cse414@cc2linux ~]$ ls
latest new

Present working directory:


[cse414@cc2linux ~]$ pwd
/home/cse414
III.PROCESS AND STATUS INFORMATION COMMAND
Date:
[cse414@cc2linux ~]$ date
Sun Dec 31 23:55:10 IST 2000
[cse414@cc2linux ~]$ date +%d
31
[cse414@cc2linux ~]$ date +%m
12
[cse414@cc2linux ~]$ date +%h
Dec
[cse414@cc2linux ~]$ date +%y
00
[cse414@cc2linux ~]$ date +%R
23:55
[cse414@cc2linux ~]$ date +%T
23:55:38

Calendar:
[cse414@cc2linux ~]$ cal
December 2000
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

[cse414@cc2linux ~]$ cal 2015


2015

January February March


Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28
25 26 27 28 29 30 31 29 30 31

April May June


Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
30 31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31

Binary calculator:
[cse414@cc2linux ~]$ bc
bc 1.06
5*4
20

Who :
[cse414@cc2linux ~]$ who
cse319 pts/1 Dec 31 23:04 (132.147.162.8)
cse318 pts/2 Dec 31 23:51 (132.147.162.7)
cse212 pts/3 Dec 31 23:04 (132.147.162.9)
cse316 pts/4 Dec 31 23:24 (132.147.162.5)

Finger command:
[cse414@cc2linux ~]$ finger
Login Name Tty Idle Login Time Office Office Phone
Cse303 cse303 pts/22 Dec 31 23:10 (132.147.162.11)
Cse322 cse322 pts/3 1 Dec 31 23:04 (132.147.162.9)
Cse324 cse324 pts/8 1 Dec 31 23:06 (132.147.162.10)
cse325 cse325 pts/13 Dec 31 23:19 (132.147.162.1)
cse326 cse326 pts/11 Dec 31 23:55 (132.147.162.2)
cse332 cse332 pts/9 Dec 31 23:06 (132.147.162.3)
cse335 cse335 pts/7 1 Dec 31 23:05 (132.147.162.4)
cse338 cse338 pts/4 Dec 31 23:24 (132.147.162.5)
cse340 cse340 pts/10 2 Dec 31 23:32 (132.147.162.27)
cse346 cse346 pts/2 Dec 31 23:51 (132.147.162.7)

Who am i:
[cse414@cc2linux ~]$ who am i
cse414 pts/11 Dec 31 23:55 (132.147.162.2)

Man:
[cse414@cc2linux ~]$ man date
DATE(1) User Commands DATE(1)
NAME
date - print or set the system date and time
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION
Display the current time in the given FORMAT, or set the system
date.

-d, --date=STRING
display time described by STRING, not ânowâ
-f, --file=DATEFILE
like --date once for each line of DATEFILE

[1]+ Stopped man date

Word count:
[cse414@cc2linux ~]$ wc new
wc new
6 85 557 new
[cse414@cc2linux ~]$ wc -w new
85 new
[cse414@cc2linux ~]$ wc -c new
557 new
[cse414@cc2linux ~]$ wc -l new
6 new

Echo:
[cse414@cc2linux ~]$ echo

Read:
[cse414@cc2linux ~]$ read a
5
[cse414@cc2linux ~]$ echo $a
5

Terminal line:
[cse414@cc2linux ~]$ tty
/dev/pts/11

Adding line number to file:


[cse414@cc2linux ~]$ nl new
1 multitasking is the capability of the operating system to peerform various
tasks.A single user can perform various tasks.
2 every user can have a login name and a password .so, accessing another
users data is impossible without permission.

Which :
[cse414@cc2linux ~]$ which date
/bin/date
[cse414@cc2linux ~]$ which new
/usr/bin/which: no new in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr
/X11R6/bin:/home/cse414/bin)

IV.FILTER COMMAND
Sorting:
[cse414@cc2linux ~]$ cat new
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
[cse414@cc2linux ~]$ sort new
every user can have a login name and a password .
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.

[cse414@cc2linux ~]$ sort -r new


multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .

[cse414@cc2linux ~]$ sort -u new


every user can have a login name and a password .
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.

[cse414@cc2linux ~]$ sort -n new


communication is between different terminals.
every user can have a login name and a password .
multitasking is the capability of the operating system to perform various tasks
.A single user can perform various tasks.

Head:
[cse414@cc2linux ~]$ head new
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .

[cse414@cc2linux ~]$ head -2 new


multitasking is the capability of the operating system to perform various tasks.A
single user can perform various tasks.
every user can have a login name and a password .so, accessing another users data
is impossible without permission.

Tail:
[cse414@cc2linux ~]$ tail new
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
[cse414@cc2linux ~]$ tail -2 new
Necessary ingredients like conditional and control structures and variables.
communication is between different terminals.

Cut:
[cse414@cc2linux ~]$ cut -c1-3 new
mul
eve
UNI
os
UNI
com
[cse414@cc2linux ~]$ cut -r1-3 new
cut: invalid option -- r
Try `cut --help' for more information.
[cse414@cc2linux ~]$ ls
latest new

Paste:
[cse414@cc2linux ~]$ paste new latest
multitasking is the capability of the operating system to perform various tasks.A
single user can perform various tasks. multitasking is the capability of the
operating system to perform various tasks.A single user can perform various tasks.

Grep:
[cse414@cc2linux ~]$ grep "between" new
os is used to have interface between system and user.
communication is between different terminals.

Paste:
[cse414@cc2linux ~]$ paste -d "&" new latest
multitasking is the capability of the operating system to perform various tasks.A
single user can perform various tasks.&multitasking is the capability of the
operating system

.& os is used to have interface between system and user.&os is used to have
interface between system and user

Common:
[cse414@cc2linux ~]$ comm new latest
multitasking is the capability of the operating system to peerform
various tasks.A single user can perform various tasks.
every user can have a login name and a password .so, accessing
another users data is impossible without permission.
os is used to have interface between system and user.
communication is between different terminals.

Pipes:
[cse414@cc2linux ~]$ who | wc -l
22

________________
EX.NO: 2a IMPLEMENTING opendir, readdir, closedir, etc.. SYSTEM CALLS

PROGRAM:
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf(“\n\n ENTER THE DIRECTORY NAME:”);
scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{
printf(“The given directory does not exist”);
exit(1);
}
while(dptr=readdir(dirp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);}

OUTPUT:
[kalai@localhost ~]$ cc sys1.c
[kalai@localhost ~]$ ./a.out

ENTER THE DIRECTORY NAME:kanna


.
..
flower

________________
EX.NO: 2b IMPLEMENTING Fork, getpid, Exit, Etc…. SYSTEM CALLS

PROGRAM:
#include<stdio.h>
#include<unistd.h>
main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf(“ERROR IN PROCESS CREATION \n”);
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf(“\n THE PARENT PROCESS ID IS: %d\n”, pid1);
}
else
{
pid2=getpid();
printf(“\n THE CHILD PROCESS ID IS: %d\n”, pid2);
}
}

OUTPUT:
[kalai@localhost ~]$ cc sys2.c
[kalai@localhost ~]$ ./a.out

THE PARENT PROCESS ID IS: 8062

THE CHILD PROCESS ID IS: 8063

________________
EX.NO: 2c IMPLEMENTING open,read SYSTEM CALLS

1.OPEN SYSTEM CALL

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fd;
if((fd=open("prime.sh"))==-1)
{
perror("cannot open the file.dat");
exit(0);
}
else
printf("\n FILE OPENED SUCCESSSFULLY");
return 0;
}
OUTPUT:
[kalai@localhost ~]$ cc opdir.c
[kalai@localhost ~]$ ./a.out
FILE OPENED SUCCESSSFULLY

________________
2.READ SYSTEM CALL
#include<stdio.h>
#include<stdlib.h>
main()
{
char b[20];
int fd,xr;
if((fd=open("flowers.sh",0))==-1)
{
printf("cannot open file");
exit(1);
}
do
{
xr=read(fd,b,20);
b[xr]='\0';
printf("%s",b);
}
while(xr==20);
close(fd);}

OUTPUT:
[kalai@localhost ~]$ cc sysread1.c
[kalai@localhost ~]$ ./a.out
rose
jasmine
lotus
________________
EX.NO:3a SIMULATE grep COMMAND USING C

PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}

OUTPUT:
[kalai@localhost ~]$ cc grep1.c
[kalai@localhost ~]$ ./a.out
Enter file name
grep1.c
Enter pattern to be searched
printf
printf("Enter file name\n");
printf("Enter pattern to be searched\n");
printf("%s",temp);
________________
EX.NO:3b SIMULATE cp COMMAND USING C

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>

#define BUFF_SIZE 1024

int main(int argc, char* argv[])


{
int srcFD,destFD,nbread,nbwrite;
char *buff[BUFF_SIZE];
/*Check if both src & dest files are received or --help is received to get
usage*/
if(argc != 3 || argv[1] == "--help")
{
printf("\nUsage: cpcmd source_file destination_file\n");
exit(EXIT_FAILURE);
}
/*Open source file*/
srcFD = open(argv[1],O_RDONLY);
if(srcFD == -1)
{
printf("\nError opening file %s errno = %d\n",argv[1],errno);
exit(EXIT_FAILURE);
}
/*Open destination file with respective flags & modes O_CREAT & O_TRUNC is
to truncate existing file or create a new file S_IXXXX are file permissions for
the user,groups & others*/
destFD = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR |
S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(destFD == -1)
{
printf("\nError opening file %s errno = %d\n",argv[2],errno);
exit(EXIT_FAILURE);
}

/*Start data transfer from src file to dest file till it reaches EOF*/
while((nbread = read(srcFD,buff,BUFF_SIZE)) > 0)
{
if(write(destFD,buff,nbread) != nbread)
printf("\nError in writing data to %s\n",argv[2]);
}
if(nbread == -1)
printf("\nError in reading data from %s\n",argv[1]);
if(close(srcFD) == -1)
printf("\nError in closing file %s\n",argv[1]);

if(close(destFD) == -1)
printf("\nError in closing file %s\n",argv[2]);

exit(EXIT_SUCCESS);
}

OUTPUT:
[kalai@localhost ~]$ cc cp1.c
[kalai@localhost ~]$ cat flower
ROse
lotus
jasmine
[kalai@localhost ~]$ cat pookul
cat: pookul: No such file or directory
[kalai@localhost ~]$ ./a.out flower pookul
[kalai@localhost ~]$ cat pookul
ROse
lotus
jasmine

________________
EX.NO:3c SIMULATE cat COMMAND USING C

PROGRAM:
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[3] )
{
int fd,i;
char buf[2];
fd=open(argv[1],O_RDONLY,0777);
if(fd==-argc)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)
{
printf("%c",buf[0]);
}
close(fd);
}
}
OUTPUT:
[kalai@localhost ~]$ cc cat1.c
[kalai@localhost ~]$ ./a.out area1.sh
echo "Enter the radius: "
read r
p=3.14
a=`expr $p\*$r\*$r|bc`
echo "Area is $a"
________________
EX.NO: 4a AREA OF A CIRCLE

PROGRAM:
echo "Enter the radius: "
read r
p=3.14
a=`expr $p\*$r\*$r|bc`
echo "Area is $a"

OUTPUT:
[kalai@localhost ~]$ sh area1.sh
Enter the radius:
4
Area is 50.24

________________
EX.NO: 4b GREATEST OF THREE NUMBERS

PROGRAM:
echo "Enter three numbers:"
read a
read b
read c
if test $a -gt $b -a $a -gt $c
then
echo "A is largest"
elif test $b -gt $c
then
echo "B is largest"
else
echo "C is largest"
fi

OUTPUT:
[kalai@localhost ~]$ sh greatest3.sh
Enter three numbers:
4
6
9
C is largest

________________
EX.NO: 4c ARITHMETIC OPERATION USING SWITCH STATEMENT

PROGRAM:
echo "Enter two no.s"
read a
read b
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Exit"
echo "Choose an option"
read c
case $c in
1)s=`expr $a+$b|bc`;;
2)s=`expr $a-$b|bc`;;
3)s=`expr $a\*$b|bc`;;
4)s=`expr $a\$b|bc`;;
5)exit;;
esac
echo "Result is $s"

OUTPUT:
[kalai@localhost ~]$ sh arithcase.sh
Enter two no.s
4
2
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Choose an option
1
Result is 6

________________
EX.NO: 4d SUM OF DIGITS OF GIVEN NUMBER

PROGRAM:
echo "Enter a number"
read n
s=0
z=0
t=10
while test $n -gt $z
do
r=`expr $n % $t|bc`
n=`expr $n / $t|bc`
s=`expr $s + $r|bc`
done
echo "Sum of digits is: $s"

OUTPUT:
[kalai@localhost ~]$ sh sumofdigits.sh
Enter a number
345
Sum of digits is: 12
________________
EX.NO: 4e COMMAND SUBSTITUTION

PROGRAM:
echo "1)Display list of files"
a=`ls`
echo $a
echo
echo
echo "2)Display calendar"
b=`cal`
echo $b
echo
echo
echo "3)Display the date"
c=`date`
echo $c
echo
echo
echo "4)Display the file content"
f=`cat area1.sh`
echo $f
echo

OUTPUT:
[kalai@localhost ~]$ sh comsub.sh
1)Display list of files
aaa area1.sh area.sh arithcase.sh bank.c bankpy.py cat1.c comsub.sh cp1.c ddetect.c
fact1.sh file1 file.dat files files3 files4

2)Display calendar
February 2019 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28

3)Display the date


Mon Feb 4 20:20:54 IST 2019

4)Display the file content


echo "Enter the radius: " read r p=3.14 a=`expr $p\*$r\*$r|bc` echo "Area is $a"
________________
EX.NO:5a IMPLEMENTING FIRST COME FIRST SERVED CPU SCHEDULING ALGORITHM

PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;
printf("\n\t First In First Out");
printf("\n\t No. Of Process : ");
scanf(" %d",&n);
printf(" \n\t Process Burst Time\n");
for(i=1;i<=n;i++)
{
printf("\t P%d\t",i);
scanf(" %d",&bt[i]);
}
wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}
printf("\n\t Process Burst-Time Wait-Time Turnaround-Time\n");
for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t\t %d \t\t%d\n",i,bt[i],wt[i],tat[i]);
printf("\n\t Average Turn Around Time : %f \n",atat/n);
printf("\n\t Average Waiting Time : %f \n",awt/n);
}

________________
OUTPUT:
[pec292@localhost ~]$ cc firstcome.c
[pec292@localhost ~]$ ./a.out

First In First Out


No. Of Process : 3

Process Burst Time


P1 5
P2 3
P3 8

Process Burst-Time Wait-Time Turnaround-Time

P1 5 0 5

P2 3 5 8

P3 8 8 16

Average Turn Around Time : 9.666667

Average Waiting Time : 4.333333


________________
EX.NO:5b IMPLEMENTING SHORTEST JOB FIRST CPU SCHEDULING ALGORITHM

PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;

printf("\n\t\t Shortest Job First\n\t");

printf("\n\t No. Of Process : ");


scanf(" %d",&n);
printf(" \n\t Process Burst Time\n");
for(i=1;i<=n;i++)
{
printf("\t P%d\t",i);
scanf(" %d",&bt[i]);
p[i]=i;
}

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(bt[i]<bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;

t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}

printf("\n\t Executed In Following Order\n");

printf("\n\t Process Burst Time \n");


for(i=1;i<=n;i++)
printf("\n\t P%d \t %d",p[i],bt[i]);

wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;

for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}

printf("\n\t Process Burst-Time Wait-Time Turnaround-Time\n");


for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t\t %d \t\t%d\n",p[i],bt[i],wt[i],tat[i]);

printf("\n\t Average Turn Around Time : %f \n",atat/n);


printf("\n\t Average Waiting Time : %f \n",awt/n);
return 0;
}

________________
OUTPUT:
[kalai@localhost ~]$ cc sjb.c
[kalai@localhost ~]$ ./a.out

Shortest Job First

No. Of Process : 3

Process Burst Time


P1 5
P2 2
P3 8

Executed In Following Order

Process Burst Time

P2 2
P1 5
P3 8
Process Burst-Time Wait-Time Turnaround-Time

P2 2 0 2

P1 5 2 7
P3 8 7 15

Average Turn Around Time : 8.000000

Average Waiting Time : 3.000000


________________
EX.NO:5c IMPLEMENTING PRIORITY CPU SCHEDULING ALGORITHM

PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],pr[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;

printf("\n\t\t Priority Scheduling \n\t");

printf("\n\t No. Of Process : ");


scanf(" %d",&n);
for(i=1;i<=n;i++)
{
printf("\n\t For Process P%d : \n\tBurst Time : ",i);
scanf(" %d",&bt[i]);
printf("\t Priority : ");
scanf(" %d",&pr[i]);
p[i]=i;
}

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(pr[i]<pr[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;

t=p[i];
p[i]=p[j];
p[j]=t;

t=pr[i];
pr[i]=pr[j];
pr[j]=t;
}
}
}
printf("\n\t Executed In Following Order\n");

printf("\n\t Process Burst Time Priority\n");


for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t %d",p[i],bt[i],pr[i]);

wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;

for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}

printf("\n\t Process Burst-Time Wait-Time Turnaround-Time\n");


for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t\t %d \t\t%d\n",p[i],bt[i],wt[i],tat[i]);

printf("\n\t Average Turn Around Time : %f \n",atat/n);


printf("\n\t Average Waiting Time : %f \n",awt/n);
return 0;
}

________________
OUTPUT:
[pec292@localhost ~]$ cc priority.c
[pec292@localhost ~]$ ./a.out

Priority Scheduling

No. Of Process : 3

For Process P1 :
Burst Time : 2
Priority : 1

For Process P2 :
Burst Time : 4
Priority : 3
For Process P3 :
Burst Time : 6
Priority : 2

Executed In Following Order

Process Burst Time Priority

P1 2 1
P3 6 2
P2 4 3
Process Burst-Time Wait-Time Turnaround-Time

P1 2 0 2

P3 6 2 8

P2 4 8 12

Average Turn Around Time : 7.333333

Average Waiting Time : 3.333333


________________
EX.NO:5d IMPLEMENTING ROUND ROBIN CPU SCHEDULING ALGORITHM

PROGRAM:
#include <stdio.h>
main()
{
int i,f[10],bt[10],p[10],cbt[10],j,k,wt[10],tat[10];
int n,t,q,flag;
float atat,awt;

printf("\n\t\t Round Robin Scheduling \n\t");

printf("\n\t No. Of Process : ");


scanf(" %d",&n);

printf("\n\t Process Burst Time ");

for(i=1;i<=n;i++)
{
printf("\n\t P%d \t ",i);
scanf(" %d",&bt[i]);
cbt[i]=bt[i];
p[i]=i;
}

printf("\n\t Time Quantum : ");


scanf(" %d",&q);

for(i=1;i<=n;i++)
{
wt[i]=0;
tat[i]=0;
f[i]=0;
}

do
{
for(i=1;i<=n;i++)
{
if(f[i]==0)
{
if(bt[i]<=q)
{
f[i]=1;
t=bt[i];
}
else
t=q;

bt[i]=bt[i]-t;

for(j=1;j<=n;j++)
{
if((j==i)||(f[j]==1))
continue;

wt[j]=wt[j]+t;
}//for j
}//if finish
}//for i
flag=0;
for(i=1;i<=n;i++)
if(f[i]==0)
flag=1;
}while(flag==1);

atat=awt=0;
for(i=1;i<=n;i++)
{
tat[i]=wt[i]+cbt[i];
atat+=tat[i];
awt+=wt[i];
}
printf("\n\t Process Burst-Time Wait-Time Turnaround-Time\n");
for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t\t %d \t\t%d\n",p[i],cbt[i],wt[i],tat[i]);

printf("\n\t Average Turn Around Time : %f\n",atat/n);


printf("\n\t Average Waiting Time : %f\n",awt/n);

return 0;
}

OUTPUT:
[pec292@localhost ~]$ cc robin.c
[pec292@localhost ~]$ ./a.out

Round Robin Scheduling

No. Of Process : 3

Process Burst Time


P1 5

P2 8

P3 2

Time Quantum : 3

Process Burst-Time Wait-Time Turnaround-Time

P1 5 5 10

P2 8 7 15

P3 2 6 8

Average Turn Around Time : 11.000000

Average Waiting Time : 6.000000


________________
EX.NO:6 IMPLEMENTING PRODUCER – CONSUMER ALGORITHM USING SEMAPHORES

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}

return 0;
}

int wait(int s)
{
return (--s);
}

int signal(int s)
{
return(++s);
}

void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}

OUTPUT:
-bash-4.1$ cc sema1.c
-bash-4.1$ ./a.out
1.Producer
2.Consumer
3.Exit
Enter your choice:1

Producer produces the item 1


Enter your choice:1

Producer produces the item 2


Enter your choice:1

Producer produces the item 3


Enter your choice:1
Buffer is full!!
Enter your choice:2

Consumer consumes item 3


Enter your choice:2

Consumer consumes item 2


Enter your choice:2

Consumer consumes item 1


Enter your choice:2
Buffer is empty!!
Enter your choice: 3
________________
EX.NO:7a IMPLEMENTING INTER PROCESS COMMUNICATION

MSGSEND.C
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct mymsg {
long mtype;
char data[50];
}message;
main() {
int qid,len;
qid=msgget((key_t)107,IPC_CREAT|0666);
printf("input message? ");
scanf("%s",message.data);
message.mtype=1;
len=strlen(message.data);
if(msgsnd(qid,&message,len,0) == -1) {
printf("MESSAGE FAILED");
exit(1);
}
else
printf("MESSAGE SENT");
}

________________
MSGRECEIVE.C
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct mymsg {
long mtype;
char data[50];
}b;
int main()
{
int qid;
qid=msgget((key_t)107,0666);
if(qid==-1)
{
printf("MESSAGE FAILED");
exit(0);
}
if(msgrcv(qid,&b,13,1,IPC_NOWAIT) == -1)
printf("MESSAGE FAILED");
else
printf("MESSAGE RECIEVED %s \n",b.data);
}

OUTPUT:
[kalai@localhost ~]$ cc -o send msgsend.c
[kalai@localhost ~]$ ./send
input message? hi
MESSAGE SENT[kalai@localhost ~]$ cc -o rece msgreceive.c
[kalai@localhost ~]$ ./rece
MESSAGE RECIEVED welcome

________________
EX.NO:7b IMPLEMENTING SHARED MEMORY USING PIPES
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h>
#define MAXLINE 100
int main(void){
int n,i;
int fd[2];
pid_t pid;
char line[MAXLINE],msg[MAXLINE];
if (pipe(fd) < 0)
{
printf("pipe error");
return 0;
}
if ((pid = fork()) < 0)
{
printf("fork error");
return 0;
}
else if (pid > 0)
{ /* parent */
close(fd[0]);
for(i=0;i<5;i++)
{
printf("\ninput string? ");
scanf("%s",msg);
write(fd[1], msg , strlen(msg));
sleep(1);
}
}
else
{ /* child */
close(fd[1]);
for(i=0;i<5;i++)
{
n = read(fd[0], line, MAXLINE);
write(STDOUT_FILENO, line, n);
}
}
exit(0);
}

OUTPUT:
[kalai@localhost ~]$ cc share.c
[kalai@localhost ~]$ ./a.out

input string? hi
hi

input string? hello


hello
input string? 1
1
input string? 45
45
input string? @
@

________________
EX.NO: 8 IMPLEMENTING BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE

PROGRAM:
#include<stdio.h>
void main()
{
int k=0,output[10],d=0,t=0,ins[5],i;
int avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=0;
clrscr();
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for(i=0;i<rz;i++)
{
avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");
for(i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for(i=0;i <pno;i++)
{
P[i]=i;
printf("P[%d] ",P[i]);
for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}

printf("\nEnter the MAX matrix \n ");


for(i=0;i<rz;i++)
{
printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d] ",i);
for(j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}
printf("\n");
A: d=-1;
for(i=0;i <pno;i++)
{
count=0; t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz)
{
output[k++]=P[i];
for(j=0;j<rz;j++)
avail[j]+=allocated[t][j];
}
else
P[++d]=P[i];
}

if(d!=-1)
{
pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
}

OUTPUT:
[kalai@localhost ~]$ cc bank.c
[kalai@localhost ~]$ ./a.out

Enter the number of resources : 3

enter the max instances of each resources


a= 10
b= 5
c= 7

Enter the number of processes : 5

Enter the allocation matrix


a b c
P[0] 0 1 0
P[1] 2 0 0
P[2] 3 0 2
P[3] 2 1 1
P[4] 0 0 2
Enter the MAX matrix
a b c
P[0] 7 5 5
P[1] 3 2 2
P[2] 9 0 2
P[3] 2 2 2
P[4] 4 3 3

< P[1] P[3] P[4] P[0] P[2] >


________________
EX.NO: 9 DEADLOCK DETECTION

PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();

return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
} }
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
} }
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
} }}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}

for(i=0;i<n;i++)
{ finish[i]=0;
}

for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{

for(k=0;k<r;k++)

avail[k]+=alloc[i][j];

finish[i]=1;

flag=1;

if(finish[i]==1)

i=n;
}
} } } } }
j=0;
flag=0;

for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock
process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}}

________________
OUTPUT:
[kalai@localhost ~]$ cc deaddetect.c
[kalai@localhost ~]$ ./a.out
********** Deadlock Detection Algo ************
Enter the no of Processes 3
Enter the no of resource instances 3
Enter the Max Matrix
3 6 8
4 3 3
3 4 4
Enter the Allocation Matrix
3 3 3
2 0 3
1 2 4
Enter the available Resources
1 2 0
Process Allocation Max Available
P1 3 3 3 3 6 8 1 2 0
P2 2 0 3 4 3 3
P3 1 2 4 3 4 4

System is in Deadlock and the Deadlock process are


P0 P1 P2

You might also like