Professional Documents
Culture Documents
Unix & Os Manual
Unix & Os Manual
JYOTHISHMATHI INSTITUTE OF
TECHNOLOGY & SCIENCE
NUSTULAPUR, KARIMNAGAR
LAB MANUAL OF
UNIX & OS LAB (IT)
(III B.Tech. II SEM)
INDEX
SNO
PROGRAM NAME
2
3
4
5
6
8
9
10
11
12
13
PAGE
NO
1
2
3
4
5
6-7
8-10
11
12
13-14
15-16
17-18
19-20
O.S. PROGRAMMES
S.NO
1
2
3
4
5
6
7
8
9
10
11
12
13
PROGRAM NAME
Write a C program for ROUND ROBIN CPU scheduling
algorithm
Write a C program for SJF CPU scheduling algorithm
Write a C program for FCFS CPU scheduling
algorithm
Write a C Program for priority CPU scheduling
algorithm.
Write a C Program for sequential file allocation.
Program for indexed file allocation strategy.
Program for linked file allocation strategy.
Write a C Program for MVT first fit.
Write a C Program for MFT .
Write a C Program for MVT best fit.
Program for bankers algorithm
Write a C program for FIFO page allocation
algorithm.
Write a C program for LRU page replacement
algorithm.
PAGE
NO
20-21
22-23
24
25-26
27-28
29-30
31-32
33
34
35-36
37-39
40-41
42-44
Program 1:
AIM:
Write a shell script to print the multiplication table for the
given number.
Algorithm:
1.Start.
2.Read n.
3.i=1.
4.f = n * i.
5.i = i + 1.
6.Display f.
7.Repeat 4,5,6 steps until i = 10.
8.End.
Source Code:
#! /bin/sh
echo enter the number
read n
for i in 1 2 3 4 5 6 7 8 9 10
do
x= `expr $n \* $i`
done
Input:
enter the number
6
Output:
6*1=6
6 * 2 = 12
Program 2:
AIM:
Write a shell script that copies multiple files into a directory.
Algorithm:
1.Start.
2.Read *.c files into i.
3.Copy $i to the root directory.
4.Repeat 2,3 steps until all values of $i..
5.End.
Source Code:
#! /bin/sh
for i in `ls *.c`
do
cp $i /root
done
echo file are copied into root
Output:
file are copied into root
Program 3 :
AIM:
Write a shell script to find no. of words and characters in a
given file.
Algorithm:
1. Start.
6
cc = 0,w=0.
give file name as commandline argument (i.e $1).
for i in `cat $1`.
w = w + 1.
c = expr $i : *.
cc = cc + c.
Repeat 4,5,6,7 steps until eof.
End.
Source Code:
# /bin/sh
w=0
cc=0
for i in cat $1
do
j= $i
echo $j
w=`expr $w + 1`
c=`expr $j:.*`
cc = `expr $cc + $c`
done
echo no.of characters $cc
echo no.of words $w
Input:.
./a.out sun
Output:
no. of characters 56
no. of words 11
Program 4:
AIM:
Write a shell script to display all files in a given directory.
Algorithm:
1.Start.
2.Read i value from ls sss.
3.Display i value .
4.Repeat above 2 steps until end of directory.
5.End.
Source Code:
#! /bin/sh
for i in `ls sss`
do
echo $i
done
Output:
abc.txt
ss.c
Program 5:
AIM:
Write a shell script to perform simple calculator.
Algorithm:
1.Start
2.Read a and b
3.Read op
4.Depending on value of operator perform
Operation.
5.End
Source Code:
#! /bin/sh
echo enter the value for a
read a
echo enter the value for b
read b
case
Input:
enter the value for a
3
enter the value for b
6
enter the operator
*
Output:
18
Program 6a:
AIM:
Write a C program to count no. of blanks, characters, lines
using standard i/o function.
Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
Start.
open the file using fopen( ) function in r mode
ch=fgetc(fp) (to read character by character)
if ch = or \n
b=b+1.
if ch = \n
l=l+1.
c=c+1.
Repeat 3,4,5&6 steps until ch = eof
End
Source Code:
#include<stdio.h>
int main( )
{
10
Input:
./a.out sss.txt
Output:
no.of blanks 5
n.of lines 2
no.of characters 36
Program 6b:
AIM:
Write a C program to count no. of blanks, characters, lines
using system calls.
Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
Start.
open the file using open( ) system call in O_RDONLY
rc=read(fd,buf,5) (to read characters )
if ch = or \n
w=w+1.
if ch = \n
l=l+1.
c=c+1.
Repeat 3,4,5&6 steps until ch = eof
End
11
Input:
./a.out sss.txt
Output:
no. of blanks 5
no. of lines 2
no. of characters 36
Program 7a:
AIM:
Write a C program to illustrate the mv command using
system Calls
Algorithm:
1. open one existed file and one new open file using open( )
system call
2. read the contents from keyboard using read( )
3. write these contents into file using write()
4. repeat 2,3 steps until eof
5. close 2 file using fclose( ) system call
6. delete existed file using using unlink( ) system
12
Input:
hai hello
A friend in need is a friend in deed
^z
Output:
hai hello
A friend in need is a friend in dee
Program 7b :
AIM:
Write a program to illustrate ls command using system calls
Algorithm:
1.
2.
3.
4.
5.
6.
Start.
open directory using opendir( ) system call.
read the directory using readdir( ) system call.
print dp.name and dp.inode .
repeat above step until end of directory.
End
13
output:
mul.sh
division.c
process.c
12344
12345
12346
Program 7c:
AIM:
Write a C program to illustrate the mv command using
system calls.
Algorithm:
1. Start
2. open an existed file and one new open file using open()
14
system call
read the contents from existed file using read( ) system
call
write these contents into new file using write system
call using write( ) system call
repeat above 2 steps until eof
close 2 file using fclose( ) system call
delete existed file using using unlink( ) system
End.
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{
int fd,fd1,rc;
char ch[5];
fd=open(sss,O_RDONLY );
fd1= open(rrr,O_WRONLY | O_CREATE);
while((rc=read(fd,ch,5))>0)
write(fd1,ch,rc);
close fd1;
close fd;
unlink(fd);
printf( file is copied );
}
Output:
file is copied
Program 8:
AIM:
Write a C program to illustrate the stat system calls
Algorithm:
1. Declare s and st of type struct stat.
2. Use stat system call by specifying sss.c file
15
Source Code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int s;
struct stat st;
s=stat(sss.c,&st);
if(s<0)
{
printf(use of stat() is unsuccessful );
exit(0);
}
printf(inode number of sss.c file is %d,st.st_ino);
printf(no.of links to sss.c is %d, st.st_nlink );
printf(file permissions sss.c is %d,st.st_mode);
printf(size of sss.c file is ,st.st_size);
printf(last access time of sss.c %d,st.st_atime );
}
Output::
Output
inode number of sss.c file is 12345
no.of links to sss.c file is 2
file permissions of sss.c file is 666
size of sss.c file is 8
last access time of sss.c file is 7:40
Program 9:
AIM:
Write a C program that
that illustrates the creation of child
process using fork( ) system call
16
Algorithm:
1.
2.
3.
4.
5.
6.
7.
8.
Start
Declare pid
create new process using fork( ) system call
If pid!=0 then
Display parent process getpid(),getppid().
Else
Display child process getpid().getppid().
End
Source code:
#include<stdio.h>
int main( )
{
printf(original process with pid %d ppid %d\n,
getpid() ,getppid());
pid=fork();
if(pid!=0)
printf(parent process with pid %d ppid %d \n,
getpid(),getppid());
else
{
sleep(5);
printf(child process with pid %d ppid %d\n,
getpid(),getppid());
}
printf( pid %d terminates ,getpid());
}
Output:
original process with pid 3456 and ppid 3525
child process with pid 3457 and ppid 3456
pid 3457 terminates
parent process with pid 3456 and ppid 3525
pid 3456 terminates
Program 10:
AIM:
Write a C program that illustrates file locking.
17
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char* argv[])
{
struct flock f1={f_wrlck,seek_set,0,0,0);
int fd;
f1.l_type=f_rdlck;
if((fd=open(rr.txt,o_rdwr))==-1)
{
perror(open);
exit(1);
}
printf(press<return> to try to get lock:);
getchar();
printf(trying to get lock):
if(fnctl(fd,f_setlkw,&f1)==-1)
{
perror(fcntl);
exit(1);
}
printf(got lock \n);
printf(press <return> to release lock:);
getchar( );
f1.l_type=f_unlck;
if(fcntl(fd,f_setlk,&f1)==-1)
{
perror(fcntl);
exit(1);
}
18
Output:
press<return> to try to get lock
trying to get lock
press <return> to release lock
unlocked
19
Program 11:
AIM:
Write a C program that implements producer consumer
problem using semaphore system calls
Algorithm:
1.
2.
3.
4.
5.
6.
Start
create semaphore using semget( ) system call
if successful it returns positive value
create two new processes
first process will produce
until first process produces second process cannot
consume
7. End.
Source code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define num_loops 2
int main(int argc,char* argv[])
{
int sem_set_id;
int child_pid,i,sem_val;
struct sembuf sem_op;
int rc;
struct timespec delay;
clrscr();
sem_set_id=semget(ipc_private,2,0600);
if(sem_set_id==-1)
{
perror(main:semget);
exit(1);
}
printf(semaphore set created,semaphore setid%d\n ,
sem_set_id);
20
case 0:
for(i=0;i<num_loops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(producer:%d\n,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(consumer:%d\n,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,null);
}
}
break;
}
return 0;
}
Output:
semaphore set created
semaphore set id 327690
producer: 0
consumer:0
producer:1
21
Program 12:
AIM:
Write a C program to illustrate inter process communication
using shared memory system calls.
Algorithm:
1.
2.
3.
4.
Start
create shared memory using shmget( ) system call
if success full it returns positive value
attach the created shared memory using shmat( ) system
call
5. write to shared memory using shmsnd( ) system call
6. read the contents from shared memory using shmrcv( )
system call
7. End .
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string.h>
#include<sys/shm.h>
#define shm_size 1024
int main(int argc,char * argv[])
{
key_t key;
int shmid;
char *data;
int mode;
if(argc>2)
{
fprintf(stderr,usage:stdemo[data_to_writte]\n);
exit(1);
}
if((shmid=shmget(key,shm_size,0644/ipc_creat))==-1)
{
perror(shmget);
exit(1);
22
Input:
#./a.out swarupa
Output:
writing to segment swarupa
23
Program 13:
AIM:
Write a C program to
( a ) create message queue
( b ) write to message queue
( c ) read from message queue
Algorithm:
1. Start
2. create message queue using msgget( ) system call
3. while creating message queue it returns id into file
descriptor
4. print the message queue id
5. if successful rite into message queue using msgsnd()
system call
6. read the contents from message queue using msgrcv( )
system call
7. End.
Source Code:
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
int main(int argc,char* argv[])
{
int q_id;
if(q_id=msgget(ipc_private,0600)==-1)
perror(msgget);
struct msgbuf
{
long mtype;
char mtext[20];
}
struct msgbuf* msg;
24
Output:
message queue created 0
message placed on the queue successfully
msgrcv:received message:
mtype: 0
mtext: hello world
25
LAB MANUAL OF
26
Program 1:
AIM:
Write a C program for ROUND ROBIN CPU scheduling algorithm.
Source Code:
#include<stdio.h>
main()
{
int s[10],p[10],n,i,j,wi=0,w[10],t[10], st[10],tq,tst=0;
int tt=0,tw=0;
float aw at;
printf("enter no.of process");
scanf("%d",&n);
printf("\n enter time quanum");
scanf("%d",&tq);
printf("\n enter process&service time");
for(i=0;i<n;i++)
scanf("%d%d",&p[i],&s[i]);
27
printf("process\tst\twt\ttt");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%d",p[i],st[i],w[i],t[i]);
printf("awt=%d",aw);
printf("att=%d",at);
}
Input:
enter no of process 3
enter time quantum 2
enter process&service time
1
4
2
6
3
28
Output:
process
1
2
3
st
wt
tt
4
6
2
4
6
4
8
12
6
Awt = 4.000000
att = 8.000000
Program 2:
AIM:
Write a C program for SJF CPU scheduling algorithm
Source Code:
#include<stdio.h>
main()
{
int i,j,bt[10],n,pt[10],wt[10],tt[10],t,k,l,w1=0,t1=0;
float at,aw;
printf(enter no of jobs);
scanf(%d,&n);
printf(enter burst time);
for(i=0;i<n;i++)
scanf(%d,&bt[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
29
Input:
Enter no of jobs
4
Enter burst time
5
12
8
20
Output:
Bt
5
12
8
20
wt
0
5
13
25
tt
5
13
25
45
30
Program 3:
AIM:
Write a C program for FCFS CPU scheduling algorithm
Source Code:
include<stdio.h>
main()
{
int i,j,bt[10],n,pt[10],wt[10],tt[10],t,k,l,w1=0,t1=0;
float at,aw;
printf(enter no of jobs);
scanf(%d,&n);
printf(enter burst time);
for(i=0;i<n;i++)
scanf(%d,&bt[i]);
31
output:
bt
wt
tt
12
0
12
8
12
20
20
20
40
aw=10.666670
at=24.00000
Program 4:
AIM:
Program
Write a C Progra
m for priority CPU scheduling algorithm.
Source Code:
#include<stdio.h>
main()
{
int i,j,bt[10],n,pt[10],wt[10],tt[10],t,k,l,w1=0,t1=0;
float at,aw;
printf(enter no of jobs);
32
Input:
Enter no of jobs
4
Enter bursttime
10
2
4
7
Enter priority values
4
33
Output:
Bt
priority
wt
tt
4
2
7
10
1
2
3
4
0
4
6
13
4
6
13
23
aw=5.750000
at=12.500000
Program 5:
AIM:
Write a C Program for sequential file allocation.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
34
Input:
enter starting block & length of files
4
5
Output:
file not allocated (y-1/n-0)
35
1
1
1
1
1
Program 6:
AIM:
Write a C Program for indexed file allocation strategy.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int f[50],i,k,j,index[50],n,c,count=0;
36
Input:
enter index block
3
enter no of files on index
37
Output:
Allocated
file indexed
4->5:1
4->6:1
4->7:1
4->8:1
index is already allocated
if u enter one more block(1/0)
0
Program 7:
AIM:
Write a C Program for linked file allocation strategy.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
38
Input:
enter how many blocks already allocated
5
39
Output:
4->1
5->1
6->1
7->file is already allocated
8->1
9->file is already allocated
10->file is already allocated
11->1
12->1
13->1
14->file is already allocated
15->1
16->1
17->1
Program 8:
AIM:
Write a C Program for MVT first fit.
Source Code:
#include<stdio.h>
#include<conio.h>
40
Input:
Enter no of blocks3
Enter block size100
200
300
Enter no of process2
Enter process123
243
Output:
Total fragmentation=134
Program 9:
AIM:
Write a C Program for MFT .
41
Program 10:
AIM:
Write a C Program for MVT best fit.
42
Input:
enter no of blocks
3
43
Output:
total fragmentation 15
44
Program 11:
AIM:
Program for bankers algorithm
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int
i,j,a=0,b=0,c=0,f[10],t[10][10],al[10][10],ta[10][10];
int a1[10][10], max[10][10], n[10][10], n1,p,k=0;
printf(\n enter no.of resources);
scanf(%d,n1);
printf(\nenter the max no .of resources for each type);
for(i=0;i<n1;i++)
scanf(%d,&t[b][i]);
printf(\nenter no .of process);
scanf(%d,&p);
printf(\nenter allocation resources);
for(i=0;i<p;i++)
{
f[i]=0;
for(j=0;j<n1;j++)
scanf(%d,&a1[i][j]);
}
for(i=0;i<p;i++)
for(j=0;j<n1;j++)
{
if(a1[i][j]<=t[b][j])
{
t[b][j]+=a1[i][j];
continue;
}
else
printf(\n wrong resourcesallocation);
printf(\n chance of deadlock occurrence after
allocation);
for(j=0;j<n1;j++)
printf(%d,a1[b][j]);
printf(\n enter the max resources for every process);
for(i=0;i<p;i++)
45
46
Input:
Output:
total available resources after allocation
3 3 2
enter the max resources for every process
7 5 3 3 2 2 9 0 2 2 2 2 4 3 3
needed resources for every process to start execution
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
Safe sequence ,the sequence of process to complete their
execution
Procee 2 execution started
& completed
Procee 4 execution started
& completed
Procee 5 execution started
& completed
Procee 1 execution started
& completed
Procee 3 execution started
& completed
47
Program 12:
AIM:
Write a C program for FIFO page allocation algorithm.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],b[5],n,t,i,c=0,k=0,j=0,m,x;
printf("\nenter no of requests");
scanf("%d",&n);
printf("\nenter no of frames");
scanf("%d",&t);
printf("enter requests one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(k<t)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==k)
{
b[k]=a[i];
k++;
m=i;
}
}
}
for(i=m;i<n;i++)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==3)
{
48
Input:
enter no of requests
13
enter no of frames
3
enter requests one by one
2
5
3
2
0
1
3
5
4
2
2
3
4
Output:
no of pagefaults
6
49
Program 13:
AIM:
Write a C program for LRU page replacement algorithm.
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a[50],b[40],c[15],n,f,i,y,s,p=0,k=0,l=0,m,x,g;
printf("\nenter no of requests");
scanf("%d",&n);
printf("\nenter no of frames");
scanf("%d",&f);
printf("enter requests one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(k<f)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==k)
{
b[k]=a[i];
c[k]=a[i];
k++;
m=i;
}
}
}
for(i=m;i<n;i++)
{
printf("%d",a[i]);
for(x=0;x<f;x++)
50
for(x=0;x<f&&l<=f;x++)
if(a[g]==b[x]&&c[x]==b[x])
{
y=c[x];
c[x]=-1;
l++;
}
if(l==f)
{
for(s=0;s<f;s++)
if(y==b[s])
b[s]=a[i];
}
if(l<f)
for(s=0;s<f;s++)
if(c[s]!=-1)
{
b[s]=a[i];
break;
}
for(s=0;s<f;s++)
c[s]=b[s];
}
for(s=0;s<f;s++)
printf("%4d",b[s]);
printf("\n");
}
printf("\n no of pagefaults=%d",p);
getch();
}
51
Input:
enter no of requests
13
enter no of frames
3
enter requests one by one
2 5 3 2 0 1 3 5 4 2 2 3 4
Output:
3 2 5
2 2 5
0 2 5
1 2 0
3 2 0
5 2 0
4 2 5
2 2 5
2 2 5
3 2 5
4 2 3
No of
3 -----------3 -----------3 -----------3 -----------1 -----------1 -----------1 -----------4 -----------4 -----------4 -----------4 -----------pagefaults=7
2
2
2
2
2
2
2
2
2
2
2
5
5
0
0
0
5
5
5
5
3
3
3
3
3
1
1
1
4
4
4
4
4
52