Displaying The File: EX - NO:1 Unix Commands AIM
Displaying The File: EX - NO:1 Unix Commands AIM
AIM :
To Study the UNIX commands.
BASIC COMMANDS
1. CAT:
SYNTAX:
Cat >file name
EXAMPLE:
Cat > rose
Hai
Hello
EXAMPLE:
Cat rose
Hai
Hello
2. MKDIR:
SYNTAX:
mkdir <directory name>
EXAMPLE:
Mkdir tom
3. PWD:
SYNTAX:
Pwd
EXAMPLE:
home/w/x1
4. CD
This command is used to change the working directory to the specified directory.
SYNTAX:
[1]
cd
EXAMPLE:
[HTS171@vc2839 ~]$ cd tom
[HTS171@vc2839 tom]$
5. RMDIR:
SYNTAX:
rmdir <directory name>
EXAMPLE:
Rmdir tom
6. CLEAR:
SYNTAX:
Clear
7. LISTING DIRECTORY:
SYNTAX:
ls
EXAMPLE:
[HTS171@vc2839 ~]$ ls
f1 f3 gee rose
EXAMPLE:
[HTS171@vc2839 ~]$ ls -a
. .bash_history .bash_profile f1 gee rose
.. .bash_logout .bashrc f3 .kde .zshrc
EXAMPLE:
[HTS171@vc2839 ~]$ ls -b
f1 f3 gee rose
EXAMPLE:
[HTS171@vc2839 ~]$ ls -d
EXAMPLE:
[2]
[HTS171@vc2839 ~]$ ls -l
total 28
-rw-rw-r-- 1 HTS171 HTS171 97 2007-12-18 02:47 f1
-rw-rw-r-- 1 HTS171 HTS171 97 2007-12-18 03:07 f3
drwxrwxr-x 2 HTS171 HTS171 4096 2007-12-18 03:08 gee
-rw-rw-r-- 1 HTS171 HTS171 0 2007-12-18 01:46 rose
EXAMPLE:
[HTS171@vc2839 ~]$ ls -m
f1, f3, gee, rose
(7.6) -n Lists long format showing UID and GID numbers instead of strings.
EXAMPLE:
[HTS171@vc2839 ~]$ ls -n
total 28
-rw-rw-r-- 1 616 616 97 2007-12-18 02:47 f1
-rw-rw-r-- 1 616 616 97 2007-12-18 03:07 f3
EXAMPLE:
[HTS171@vc2839 ~]$ ls -q
f1 f3 gee rose
(7.8) -r Lists files in reverse alphabetical order or oldest first when used with –t
EXAMPLE:
[HTS171@vc2839 ~]$ ls -r
rose gee f3 f1
(7.9) -t Lists file information sorted by most recent to oldest time stamp
EXAMPLE:
[HTS171@vc2839 ~]$ ls -t
gee f3 f1 rose
EXAMPLE:
[HTS171@vc2839 ~]$ ls -1
f1
f3
8. COPY:
This command is used to copy the contents of one file to another file.
SYNTAX:
cp filename new filename
EXAMPLE:
[3]
cp rose rose2
9. MOVE:
SYNTAX:
mv old filename new filename
EXAMPLE:
mv ram1 ram2
10. SH COMMAND
Starts up the default shell command interpreter
SYNTAX:
Sh
EXAMPLE:
[HTS171@vc2839 ~]$ sh
sh-3.2$
EXAMPLE:
[HTS171@vc2839 ~]$ stty -g
500:5:bd:8a3b:3:1c:7f:15:4:0:1:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0
:0:0:0
12. WC COMMAND:
This is used to count the number of words, lines and number characters in a file.
SYNTAX:
wc filename
[4]
EXAMPLE:
EXAMPLE:
[HTS171@vc2839 ~]$ wc file -l
4 file
[HTS171@vc2839 ~]$ wc -c file
46 file
[HTS171@vc2839 ~]$ wc -w file
6 file
13. DATE:
SYNTAX:
date
EXAMPLE:
[HTS171@vc2839 ~]$ date
Wed Dec 19 02:23:20 IST 2007
This command is used to display the string printed in this command line.
[5]
SYNTAX:
Echo”string”
EXAMPLE:
Echo hello
16. WHO:
This command is used to display the information about the current user.
SYNTAX:
Who
EXAMPLE:
[HTS171@vc2839 ~]$ who
VH946 pts/3 2007-12-18 02:09 (172.16.1.8)
VH940 pts/2 2007-12-18 02:10 (172.16.1.2)
VH947 pts/9 2007-12-18 02:14 (172.16.1.10)
17. GREP:
This command is used to search and print the specified pattern from file
SYNTAX:
grep[option]patternfilename
EXAMPLE:
Cat>count
Ab
ac
bc
Grep “a” count
Ab
Ac
18. SORT:
This command is used to sort the content of a file
SYNTAX:
Sort filename
EXAMPLE:
Cat >count
Ab
Ax
Ac
sort count
ab
ac
ax
19. READ:
This command is used to read the input values.
SYNTAX:
read varname
EXAMPLE:
read a
20. WHOAMI:
[6]
This command is used to print the details of the user.
SYNTAX:
Whoami
EXAMPLE:
[HTS171@vc2839 ~]$ whoami
HTS171
21. CALENDAR:
This command is used to display the calendar of any year and month.
SYNTAX:
Cal
EXAMPLE: [HTS171@vc2839 ~]$ cal 4 2007
April 2007
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
22. FINDCOMMAND:
This command locates file and folders.
SYNTAX:
finddirectoryname
EXAMPLE:
[HTS171@vc2839 ~]$ find gee
gee
gee/f5
23. BC COMMAND:
Performs interactive arithmetic processing and displays results
SYNTAX:
bc
EXAMPLE:
[HTS171@vc2839 ~]$ bc
bc 1.06
[7]
SYNTAX:
paste file1 file2
EXAMPLE:
[HTS171@vc2839 ~]$ cat file1
visual program
[HTS171@vc2839 ~]$ cat file2
Commands
unix
[HTS171@vc2839 ~]$ paste file1 file2
visual program commands
unix
CONDITIONAL STATEMENTS:
IF & IF-THEN-ELSE:
if Command
Executes command and checks for successful command completion status
then commands
Executes commands when if completes successfully
test command
Runs command if condition holds
elif command
Executes command and checks completion status after failure of preceding if
else command
Executes commands when if check does not complete successfully
fi
nds the if…then structure
SWITCH CASE:
case x in y command
xecutes command if string x is found in pattern y
FOR LOOP:
for x
Sets up a command loop where x is the number of positional parameters
in list
Specifies a list of the number of times to execute for
DO-WHILE LOOP:
do command
Executes commands each time for loop is entered
done command
Ends the for…do structure
while command
ets up a loop to execute while commands is true
do commands
xecutes commands each time while loop is entered
done command
nds the while…..do structure
[8]
UNTIL:
until command
ets up a loop to execute until commands is true
do command
xecutes commands each time until loop is entered
done command
nds the until…..do structure
while true
ets up an execution loop stopped when a condition is no longer true
until false
Sets up an execution loop stopped when a condition is false
EDITING COMMANDS:
COMMUNICATION COMMANDS:
1. MAILCOMMAND:
This sends a mail to another user.
SYNTAX:
Mailuser
EXAMPLE:
mail u
hh
^d
2. mailx Process mail interactively
5. tty Provides a specific terminal tty for a user logged in more than once
6. uname Lists the name of the current system you are logged into
-n Shows the communication node name for the sytem
-rv Dispalys the operating system release and version of the machine
9. line Specifies a tty line for a user logged in on more than one line
Vi MODE COMMANDS
[9]
ENTERING INPUT MODE
a Appending typed text into the buffer immediately after the position of the cursor
i Inserting typed text into the buffer immediately before the position of the cursor
A Appends material at the end of the current line
I Inserts material at the beginning of the current line
o Opens up a line above the current line places the cursor there, and puts vi in input mode
O Opens up a line below the current line places the cursor there, and puts vi in input mode
EXITING VI
:w Will write the contents of the editing buffer into the file
:wq Will write and quit
zz Which represents “last command” is equivalent
:x Exit and is also equivalent to :wq If you have made changes ,you regret you can cancel all the changes you have
made by quitting the editor without writing the buffer to a file
:q! “Quit and I really meant it”
MODIFYING TEXT:
DELETING TEXT:
[10]
d} Deletes to the beginning of the next paragraph
d]] Deletes to the beginning of the next section
dd Deletes the current line
2dd deletes two lines
dRETURN Deletes two lines
dG Deletes from the cursor to the end of the file
[11]
RESULT:
Thus the UNIX commands was executed and the output has been verified
AIM:
Parent
Child1
Child2
Child3
ALGORITHM
PROGRAM:
#include<stdio.h>
void main()
int pid1,pid2,pid3;
pid1=fork();
[12]
if(pid1==0)
pid2=fork();
if(pid2==0)
pid3=fork();
if(pid1==0&&pid2==0&&pid3==0)
OUTPUT:
[13]
RESULT:
Thus the process id, value were printed by using the fork and getpid system calls.
AIM:
ALGORITHM:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
int pid;
pid =fork();
if(pid<0)
[14]
exit(-1);
else if(pid==0)
execlp("/bin/ls","ls",NULL);
else
wait(NULL);
printf("Child Complete");
OUTPUT:
[cse1@minix ~]$./a.out
Child Complete
[15]
RESULT:
AIM:
To write a shell program to simulate file open, read and write operations.
ALGORITHM:
#include<stdio.h>
char *progname;
main(argc, argv)
int argc;
char *argv[ ];
char buf[BUFSIZE];
progname=argv[0];
[16]
if (argc != 3)
if (write(fd2, buf, n) != n)
exit(0);
OUTPUT:
hai
hello
welcome
hai
hello
welcome
RESULT:
[17]
Thus the file operations were simulated successfully.
AIM:
ALGORITHM:
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
main(argc,argv)
int argc;
char *argv[];
DIR *dirname;
dirname=opendir(argv[1]);
while(1)
[18]
rdir=readdir(dirname);
if(rdir==NULL)
closedir(dirname);
exit(0);
closedir(dirname);
exit(0);
OUTPUT:
RESULT:
[19]
EX.NO. 4 (b) SIMULATION OF GREP COMMAND
AIM:
ALGORITHM:
Step2: Using write() function enter the data to be stored in the file
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
FILE *fptr;
char ch;
int i;
char p[10],a[50];
fptr=fopen("input.txt","w");
ch = getchar();
while(ch!='$')
[20]
{
fprintf(fptr,"%c",ch);
ch = getchar();
fclose(fptr);
scanf("%s",p);
fptr=fopen("input.txt","r");
i=0;
while(!feof(fptr))
ch=getc(fptr);
if(ch!='\n')
a[i]=ch;
else
a[i]='\0';
if (strncmp(a,p,strlen(p))==0)
printf("%s\n",a);
i=-1;
i++;
fclose(fptr);
//getch();
[21]
OUTPUT:
wel
welcome
well
cse
vel
wel
welcome
well
[22]
RESULT:
AIM:
ALGORITHM:
1.Start theprogram
5.Enter the values in the pipe and check the sum of odd and even numbers inside the pipe and print the result
PROGRAM:
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
int main ()
int c;
int pid;
int fd[2];
int n,i=0;
int t1,even=0,odd=0,r;
pid=pipe(fd);
if(pid!=0)
exit(0);
[23]
c=fork();
if(c!=0)
close(fd[0]);
printf("Enterthevalues \n");
while(n!=-99)
scanf("%d",&n);
write(fd[1],&n,sizeof(n));
else if(c==0)
close(fd[1]);
while(n!=-99)
read(fd[0],&n,sizeof(n));
r=n%2;
if(r==0)
even=even+n;
odd=odd+n;
[24]
OUTPUT:
Enterthevalues
12 11 14 5 6
-99
[25]
RESULT:
AIM:
ALGORITHM :
PROGRAM:
PRODUCER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/shm.h>
main()
union pro
int val;
[26]
struct seminfo *_buf;
}arg;
printf("\n\t -----------------------------");
scanf("%d",&n);
id=shmget((key_t)125,(1*sizeof(int)),0766|IPC_CREAT|IPC_EXCL);
if(id == 0)
exit(0);
a=shmat(id,NULL,0700);
SID=semget((key_t)135,3,0700| IPC_CREAT|IPC_EXCL);
if(SID==0)
exit (0);
arg.array[0]=1;
arg.array[1]=0;
arg.array[2]=1;
t=semctl(SID,0,SETALL,arg);
*(a+0)=n;
*(a+10)= 0;
t=semctl(SID,0,GETALL,arg);
[27]
printf("\n\t -----------------------------");
for(i=2;i<=n+2;i++)
wait(SID,2);
wait(SID,0);
scanf("%d",&r);
*(a+i) = r ;
signal(SID,0);
signal(SID,1);
while(*(a+1)==0);
if(*(a+1)== -99);
shmdt(a);
shmctl(id,IPC_RMID,NULL);
semctl(SID,0,IPC_RMID,NULL);
printf("\n\n");
CONSUMER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<sys/shm.h>
main()
int i,j,r,SID,id,*a,n,t;
[28]
void wait(int id , short num);
union consum
int val;
}arg;
printf("\n\t -----------------------------");
id=shmget((key_t)125,(1*sizeof(int)),0766);
if(id==0)
exit(0);
a=(int *)shmat(id,NULL,0766);
SID=semget((key_t)135,3,0700);
printf("\n\n %d",SID);
if(SID==0)
exit(0);
n=*(a+0);
t=semctl(SID,0,GETALL,arg);
for(i=2;i<=n+2;i++)
[29]
{
wait(SID,1);
wait(SID,0);
printf("\n%d",*(a+i));
signal(SID,0);
signal(SID,2);
*(a+1)==0;
*(a+1)==-99;
shmdt(a);
int t;
op->sem_num= id;
op->sem_op= - 1;
t= semop(id,op,1);
int t;
op->sem_num = id;
op->sem_op = 1;
t= semop(id,op,1);
[30]
OUTPUT:
SEMAID : 196612
producer product
12 10 24 51 71
sh-3.2$ ./a.out
196612
12
10
24
51
71
[31]
RESULT:
Thus the producer consumer process was implanted and they were allowed to communicate via semaphore
successfully.
AIM:
ALGORITHH:
2.Create three Semaphore variable MUTEX,WRITE and RWCNT for mutual exclusion, Writer process for both
read and write count respectively..
4. During Reader process, issue WAIT to MUTEX and WRITE semaphores also increment RWCNT semaphore
then SIGNAL to MUTEX semaphore and display the file.
5. After reading a file, issue WAIT to MUTEX semaphore and decrement RWCNT semaphore then SIGNAL to
WRITE and MUTEX semaphores.
6. During Writing process, issue WAIT to WRITE semaphore then modify contents of the file. After updating the
file, issue SIGNAL to WRITE semaphore. Here no reading process is allowed.
PROGRAM:
#include<stdio.h>
#define down 0
#define up 1
semaphore mutex=1;
semaphore db=1;
semaphore win=up,pwin=up;
int readq[10],writeq[10];
int rc=0,pro=0,a=0,rl=0;
int rbeg=0,rend=0,wbeg=0,wend=0;
int rcount=0,wcount=0;
[32]
void readerin(int);
void readerout();
void writerin(int);
void writerout();
main()
char ch,prev='n';
printf("\n\t\t ***************");
printf("\n");
do
printf("\n");
ch=getchar();
putchar(ch);
switch(ch)
case 'r':
rcount++;
readerin(rcount);
break;
case 'w':
wcount++;
writerin(wcount);
[33]
break;
Default:
break;
if(prev=='r')
if(pro>0) {
a=a+1;
readerout();
if(prev=='w')
if(pwin==down)
writerout();
prev=ch;
pro=rc;
pwin=win;
}while(ch!='n');
printf("\n");
void readerin(int r)
if(win==down)
[34]
printf("\n Reader %d is put in queue",r);
writeq[wend]=wcount;
wend++;
else {
mutex=down;
rc++;
if(rc==1)
db=down;
mutex=up;
r=rc;
void readerout()
mutex=down;
rc--;
if(rc==0);
db=up;
mutex=up;
void writerin(int w)
if(win==down)
[35]
printf("\n Writer %d is Waiting",w);
writeq[wend]=w;
wend++;
} else
if(rc>0)
writeq[wend]=w;
wend++;
else
if((wend-wbeg)>0)
writeq[wend]=w;
wend++;
w=writeq[wbeg];
wbeg++;
win=down;
db=down;
void writerout()
[36]
int r,w;
db=up;
win=up;
if((rend-rbeg)>0)
do
r=readq[rbeg];
readerin(r);
rbeg++;
}while((rend-rbeg)>0);
else
if((wend-wbeg)>0)
w=writeq[wbeg];
wbeg++;
writerin(w);
OUTPUT:
READER-WRITER PROBLEM
Initial state
[37]
w
[cse1@minix ~]$
[38]
RESULT:
AIM:
ALOGORITHM:
PROGRAM:
#include<stdio.h>
char state[10];
void test(int k)
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e'))
state[k]='e';
void take(int i)
state[i]='h';
test(i);
[39]
}
void drop(int i)
state[i]='t';
test((i+4)%5);
test((i+1)%5);
main()
int i,n,opt=0;
printf("\n\t\t_____________\n");
for(i=0;i<5;i++)
state[i]='t';
for(i=0;i<5;i++)
printf("\n\n\t%d\t%c\t",i+1,state[i]);
while(opt<3)
printf("\n\tMenu\n1.hungry\n2.thinking\n3.exit\n");
scanf("%d",&opt);
switch(opt)
[40]
{
case 1:
scanf("%d",&n);
take(n-1);
break;
case 2:
printf("Thinking");
scanf("%d",&n);
drop(n-1);
break;
if(opt!=3)
printf("__________");
for(i=0;i<5;i++)
printf("\t\t%d\t%c\t\n\n",i+1,state[i]);
OUTPUT:
philosopher state
[41]
1 t
2 t
3 t
4 t
5 t
Menu
1. Hungry
2. Thinking
3. Exit
1 t
2 t
3 e
4 t
5 t
Menu
1. Hungry
2. Thinking
3. Exit
Thinking
1 t
[42]
2 t
3 e
4 t
5 t
Menu
1 Hungry
2. Thinking
3. Exit
1 t
2 t
3 e
4 h
5 t
Menu
1 Hungry
2. Thinking
3.Exit
Thinking
[43]
1 t
2 t
3 e
4 h
5 t
Menu
1. Hungry
2. Thinking
3. Exit
1 t
2 h
3 e
4 h
5 t
Menu
1. Hungry
2. Thinking
3. Exit
[44]
RESULT:
AIM:
ALOGORITHM:
type.(ie)
4c.calculate each processes need of resource instances for each resource type.(ie)
need[j][i]=max[j][i]-alloc[j][i].
[45]
alloc[j][i]=alloc[j][i]+need[j][i]
avail[i]=avail[i]-need[j][i]
PROGRAM:
#include<stdio.h>
#include<unistd.h>
struct process
int id,status,max[10],alloc[10],need[10];
};
int available[10];
void alloc(int,int);
main()
int i,j;
int p,r;
int res[10],req[10],rp;
int flag,flag1;
char ch;
scanf("%d",&p);
scanf("%d",&r);
for(i=0;i<r;i++)
[46]
scanf("%d",&res[i]);
available[i]=res[i];
for(i=0;i<p;i++)
pro[i].id=i;
pro[i].status=0;
for(j=0;j<r;j++)
scanf("%d",&pro[i].max[j]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
scanf("%d",&pro[i].alloc[j]);
for(i=0;i<r;i++)
for(j=0;j<p;j++)
available[i]-=pro[j].alloc[i];
for(i=0;i<r;i++)
printf("\t%d",available[i]);
while(1)
[47]
printf("\n Is there any request:(y/n)");
scanf("%s",&ch);
if(ch=='n')break;
scanf("%d",&rp);
for(i=0;i<r;i++)
scanf("%d",&req[i]);
flag=0;
for(i=0;i<r;i++)
if(req[i]<=available[i])flag++;
if(flag==r)
for(i=0;i<r;i++)
pro[rp].alloc[i]+=req[i];
available[i]-=req[i];
else if(flag!=r)
exit(0);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
[48]
pro[i].need[j]=pro[i].max[j]-pro[i].alloc[j];
for(i=0;i<p;i++)
printf("\nP%d",pro[i].id);
for(j=0;j<r;j++)
printf("\t%d",pro[i].max[j]);
printf("\n\nRESOURCE ALLOCATION\n");
for(i=0;i<p;i++)
printf("\nP%d",pro[i].id);
for(j=0;j<r;j++)
printf("\t%d",pro[i].alloc[j]);
printf("\n\nNEED\n");
for(i=0;i<p;i++)
printf("\nP%d",pro[i].id);
for(j=0;j<r;j++)
printf("\t%d",pro[i].need[j]);
printf("\n\nAVAILABLE RESOURCES\n");
for(i=0;i<r;i++)
printf("\t%d",available[i]);
for(i=0;i<p;i++)
allo(r,p);
flag1=0;
[49]
for(i=0;i<p;i++)
if(pro[i].status==1)
flag1++;
if(flag1==p)
else if(flag1!=p)
int i,j,flag;
for(i=0;i<p;i++)
flag=0;
if(pro[i].status==0)
for(j=0;j<r;j++)
if(pro[i].need[j]<=available[j])
flag++;
if(flag==r)
pro[i].status=1;
for(j=0;j<r;j++)
available[j]+=pro[i].alloc[j];
for(j=0;j<r;j++)
printf("\t%d",available[j]);
break;
[50]
}
OUTPUT:
For process p0
753
For process p1
322
For process p2
902
For process p3
222
For process p4
433
For process p0
010
For process p1
200
For process p2
302
For process p3
[51]
211
For process p4
002
Available resources
3 3 2
P0 7 5 3
P1 3 2 2
P2 9 0 2
P3 2 2 2
P4 4 3 3
Resource Allocation
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
Need
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Available Resources
3 3 2
P1 is executed
[52]
After execution
Available resource is 5 3 2
P3 is executed
After execution
Available resource is 7 4 3
P0 is executed
After execution
Available resource is 7 5 3
P2 is executed
After execution
Available resource is 10 5 5
P4 is executed
After execution
Available resource is 10 5 7
System is in safe state
[53]
RESULT:
Thus the deadlock avoidance using banker’s algorithm was executed successfully
AIM:
To write a shell program to solve mutual exclusion problem by using Dekker’s Algorithm
ALGORITHM:
3. Every process checks the other flag after setting its own. If both are set, the turn variable is used to allow only one
process to proceed.
4. The trun variable is only considered when both processes are using, or trying to use, the resource.
5. The process with the turn will (eventually) discover the other flag free and will proceed.
PROGRAM:
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int j=1-i;
s2[i]=1;
while(s2[j])
if (s2[2]==j)
s2[i]=0;
while(s2[2]==j);
[54]
s2[i]=1;
int j=1-i;
s2[2]=j;
s2[i]=0;
int i=0;
do
tmp[i++]=s1[in++];
while(s1[in-1]!='\0');
int len,i;
len=strlen(tmp);
for(i=0;i<len;i++)
s1[in++]=tmp[i];
s1[in++]='\0';
[55]
{
int len,i;
char t;
len=strlen(tmp);
for(i=0;i<len-1;i+=2)
if(tmp[i]!='\0')
t=tmp[i];
tmp[i]=tmp[i+1];
tmp[i+1]=t;
int len,i,j=0;
char t[20];
len=strlen(tmp);
for(i=len-1;i>=0;i--)
t[j++]=tmp[i];
t[j]='\0';
strcpy(tmp,t);
main()
char *s1;
int *s2;
[56]
char tmp[20];
int id1,id2,pid,i,j,in=0,inptr=0,cnt=0,n;
scanf("%d",&n);
id1=shmget(IPC_PRIVATE,50*sizeof(char),0700|IPC_CREAT|IPC_EXCL);
if(id1==-1)
exit(1);
id2=shmget(IPC_PRIVATE,4*sizeof(int),0700|IPC_CREAT|IPC_EXCL);
if(id2==-1)
exit(1);
s1=(char *)shmat(id1,NULL,0700);
if(s1==(char *)-1)
exit(1);
s2=(int *)shmat(id2,NULL,0700);
if(s2==(int *)-1)
exit(1);
[57]
s2[0]=0;
s2[1]=0;
s2[2]=0;
printf("\n\t--------------------\n");
for(i=0;i<n;i++)
scanf("%s",tmp);
for(j=0;j<strlen(tmp);j++)
s1[in]=tmp[j];
in++;
s1[in]='\0';
in++;
pid=fork();
if(pid!=0)
do
dekker_start(s2,0);
read_string(s1,tmp,inptr);
swap(tmp);
write_string(s1,tmp,inptr);
[58]
read_string(s1,tmp,inptr);
printf("\t%s\n",tmp);
inptr=inptr+strlen(tmp)+1;
dekker_exit(s2,0);
cnt++;
while(cnt!=n);
wait(&i);
cnt=0;
in=0;
do
read_string(s1,tmp,in);
in=in+strlen(tmp)+1;
cnt++;
}while(cnt!=n);
if(shmdt(s1)==-1)
exit(1);
if(shmdt(s2)==-1)
exit(1);
if(shmctl(id1,IPC_RMID,0)==-1)
[59]
{
exit(1);
if(shmctl(id2,IPC_RMID,0)==-1)
exit(1);
else
do
dekker_start(s2,1);
read_string(s1,tmp,inptr);
reverse(tmp);
write_string(s1,tmp,inptr);
read_string(s1,tmp,inptr);
printf("\t%s\n",tmp);
inptr=inptr+strlen(tmp)+1;
dekker_exit(s2,1);
cnt++;
while(cnt!=n);
[60]
OUTPUT:
--------------------
elppa
egnaro
ognam
leppa
geanor
goanm
[vtu1011@minix ~]$
[61]
RESULT:
AIM:
To implement FIFO page replacement algorithm for the given reference string.
ALGORITHM:
FIFO page replacement algorithm replaces the oldest page that is brought into memory.
5.Print the page frame allocation of the reference string for each iteration.
PROGRAM:
#include <stdio.h>
int main()
int main_mem,cur=0,i,j,fault,page[100],page_mem[100],flag,num;
for(i=0;i<100;i++)
page_mem[i]=-2;
scanf("%d",&main_mem);
[62]
scanf("%d", &num);
for(i=0;i<num;i++)
scanf("%d", &page[i]);
printf("\n\t\tFIFO Paging\n\n");
for(i=0;i<main_mem;i++)
for(i=0;i<num;i++)
for(j=0;j<main_mem; j++)
if(page[i]==page_mem[j])
flag=1;
break;
if(!flag)
page_mem[cur]=page[i];
fault++;
printf("\n\n");
for (j=0;j<main_mem;j++)
printf("\t%d", page_mem[j]);
printf("\n\n");
if(!flag &&cur<main_mem-1)
cur++;
[63]
else if(!flag)
cur=0;
flag=0;
OUTPUT:
[64]
Enter page reference: 4
FIFO Paging
1 -2 -2
1 2 -2
1 2 3
1 2 3
1 2 3
1 2 3
4 2 3
4 5 3
4 5 6
7 5 6
[65]
RESULT:
AIM:
ALGORITHM:
First fit placement algorithm allocates the first free block in main memory the process fits
1.Enter the number of free blocks, the base address & block size of each block in the main memory.
Best fit placement algorithm searches the entire list of free blocks in main memory and allocates the smallest free
block that is big enough for the process to be fitted.
1.Enter the number of free blocks,the base address & block size of each block in the main memory.
3.Sort the free blocks in the ascending order of the block size.
PROGRAM:
#include<unistd.h>
struct process
int size,status,allo;
[66]
}p[10];
struct partition
int id,size,status,frag;
}pa[10];
int pro,part;
int frag1=0;frag2=0,frag3=0;
void first();
void best();
void worst();
void getdata();
void reset();
main()
int n;
scanf("%d",&part);
scanf("%d",&pro);
getdata();
do
scanf("%d",&n);
if(n==1)
first();
else if(n==2)
[67]
best();
else if(n==3)
worst();
}while(n <=3);
if(frag1<frag2)
if(frag1<frag3)
else
else
if(frag2<frag3)
else
void getdata()
int i,k;
for(i=0;i<pro;i++)
scanf("%d",&p[i].size);
p[i].status=0;
for(k=0;k<part;k++)
[68]
printf("\n size of %d partition :" ,k);
scanf("%d",&pa[k].size);
pa[k].id=k;
pa[k].status=0;
pa[k].frag=0;
void reset()
int i;
for(i=0;i<pro;i++)
p[i].status=0;
p[i].allo=-1;
for(i=0;i<part;i++)
pa[i].status=0;
pa[i].frag=-1;
void first()
int i,j;
for(i=0;i<pro;i++)
for(j=0;j<part;j++)
[69]
{
p[i].status=1;
pa[j].status=1;
pa[j].frag=pa[j].size-p[i].size;
p[i].allo=pa[j].id;
break;
frag1=0;
for(i=0;i<part;i++)
if (pa[i].status==1)
frag1+=pa[i].frag;
else if(pa[i].status==0)
frag1+=pa[i].size;
for(i=0;i<pro;i++)
if(p[i].status==1)
printf("\n\t%d\t%d\t\t%d\t%d",(i),p[i].size,p[i].allo,pa[p[i].allo].size);
for(i=0;i<pro;i++)
if(p[i].status==0)
reset();
void best()
[70]
{
int i,j;
for(i=0;i<part;i++)
t[i].id=pa[i].id;
t[i].size=pa[i].size;
t[i].status=0;
t[i].frag=0;
for(i=0;i<part;i++)
for(j=0;j<=(part-1);j++)
if(t[j].size>t[j+1].size)
t1=t[j].size;
t[j].size=t[j+1].size;
t[j+1].size=t1;
t2=t[j].id;
t[j].id=t[j+1].id;
t[j+1].id=t2;
for(i=0;i<pro;i++)
for(j=0;j<part;j++)
if((p[i].status==0)&&(t[j].status==0)&&(p[i].size<=t[j].size))
[71]
{
p[i].status=1;
t[j].status=1;
t[j].frag=t[j].size-p[i].size;
p[i].allo=j;
break;
frag2=0;
for(i=0;i<part;i++)
if(t[i].status==1)
frag2 +=t[i].frag;
else if(pa[i].status==0)
frag2=t[i].size;
for(i=0;i<pro;i++)
if(p[i].status==1)
printf("\n\t%d\t%d\t\t%d\t%d",i,p[i].size,t[p[i].allo].id,t[p[i].allo].size);
for(i=0;i<pro;i++)
if(p[i].status==0)
reset();
void worst()
[72]
{
int min,t1,t2;
int i,j;
for(i=0;i<part;i++)
t[i].id=pa[i].id;
t[i].size=pa[i].size;
t[i].status=0;
t[i].frag=0;
for(i=0;i<part;i++)
for(j=0;j<(part-1);j++)
if(t[j].size<t[j+1].size)
t1=t[j].size;
t[j].size=t[j+1].size;
t[j+1].size=t1;
t2=t[j].id;
t[j].id=t[j+1].id;
for(i=0;i<pro;i++)
if((p[i].status==0)&&(t[j].status==0)&&(p[i].size<=t[j].size))
p[i].status=1;
t[j].status=1;
t[j].frag=t[j].size-p[i].size;
[73]
p[i].allo=j;
break;
frag3=0;
for(i=0;i<part;i++)
if(t[i].status==1)
frag3+=t[i].frag;
else if(pa[i].status==0)
frag3+=t[i].size;
for(i=0;i<pro;i++)
if(p[i].status==1)
printf("\n\t%d\t%d\t\t%d\t%d",i,p[i].size, t[p[i].allo].id,t[p[i].allo].size);
for(i=0;i<pro;i++)
if(p[i].status==0)
reset();
OUTPUT:
[74]
Size of 0 process :212
1 – First Fit
2 – Best Fit
3 – Worst Fit
Enter ur choice :1
First Fit
0 212 1 500
1 417 4 600
2 112 2 200
1 – First Fit
2 – Best Fit
3 – Worst Fit
Enter ur choice :2
Best Fit
0 212 3 300
[75]
1 417 1 500
2 112 2 200
3 426 4 600
1 – First Fit
2 – Best Fit
3 – Worst Fit
Enter ur choice :3
Worst Fit
0 212 4 600
1 417 1 500
2 112 3 300
1 – First Fit
2 – Best Fit
3 – Worst Fit
Enter ur choice :4
[76]
RESULT:
AIM
To simulate paging by writing a c program using LINUX.
ALGORITHM
1. Start the program
4. Get the page size, page number and the value for displacement.
PROGRAM:
#include<stdio.h>
main()
int i,f,d,ps,pn,pt[4][2],pa,x;
printf("i f\n");
for(i=1;i<=4;i++)
for(f=1;f<=2;f++)
#include<stdio.h>
main()
int i,f,d,ps,pn,pt[4][2],pa,x;
[77]
printf("i f\n");
for(i=1;i<=4;i++)
for(f=1;f<=2;f++)
scanf("%d",&pn);
pt[i][f]=pn;
printf("**********\n");
scanf("%d",&ps);
scanf("%d",&d);
scanf("%d",&pn);
for(i=1;i<=4;i++)
if(pn==pt[i][1])
x=pt[i][2];
pa=(ps*x)+d;
[78]
OUTPUT:
i f
1 5
2 6
3 4
4 1
**********
pa=(3*5)+2
Physical Address: 17
pa=(3*3)+2
Physical Address: 11
[exam1@localhost ~]$
[79]
RESULT:
Thus the Linux kernel program to simulate paging was executed successfully.
AIM
To write a c program to simulate segmentation using LINUX.
ALGORITHM
1.Get the number of entities in the segment table.
2. Get the values for segment table, ie seg-no, limit, base.
3. Get the segment number.
4. If it is valued get the byte to be referenced and that byte is within the limit, then
physical address is calculated.
5. If that byte is not within the limit, then print “ cannot refer this address”.
6. If the segment no is not valid then print “ Invalid segment no”.
7. Stop the program.
PROGRAM
#include<stdio.h>
main()
int n,x,y,sc,pa,ba,ab,li,b[10],l[10],i[10],j;
y=-1;
scanf("%d", &n);
for(x=0;x<n;x++)
scanf("%d", &i[x]);
scanf("%d", &l[x]);
scanf("%d", &b[x]);
scanf("%d", &sc);
[80]
for(j=0;j<n;j++)
if(i[j]==sc)
y=1;
ba=b[j];
li=l[j];
}}
if(y<0)
exit(0);
scanf("%d", &ab);
if(ab>li)
exit(0);
pa=ba+ab;
printf("%d\n",pa);
[81]
OUTPUT
[staff@linux-router staff]$ ./a.out
Segno-limit base
1 100 50
2 200 160
3 500 470
4 1400 920
100
260
[staff@linux-router staff]$ .
RESULT:
Thus the Linux kernel program to simulate segmentation was executed successfully.
[82]