Professional Documents
Culture Documents
DATE:
2
vi. TASK : To display the current system time.
COMMAND : date
SYNTAX : date +%M
EXPLANATION: This command displays system time (in min) on the screen.
OUTPUT :
[exam1@local host ~]$date +%M
49
3
i. TASK : To display the calendar of a year.
COMMAND : cal
SYNTAX : cal year
EXAMPLE : cal 2007
EXPLANATION: This command displays the calendar of the specified year.
OUTPUT :
[exam88@local host ~] $ cal 2012
It displays all the month of the year 2012
5
12. TASK : To copy a file
COMMAND : copy
SYNTAX : cp source file destination file
EXAMPLE : cp wind.txt moon.txt
EXPLANATION: This command produces a copy of the source file to be stored in the specified destination file of
overwriting its previous content if exist.
OUTPUT :
[exam88@local host ~] $ cp wind.txt moon.txt
[exam88@local host ~] $ cat moon.txt
Hai good morning
Have a Nice Day
EXPLANATION: This command produces a copy of the source file to be stored in the specified destination
file(by creating it) .
[exam88@local host ~] $ cp wind.txt star.txt
[exam88@local host ~] $ cat star.txt
Hai good morning
Have a Nice Day
16. TASK : This command prints the line matching the pattern. grep(globally search a regular expression and print)
COMMAND : grep
SYNTAX : grep „string‟ filename
EXAMPLE : grep „stat‟ stat1.c
6
OUTPUT :
[exam1@CSELAB1 ~]$ grep 'stat' stat1.c
#include<sys/stat.h>
struct stat st;
s=stat("fork1.c",&st);
printf("\n use of stat() is unsuccessful ");
OUTPUT :
2 7 33 moon.txt
Note:
EXPLANATION : This command is useful for creating or editing programs.
SYNTAX : vi filename
EXAMPLE : vi fork.c
After completing the program in text editor press Esc+Shift+: (colon) wq to exit.
7
EX.NO: 2 PROGRAM USING SYSTEM CALL fork() , getpid() and getppid()
DATE:
AIM :
To write the program to create a Child Process using system call fork().
ALGORITHM :
Step 1 : Declare the variable pid.
Step 3 : If pid value is less than zero then print as “Error in Creation”.
Step 4 : Else if pid value is equal to zero then print current process id and parent process id.
Step 5 : Else if pid is greater than zero then print current process id and parent process id.
Step 6 : stop.
8
PROGRAM:
#include<sys/types.h>
int main()
{
int pid,p1,p2;
printf("\n Fork Calling ");
pid=fork();
printf("\n After Fork Called ");
if(pid > 0)
{
printf("\n New pid is %d ",pid);
p1=getpid();
printf("\n Current Process1 id : %d ",p1);
p2=getppid();
printf("\n Parent process1 id : %d \n",p2);
}
else if(pid==0)
{
p1=getpid();
printf("\n Current Process2 id : %d ",p1);
p2=getppid();
printf("\n Parent process2 id : %d \n",p2);
}
else if(pid==-1)
{
printf("\n Error in Creation ");
}
return 0;
}
Output: (FORK)
"fork.c" [New] 29L, 521C written
[exam03@localhost ~]$ cc fork.c
[exam03@localhost ~]$ ./a.out
Fork Calling
After Fork Called
Current Process2 id : 28920
Parent process2 id : 28919
Fork Calling
After Fork Called
New pid is 28920
Current Process1 id : 28919
Parent process1 id : 28886
[exam03@localhost ~]$
RESULT:
AIM :
ALGORITHM :
Step 5: Then use readdir() system call to read the directory entries.
10
PROGRAM CODING:
#include<stdio.h>
#include<sys/types.h>
#include<sys/dir.h>
void main(int age,char *argv[])
{
DIR *dir;
struct dirent *rddir;
printf("\n Listing the directory content\n");
dir=opendir(argv[1]);
while((rddir=readdir(dir))!=NULL)
{
printf("%s\t\n",rddir->d_name);
}
closedir(dir);
OUTPUT:
RP
roshi.c
first.c
pk6.c f2
abc FILE1
RESULT:
Thus the above program was executed successfully.
11
EX.NO :2b PROGRAM USING SYSTEM CALL exec( )
DATE:
AIM :
To write the program to implement the system call exec( ).
ALGORITHM :
PROGRAM CODING:
#include<stdio.h>
#include<unistd.h> main( ){
printf(“\n exec system call”);
printf(“displaying the date”);
execlp( “/bin/date”, “date”, 0);
OUTPUT:
Tue Dec 25 02 : 57 : 38 IST 2018
RESULT:
Thus the program was executed and verified successfully.
12
EX.NO : 2c PROGRAM USING SYSTEM CALLS wait( ) & exit( )
DATE:
AIM :
To write the program to implement the system calls wait( ) and exit( ).
ALGORITHM :
Step7 : After execution of the for loop then print “child process ends”.
Step 8 : Execute the system call wait( ) to make the parent to wait for the child process
to get over.
Step 9 : Once the child processes are terminated , the parent terminates and hence print “Parent
process ends”.
Step 10 : After both the parent and the chid processes get terminated it execute the wait( )
system call to permanently get deleted from the OS.
13
PROGRAM CODING:
#include<stdio.h> #include<unistd.h>
main( ){ int
i, pid;
pid=fork( );
if(pid== -1)
{perror(“fork failed”);
exit(0);
}else if(pid==0){
printf(“\n Child process starts‟);
for(i=0; i<5; i++){
printf(“\n Child process %d is called”, i);
}
printf(“\n Child process ends”);}
else{
wait(0);
printf(“\n Parent process ends”);
}exit(0);}
OUTPUT:
Child process starts Child
process 0 is called Child
process 1 is called Child
process 2 is called Child
process 3 is called Child
process 4 is called Child
process ends Parent process
ends
RESULT:
Thus the above program was executed successfully
14
EX. NO:3 SIMULATION OF UNIX COMMANDS “ls”
DATE:
AIM:
To write a C program to simulate the operation of “ls” commands in Unix.
ALGORITHM :
Step 1 : Include the necessary header files.
Step 2 : Define the buffer size as 1024.
Step 3 : Get the file name which has been created already.
Step 4 : Open the file in read mode.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
15
PROGRAM CODING:
#include<stdio.h>
#include<dirent.h>
int main()
{
struct dirent **namelist;
int n,i;
char pathname[100];
scanf("%s",&pathname);
getcwd(pathname);
n=scandir(pathname,&namelist,0,alphasort);
if(n<0)
printf("Error");
else
for(i=0;i<n;i++)
printf("%s\n",namelist[i]->d_name);
}
16
OUIPUT:
RESULT:
Thus the above program was executed successfully.
17
EX. NO:3a SIMULATION OF UNIX COMMANDS “grep”
DATE:
AIM:
To write a C program to simulate the operation of “grep” commands in Unix.
ALGORITHM :
Step 1 : Include the necessary header files.
Step 2 : Declare command line arguments.
Step 5 :.Read the contents of the file and store it in the buffer.
Step 6 : Print the contents stored in the buffer.
18
PROGRAM:
#include<stdio.h>
#include<string.h>
#define buffer 1024
int main(int argc,char *argv[])
{
FILE *fp;
char fline[buffer];
char *newline;
int line=1;
if(argc!=3)
{
printf("Please use the following syntax \n");
printf("./a.out filename searchstring \n");
printf("Example: ./a.out test1.c print \n");
exit(1);
}
// argv[1] is filename
if(!(fp=fopen(argv[1],"r")))
{
printf("grep : Couldnot open file : %s\n",argv[1]);
exit(1);
}
while(fgets(fline,buffer,fp)!=NULL)
{
// argv[2] is search string
if(strstr(fline,argv[2])!=NULL)
{
printf("Line number: %d text: %s\n",line,fline);
}
line++;
}
19
OUTPUT:
[exam100@localhost ~]$
RESULT:
Thus the above program was executed successfully.
20
EX. NO:4 SHELL PROGRAMMING
DATE:
Arithmetic Operators:
Relational Operators:
Note:
All the conditional expressions would be put inside square braces with one space
around them
[ $a == $b ] is correct where as [$a==$b] is incorrect
[ $a <= $b ] is correct where as [$a <= $b] is incorrect.
Boolean Operators:
22
4A. EVEN OR ODD NUMBER
AIM:
To find whether the given number is odd or even.
ALGORITHM:
1. Start the program.
2. Get the value for variable n.
3. Divide the value of n by 2 and store the value in r.
4. If the remainder of r is equal to zero then goto step 5 else goto step 6.
5. Print the given number is even number and goto step 7.
6. Print the given number is odd number and goto step 7.
7. Stop the program execution.
PROGRAM:
OUTPUT:
RESULT:
Thus the program was executed successfully.
23
4B. LINUX USING ECHO COMMAND
AIM:
To print the commands using echo.
ALGORITHM:
1. Start the program.
2. Print 1.WHO AM I?
3. Print 2.WHO IS LOGGED ON?
4. Print 3.DATE
5. Get the value for variable n.
6. Use switch case, if the value of n is 1 then displays the information about the current
user of the system on the screen else if the value of n is 2 then lists the information about all
the users who have logged on else if the value of n is 3 then displays the current system date
and time on the screen.
7. Stop the program execution.
PROGRAM:
echo "1.WHO AM I?"
echo "2.WHO IS LOGGED ON?"
echo "3.DATE"
read n
case $n in
1) whoami;;
2) who;;
3) date;;
esac
OUTPUT:
RESULT:
Thus the program was executed successfully.
24
4C. FACTORIAL OF A NUMBER
AIM:
To find the factorial of a given number.
ALGORITHM:
1. Start the program.
2. Get the value for variable num.
3. Initialize the value of i and fact as 1.
4. Check whether i is less than num, if so calculate fact=fact*i, increment the value of i
by 1, goto step 4.else goto step 5.
5. Print the factorial of a number.
6. Stop the program execution.
PROGRAM:
OUTPUT:
[exam1@CSELAB1 ~]$ sh ex3.sh
ENTER THE NUMBER
5
FACTORIAL OF 5 IS 120
[exam1@CSELAB1 ~]$
RESULT:
Thus the program was executed successfully.
25
4D. GREATEST OF THREE NUMBERS
AIM:
To find the greatest of three numbers.
ALGORITHM:
1. Start the program.
2. Enter the value for a, b &c.
3. If a is greater than b and a is greater than c then goto step 4 else check b is greater than
c then goto step 5 else Print c is greatest.
4. Print a is greatest goto step 6.
5. Print b is greatest goto step 6.
6. Stop the program execution.
PROGRAM:
OUTPUT:
[exam1@localhost ~]$ sh ex04.sh
ENTER THE VALUES:
23 98 -1
98 IS GREATEST
[exam1@localhost ~]$
RESULT:
Thus the program was executed successfully.
26
IMPLEMENTATION OF CPU SCHEDULING
EX.NO.5A
DATE:
IMPLEMENTATION OF FCFS SCHEDULING
AIM:
To write a c program to implement FCFS scheduling.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
struct process
{
int pro;
int brt;
int tt;
int wt;
}pr[MAX];
// main program
int main()
{
int i,j,n,totwt,tottt;
float avgwt,avgtt;
int prevtt;
clrscr();
27
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
printf("\tEnter burst time:");
scanf("%d",&pr[i].brt);
}
//calculating Waiting time and Turn Aruond Time
prevtt =0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].brt;
prevtt = pr[i].tt;
}
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
28
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
First Come First Serve
~~~~~ ~~~~ ~~~~~ ~~~~~
-----------------------
0 8 11 1517 23
-----------------------
RESULT:
Thus the program was executed successfully.
29
EX.NO.5B
IMPLEMENTATION OF SJF SCHEDULING
AIM:
To write a c program to implement SJF scheduling.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
}pr[MAX];
// Main program
int main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int prevtt;
clrscr();
t= pr[j].brt;
pr[j].brt= pr[i].brt;
pr[i].brt=t;
}
}
}
31
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
SHORTEST JOB FIRST
Enter the no of process:4
Process no: 1
Enter burst time:6
Process no: 2
Enter burst time:8
Process no: 3
Enter burst time:7
Process no: 4
Enter burst time:3
PROCESS NO BURST TIME WAITING TIME TURN AROUND TIME
------------- ------------- ---------- --------------
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24
RESULT:
Thus the program was executed successfully.
32
EX.NO.5C
IMPLEMENTATION OF PRIORITY SCHEDULING
AIM:
To write a c program to implement PRIORITY scheduling.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
int pri;
}pr[MAX];
// Main program
int main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int prevtt;
clrscr();
printf("\n\t\t\t PRIORITY SCHEDULING");
printf("\n\n Enter the no of process:");
scanf("%d",&n);
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
33
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
printf("\nEnter Priority:");
scanf("%d",&pr[i].pri);
}
t= pr[j].brt;
pr[j].brt= pr[i].brt;
pr[i].brt=t;
t= pr[j].pri;
pr[j].pri= pr[i].pri;
pr[i].pri=t;
}
}
}
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
34
//Average of Waiting time and Turn Aruond Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
PRIORITY SCHEDULING
Process no: 1
Enter burst time:10
Enter Priority:3
Process no: 2
Enter burst time:1
Enter Priority:1
Process no: 3
Enter burst time:2
35
Enter Priority:4
Process no: 4
Enter burst time:1
Enter Priority:5
Process no: 5
Enter burst time:5
Enter Priority:2
-------------------
01 6 161819
-------------------
RESULT:
Thus the program was executed successfully.
36
EX.NO.5D IMPLEMENTATION OF RR SCHEDULING
AIM:
To write a c program to implement Round Robin scheduling.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
int remtime;
}pr[MAX];
// Main program
void main()
{
37
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int ts;
int totbrt;
printf("\n Enter the no of process:");
scanf("%d",&n);
printf("\nEnter the time slice:");
scanf("%d",&ts);
//Getting Process no. and Burst Time
totbrt = 0;
for(i=1;i<=n;i++)
{
printf("\nEnter process no: %d \n", i);
pr[i].pro = i;
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
pr[i].remtime = pr[i].brt;
totbrt = totbrt + pr[i].brt;
}
//calculating Waiting time and Turn Around Time
tottt = 0;
while (1)
{
if(tottt == totbrt)
{
break;
}
for(i=1;i<=n;i++)
{
if(pr[i].remtime == 0)
{
continue;
}
RESULT:
Thus the program was executed successfully.
39
EX.No.6 IMPLEMENTATION OF SEMAPHORE -DINING PHILOSOPHER'S PROBLEM
DATE:
AIM:
To write a c program to implement the Dining Philosopher problem using semaphore .
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define N 5
#define LEFT (j-1)%N
#define RIGHT (j+1)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
int state[N];
void test(int);
int lt, rt;
void main()
{
int f,ch=0,i,a,b;
int take_sticks(int);
int put_sticks(int);
clrscr();
printf("\n \t *** DINING PHILOSOPHER PROBLEM**\n");
printf("ENTER THE NUMBER OF PHILOSOPHER IN THE TABLE IS %d:",N);
40
printf("\n STATUS OF THE PHILOSOPHER:\n");
for(i=0;i<N;i++)
{
state[i]=THINKING;
printf("\n P%d IS THINKING",i);
}
do
{
printf("\n\t\t MENU\n\n");
printf("\n\t1.START EATING\n\n");
printf("\n\t2.STOP EATING\n\n");
printf("\n\t3.EXIT\n\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n ENTER THE PHILOSOPHER WHO IS GOING TO EAT:");
scanf("%d",&a);
f=take_sticks(a);
if(f==EATING)
{
printf("\n THE PHILOSOPHER HAS STARTED EATING...\n\n");
}
else
{
printf("\nTHE PHILOSOPHER IS HUNGRY!!..\n\n");
printf("\n THE CHOPSTICK IS ENGAGED\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
} break;
41
case 2:
printf("\n ENTER THE PHILOSOPHER NUMBER TO RELEASE THE CHOPSTICK");
scanf("%d",&b);
if(state[b]==EATING)
{
f=put_sticks(b);
printf("\n THE PHILOSOPHER HAS STOPPED EATING!!..\n\n");
}
else
{
printf("\n THE PHILOSOPHER IS NOT EATING!!\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 3:
exit(0);
break;
}
}
while(ch!=3);
getch();
}
int take_sticks(int j)
{
state[j]=HUNGRY;
test(j);
return state[j];
}
42
int put_sticks(int j)
{
state[j]=THINKING;
if(j==0) test(N-1); else test(LEFT);
if(j==N-1) test(0); else test(RIGHT);
return state[j];
}
void test(int j)
{
if(j==0) rt = N-1; else rt = LEFT;
if(j==N-1) lt = 0; else lt = RIGHT;
if(state[j]==HUNGRY && state[lt]!=EATING && state[rt]!=EATING)
{
state[j]=EATING;
}
}
OUTPUT:
P0 IS THINKING
P1 IS THINKING
P2 IS THINKING
P3 IS THINKING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
43
P0 IS THINKING
P1 IS EATING
P2 IS THINKING
P3 IS THINKING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
P0 IS THINKING
P1 IS EATING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
44
P0 IS HUNGRY
P1 IS EATING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
P0 IS EATING
P1 IS THINKING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
RESULT:
Thus the program was executed successfully.
45
IMPLEMENT SHARED MEMORY AND IPC
DATE:
AIM:
To write a program in C to implement interprocess communication using pipes.
ALGORITHM:
1. Start
2. Create the child process using fork().
3. create the pipe structure using pipe().
4. Now close the read end of the parent process using close().
5. Write the data in the pipe using write().
6. Now close the write end of the child process using close().
7. Read the data in the pipe using read ().
8. Display the string.
9. Stop.
PROGRAM:
#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];
char b[10];
printf("\n Enter the string to enter into the pipe:");
scanf("%s",&a);
pipe(fd);
child=fork();
if(child>0)
{
write(fd[1],a,10);
}
46
else
{
read(fd[0],b,10);
OUTPUT:
Enter the string to enter into the pipe: Operating system
The string retrieved from the pipe is Operating system
RESULT:
Thus the program was executed successfully.
47
I
EX NO: 7B INTERPROCESS COMMUNICATION USING SHARED MEMORY
DATE:
AIM:
To write a program in C to implement interprocess communication using shared
memory.
ALGORITHM:
1. Start
2. Declare the variable for server program.
3. Using key,shmat,sgmid function to allocate the shared memory.
4. Declare the variable for client program.
5. Using key,shmat,sgmid function to allocate the shared memory for client
6. Print the value of shared memory client program.
7. Display the string.
8. Stop.
PROGRAM:Server Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 25
void main()
{
char c;
int shmid;
key_t key;
char *shm ,*s;
key = 9647;
printf("\n\t SHARED MEMORY SERVER PROGRAM \n");
if((shmid = shmget(key,SHMSIZE,IPC_CREAT|0666))<0)
{
perror("\n SHARED MEMORY CREATE ERROR");
exit(0);
}
if((shm = shmat(shmid,0,0))==(char *) -1)
{
perror("\n SHARED MEMORY ALLOCATED ERROR");
exit(1);
}
s = shm;
for(c='A';c<='Z';c++)
{
*s++ = c;
*s = '\0';
}
48
exit(0);
}
Client Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 0
void main()
{
char c;
int shmid;
key_t key;
char *shm ,*s;
key = 9647;
printf("\n\t SHARED MEMORY CLIENT PROGRAM\n ");
if((shmid = shmget(key,SHMSIZE,IPC_CREAT|0666))<0)
{
perror("\n SHARED MEMORY CREATE ERROR");
exit(0);
} .
if((shm = shmat(shmid,0,0))==(char *) -1)
{
perror("\n SHARED MEMORY ALLOCATED ERROR");
exit(1);
}
for(s=shm;*s!='\0';s++)
{
printf("\t");
putchar(*s);
}
exit(0);
}
[mca01@localhost ~]$
RESULT:
Thus the program was executed successfully.
49
EX.NO.8 IMPLEMENTATION OF BANKER’S ALGORITHM FOR
DATE: DEADLOCK AVOIDANCE
AIM:
To write a c program to implement BANKER‟ algorithm for Deadlock Avoidance.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void matrix_get(int[5][5],int,int);
void main()
{
int max[5][5],all[5][5],total[1][5];
int avail[1][5],f[5],need[5][5],state[5];
int s,i,j,p,r,c,ch,ch1,k=0;
clrscr();
printf("\t\t DEADLOCK -BANKER'S ALGORITHM \n");
printf("ENTER THE NUMBER OF PROCESS:");
scanf("%d",&p);
ch=p;
printf("ENTER THE NUMBER OF RESOURCE TYPE:");
scanf("%d",&r);
printf("ENTER THE MAX MATRIX:\n");
matrix_get(max,p,r);
printf("\nENTER THE ALLOCATED MATRIX:\n");
50
matrix_get(all,p,r);
printf("ENTER THE TOTAL RESOURCE PRESENT:\n");
matrix_get(total,1,r);
for(i=0;i<r;i++)
{
for(j=0;j<p;j++)
{
if(max[j][i]>total[0][i]||all[j][i]>total[0][i])
{
printf("\nINPUT IS > TOTAL RESOURCE PRESENT\n");
exit(0);
}
}
s=total[0][i];
for(j=0;j<p;j++)
{
s=s-all[j][i];
}
avail[0][i]=s;
}
}
printf("\n");
}
while(ch>0)
{
ch1=ch;
for(i=0;i<p;i++)
{
51
if(f[i]==0)
{
c=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[0][j])
{
c++;
}
}
if(c==r)
{
f[i]=1;
state[k++]=i;
ch--;
for(j=0;j<r;j++)
{
avail[0][j]=avail[0][j]+all[i][j];
need[i][j]=0;
}
}
}
}
if(ch==p)
{
printf("\n ALREADY DEADLOCK OCCURRED");
exit(0);
}
if(ch1==ch)
{
printf("\n ALLOCATION LEADS TO DEADLOCK");
exit(0);
}
}
printf("\n\n SAFE STATE S:{");
for(i=0;i<p;i++)
{
printf("P%d ",state[i]);
}
printf("}");
52
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&a[i][j]);
}
}
}
OUTPUT:
DEADLOCK -BANKER'S ALGORITHM
ENTER THE NUMBER OF PROCESS:5
ENTER THE NUMBER OF RESOURCE TYPE:3
ENTER THE MAX MATRIX:
753
322
902
222
433
AVAILABLE MATRIX
3 3 2
NEED MATRIX:
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
RESULT:
Thus the program was executed successfully.
53
EX.NO: 9 IMPLEMENTATION OF DEADLOCK DETECTION ALGORITHM
DATE:
AIM:
To write a C program to implement Deadlock Detection algorithm .
ALGORITHM:
PROGRAM:
54
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=tp;i++)
{
sum=0;
for(j=1;j<=tr;j++){
sum+=p[i][j];
} if(sum==0){
m[k]=i;
k++;
}
}
for(i=1;i<=tp;i++){
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=tr;j++)
if(c[i][j]<temp[j]){
flag=0;
break;
}
}
if(flag==1){
m[k]=i;
k++;
for(j=1;j<=tr;j++)
temp[j]+=p[i][j];
}}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++){
found=0;
for(i=1;i<k;i++){
if(j==m[i])
found=1;}
if(found==0)
printf("%d\t",j);}
getch();
}
55
OUTPUT:
Enter total no. of processes : 4
Enter total no. of resources : 5
Enter claim (Max. Need) matrix :
01001
00101
00001
10101
Enter allocation matrix :
10110
11000
00010
00000
Enter resource vector (Total resources) :
21121
Enter availability vector (available resources) :
00001
deadlock causing processes are : 2 3
RESULT:
56
EX.NO: 10 THREADING & SYNCHRONIZATION
DATE:
AIM:
To write a c program to implement threading and synchronization.
ALGORITHM:
Step 1: Start the Program
Step 2: Obtain the required data through char and in data types.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution
PROGRAM CODING:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
void* doSomeThing(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d started\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d finished\n", counter);
return NULL;
}
int main(void)
{
int i = 0;
int err;
while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); if (err != 0)
printf ("\ncan't create thread :[%s]", strerror(err)); i++;
}pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
RESULT:
Thus the program was executed successfully.
57
EX.NO: 11 IMPLEMENTATION OF THE FOLLOWING MEMORY
ALLOCATION METHODS FIXED PARTITION FIRST FIT &
BEST FIT MEMORY MANAGEMENT
DATE:
AIM:
ALGORITHM:
1. Start the program.
2. Get the segment size, number of process to be allocated and their corresponding size.
3. Get the options. If the option is „2‟ call first fit function.
5. For first fit, allocate the process to first possible segment which is free and set the personnel
slap as „1‟. So that none of process to be allocated to segment which is already allocated and vice
versa.
8. Allocate the process to the segment which is equal to or slightly greater than the process size
and set the flag as the „1‟ .So that none of the process to be allocated to the segment which is
already allocated and vice versa. Stop the program.
58
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);}
for(i=1;i<=nf;i++)
{ for(j=1;j<=nb;j++){
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}}}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",
i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
59
OUTPUT:
Enter the number of blocks: 3
Enter the number of files: 2
Enter the size of the blocks:-
Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:-
File 1: 1
File 2: 4 File File Size Block No Block Size Fragment
No
1 1 3 7 6
2 4 1 5 1
RESULT:
Thus the program was executed successfully.
60
EX.NO:12 MEMORY MANAGEMENT SCHEME – PAGING
DATE:
AIM:
To implement the memory management using paging concept.
ALGORITHM:
1. Start
2. Declare the variables.
3. Get the number of blocks in memory.
4. Get the size of each block.
5. Get the number of jobs and size of jobs.
6. If the size of the process is greater than the available pages then display error
message.
7. If the size of the process is lesser than the available pages then allocate the block.
8. Display the output.
9. Stop.
PROGRAM:
#include<stdio.h>
int main()
{
int nb,sb,nj,i,j;
int js[10];
printf("Number of blocks in memory :");
scanf("%d",&nb);
printf("Size of Each block:");
scanf("%d",&sb);
printf("Number of jobs :");
scanf("%d",&nj);
for(j=1;j<=nj;j++)
{
printf("Size of job %d:", j);
scanf("%d",&js[j]);
}
j=1;
for(i=1;i<=nb;i++)
{
printf("job %d stored in memory block %d\n ", j,i);
js[j] = js[j] - sb;
if(js[j] <= 0)
{
j++;
}
61
if(j > nj)
{
break;
}
}
if( i > nb)
{
printf("Out of memory");
}
return 0;
}
OUTPUT:
RESULT:
Thus the program was executed successfully.
62
EX.NO.13 PAGE REPLACEMENT ALGORITHM
AIM :
To write a C program to implement the following page replacement algorithms
1)FIFO
2)LRU
3)OPTIMAL
ALGORITHM:
1: Start
2: Get the number of frames, reference string and the number of elements in the reference
string using getdata() function.
3: Using fifo() function insert the pages into the frames and using search() function check
whether the page is already exist in the frame.
4: In the fifo() function check whether the page fault occurs or not using fifo replacement
algorithm.
5: Print the number of hits and number of miss.
6: Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
int nf;
int ne;
int hit,miss;
int ref[50];
int fr[15];
void getdata();
void fifo();
void getdata()
{
int i;
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void fifo()
{
63
int i,j,found=0,rear=-1;
miss=hit=0;
int search(int);
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
printf("\nThe no to be inserted :%d",ref[i]);
found=search(ref[i]);
if(found==0)
{
miss++;
if(rear==nf-1)
{
rear=-1;
}
rear++;
fr[rear]=ref[i];
}
else
{
hit++;
}
printf("\n");
for(j=0;j<nf;j++)
printf("%d ",fr[j]);
}
printf("\n\n No. of page faults=%d\n",miss);
printf("\n No. of Hits=%d",hit);
}
int search(int item)
{
int i;
for(i=0;i<nf;i++)
{
if(fr[i]==item)
{
return(1);
}
}
return 0;
}
int main()
{
printf("\n\t\t\t FIFO PAGE REPLACEMENT");
getdata();
printf("\t\t\t FIFO PAGE REPLACEMENT");
fifo();
} 64
OUTPUT:
No. of Hits=1
RESULT:
Thus the program was executed successfully.
65
13B. LRU PAGE REPLACEMENT ALGORITHM
AIM :
To write a C program to implement the following page replacement algorithms.
1)FIFO
2)LRU
3)OPTIMAL
ALGORITHM:
1: Start
2: Get the number of frames, reference string and the number of elements in the reference
string using getdata() function.
3: Using lru() function insert the pages into the frames and using search() function check
whether the page is already exist in the frame.
4: In the lru() function check whether the page fault occurs or not using lru replacement
algorithm.
5: Print the number of page faults.
6: Stop.
PROGRAM:
#include<stdio.h>
int nf;
int i,ne;
int miss=0;
int ref[50];
int fr[15];
void getdata();
void lru();
void getdata()
{
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void lru()
{
int check,j,k;
int count=0,index[5];
printf("\n\n Simulation of LRU page replacement technique!...........\n");
66
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
check=0;
for(j=0;j<nf;j++)
{
if(fr[j]==ref[i])
{
check=1;
}
}
if(check==0)
{
miss+=1;
if(count<nf)
{
fr[count++] = ref[i];
}
else
{
for(j=0;j<i;j++)
{
for(k=0;k<nf;k++)
{
if(ref[j]==fr[k])
{
index[k] = j;
}
}
}
for(j=0;j<nf;j++)
{
if(index[check]>index[j])
{
check=j;
}
}
fr[check]=ref[i];
}
printf("\n");
for(j=0;j<nf;j++)
{
printf("%d\t",fr[j]);
}
}
67
}
printf("\n Number of page faults:%d",miss);
}
void main()
{
printf("\n\t\t\t LRU PAGE REPLACEMENT");
getdata();
printf("\t\t\t LRU PAGE REPLACEMENT");
lru();
}
OUTPUT:
1 -1 -1
1 2 -1
1 2 3
4 2 3
4 2 1
Number of page faults:5
RESULT:
Thus the program was executed successfully.
68
13C. OPTIMAL PAGE REPLACEMENT ALGORITHM
AIM :
To write a C program to implement the following page replacement algorithms.
1)FIFO
2)LRU
3)OPTIMAL
ALGORITHM:
1: Start
2: Read total number of pages in reference string
3: Read total number of frames
4: Read number of page faults if all the condition satisfies the following statements are
displayed
5: Replacement stratergies are efficient
6: Optimal page replacement stratergy is efficient
7: End
PROGRAM:
#include<stdio.h>
#include<conio.h>
int fr[100],n;
void main()
{
void display();
int page[100],pages,p,i,j,fs[3];
int max,found=0,lg[3],index,k,l,flag1=0,flag2=0,pf=0;
clrscr();
printf("\n Enter total number of frames :\n");
scanf("%d",&n);
printf("\n Enter the total number of page reference string :\n");
scanf("%d",&pages);
for(i=0;i<n;i++)
{
fr[i]=-1;
}
printf("\nEnter the page reference string :");
for(i=0;i<pages;i++)
{
scanf("%d",&p);
page[i]=p;
}
for(j=0;j<pages;j++)
{
flag1=0;
flag2=0;
for(i=0;i<n;i++)
{
if(fr[i]==page[j])
{ 69
flag1=1;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<n;i++)
{
if(fr[i]==-1)
{
fr[i]=page[j];
flag2=1;
pf++;
break;
} } }
if(flag2==0)
{
for(i=0;i<n;i++)
{ lg[i]=0;
}
for(i=0;i<n;i++)
{
for(k=j+1;k<pages;k++)
{
if(fr[i]==page[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<n;i++)
{
if(lg[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<n;i++)
{
if(max<lg[i])
{
max=lg[i];
70
index=i;
} } }
fr[index]=page[j];
pf++;
}
display();
}
printf("\n no of page faults:%d",pf);
getch();
}
void display()
{
int i;
printf("\n");
for(i=0;i<n;i++)
printf("\t%d",fr[i]);
}
OUTPUT:
Enter total number of frames :
3
Enter the total number of page reference string :
20
Enter the page reference string :
70120304230321201701
7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 0 3
2 0 3
2 4 3
2 4 3
2 4 3
2 0 3
2 0 3
2 0 3
2 0 1
2 0 1
2 0 1
2 0 1
7 0 1
7 0 1
7 0 1
no of page faults:9
RESULT:
Thus the program was executed successfully.
71
EX NO: 15a FILE ALLOCATION METHODS
AIM:
To write a program in C for contiguous or sequential file allocation.
ALGORITHM:
1. Start
2. Declare the variables.
3. Get the number of files.
4. Get the number of blocks for file.
5. Allocate the blocks for file depending on length.
6. Finally display the result.
7. Stop
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,block[20],start;
printf("Enter the no. of file : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number of blocks needed for file %d: ", i);
scanf("%d",&block[i]);
}
start=0;
for(i=1;i<=n;i++)
{
printf("File %d Start %d Size %d\n",i,start,block[i]);
start=start+block[i];
}
}
OUTPUT:
"con.c" 22L, 314C written
[exam100@localhost ~]$ cc con.c
[exam100@localhost ~]$ ./a.out
Enter the no. of file : 2
Enter the number of blocks needed for file 1: 3
Enter the number of blocks needed for file 2: 2
File 1 Start 0 Size 3
File 2 Start 3 Size 2
[exam100@localhost ~]$
RESULT:
Thus the program was executed successfully.
72
EX NO: 15B INDEXED FILE ALLOCATION
AIM:
To write a program in C for Indexed file allocation.
ALGORITHM:
1. Start
2. Declare the variables.
3. Get the number of blocks are already allocated.
4. Enter the allocated block numbers.
5. Give the starting index block & length.
6. Finally display the result.
7. Stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int f[50],i,n,st,len,k;
for(i=0;i<50;i++)
{
f[i]=1;
}
printf("How many blocks are already allocated ?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the allocated block numbers ");
scanf("%d",&k);
f[k]=0;
}
printf("Enter the starting index block & length");
scanf("%d %d",&st,&len);
i=0;
k=st;
while(i<len)
{
if(f[k]==1)
{
f[k]=0;
printf("\n %d-> file allocating",k);
i++;
}
else
{
printf("\n %d->file is already allocated",k);
}
k++;
}}
73
OUTPUT:
RESULT:
Thus the program was executed successfully.
74
EX.NO :15C LINKED FILE ALLOCATION
DATE:
AIM:
To write a C program to implement File Allocation concept using the technique Linked List Technique..
ALGORITHM:
Step 1: Start the Program
Step 2: Get the number of files.
Step 3: Allocate the required locations by selecting a location randomly
Step 4: Check whether the selected location is free.
Step 5: If the location is free allocate and set flag =1 to the allocated locations.
Step 6: Print the results file no, length, blocks allocated.
Step 7: Stop the execution
CODING:
#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated"); scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & length"); scanf("%d%d",&st,&len); k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0){
f[j]=1;
printf("\n%d->%d",j,f[j]);
}else{
printf("\n %d->file is already allocated",j);
k++;
}}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}
75
OUTPUT:
Enter how many blocks are already allocated 3
Enter the blocks no‟s that are already allocated 4 7 9
Enter the starting index block & length 3
7
3-> 1
4-> File is already allocated
5->1
6->1
7-> File is already allocated
8->1
9-> File is already allocated
10->1
11->1
12->1
If u want to enter one more file? (yes-1/no-0)
RESULT :
Thus the program was executed successfully.
76