//5.
a unnamed pipes
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()
{
int pid,a[2],b[2];
char str[30],buff1[30],buff2[30];
pipe(a);
pipe(b);
pid=fork();
if(pid==0)
{
strcpy(str,"Welcome to OS lab");
write(a[1],str,strlen(str)+1);
sleep(2);
read(b[0],buff1,sizeof(buff1));
printf("response from parent is %s\n",buff1);
}
else
{
read(a[0],buff2,sizeof(buff2));
printf("request from child is %s\n",buff2);
strcpy(buff2,"parent says hello");
write(b[1],buff2,sizeof(buff2));
exit(1);
}
}
OUTPUT:
[cse22151@cseserver oslab]$ cc bopes.c
[cse22151@cseserver oslab]$ ./a.out
request from child is Welcome to OS lab
[cse22151@cseserver oslab]$ response from parent is parent says hello
//5.b Named Pipes( fifo)
//Program 1 Writes First
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
int main(){
int fd;
char str1[100],str2[100];
fd= mkfifo("fifo1",0644);
printf("named pipe created\n");
while(1){
fd=open("fifo1",O_WRONLY);
fgets(str1,100,stdin);
write(fd,str1,strlen(str1)+1);
close(fd);
fd=open("fifo1",O_RDONLY);
read(fd,str2,sizeof(str2));
printf("user2:%s\n",str2);
close(fd);
}
}
//Program 2 Reads First
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
int main()
{
int fd;
char str1[100],str2[100];
while(1)
{
fd=open("fifo1",O_RDONLY);
read(fd,str1,sizeof(str1));
printf("user1:%s\n",str1);
close(fd);
fd=open("fifo1",O_WRONLY);
fgets(str2,100,stdin);
write(fd,str2,strlen(str2)+1);
close(fd);
}
OUTPUT:-
[cse22151@cseserver os]$cc write.c –o writer
[cse22151@cseserver os]$cc read.c –o reader
[cse22151@cseserver os]$./writer
named pipe created
hello
//In Second terminal
[cse22151@cseserver os]$./reader
user1: hello
Welcome
//In First terminal
[cse22151@cseserver os]$
user2:Welcome
//5.c msgQueue
#include<stdio.h>
#include<string.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<unistd.h>
int main()
{
int pid,msqid;
char buff[20],buff1[20],a[20];
msqid=msgget((key_t)151,IPC_CREAT|0600);
pid=fork();
if(pid==0)
{ strcpy(a,"160423733121"
);
msgsnd(msqid,a,12,0);
sleep(2);
msgrcv(msqid,buff1,18,0,0);
printf(" echoed message is %s\n",buff1);
}
else
{
sleep(1);
msgrcv(msqid,buff,12,0,0);
printf("message received is %s\n",buff);
strcat(buff," pass");
msgsnd(msqid,buff,18,0);
}
}
OUTPUT:
[cse22151@cseserver oslab]$ cc mqes.c
[cse22151@cseserver oslab]$ ./a.out
message received is 160422733151
[cse22151@cseserver oslab]$ echoed message is 160423733121 pass
//5.d sharedmemory
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/shm.h>
int main(){
int pid,shmid;
char *p;
shmid=shmget((key_t)224,20,IPC_CREAT|0600);
p=(char*)shmat(shmid,0,0);
pid=fork();
if(pid==0){
strcpy(p,"data in shared memory\n");
}
else if(pid>0){
sleep(1); printf("%s\
n",p);
}
else{
perror("error");
}
}
OUTPUT:-
[cse22151@cseserver os]$ ./a.out
data in shared memory
[cse22151@cseserver os]$
//7 bankers
Algorithm
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(){
int max[10][10],need[10][10],alloc[10][10],avail[10],fin[10],safe[10];
int pr_cnt,res_cnt,i,j,process,count=0;
system("clear");
printf(" Enter the system state information\n");
printf("Enter the number of processes\n");
scanf("%d",&pr_cnt);
for(i=0;i<pr_cnt;i++){
fin[i]=0;
}
printf("Enter the number of resources\n");
scanf("%d",&res_cnt);
printf("Enter the max matrix for all processes:\n");
for(i=0;i<pr_cnt;i++){
for(j=0;j<res_cnt;j++){
printf("Enter max info for process[%d][%d]\n",i,j);
scanf("%d",&max[i][j]);
}
}
for(i=0;i<pr_cnt;i++){
for(j=0;j<res_cnt;j++){
printf("Enter allocation info for process[%d][%d]\n",i,j);
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available resources in the system:\n");
for(i=0;i<res_cnt;i++){
printf("available[%d]=\n",i);
scanf("%d",&avail[i]);
}
for(i=0;i<pr_cnt;i++){
for(j=0;j<res_cnt;j++){
need[i][j]=max[i][j]-alloc[i][j];
} }
do{
printf("Available resources are:\n"); for(j=0;j<res_cnt;j+
+)
printf("%d\t",avail[j]);
printf("\n max matrix:\t allocation matrix:\n"); for(i=0;i<pr_cnt;i+
+){
for(j=0;j<res_cnt;j++){
printf("%d\t",max[i][j]);
}
printf("\t");
for(j=0;j<res_cnt;j++){
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
process=-1;
for(i=0;i<pr_cnt;i++){
if(fin[i]==0){
process=i;
for(j=0;j<res_cnt;j++){
if(avail[j]<need[i][j]){
process=-1;
break;
}
}
}
if(process!=-1)
break;
}
if(process!=-1){
safe[count]=process+1;
count++; for(j=0;j<res_cnt;j+
+){
avail[j]+=alloc[process][j];
alloc[process][j]=0;
max[process][j]=0;
fin[process]=1;
}
}
}
while(count!=pr_cnt && process!=-1);
if(count==pr_cnt){
printf("the system is in a safe state\n");
printf("Safe sequence:<");
for(i=0;i<pr_cnt;i++)
printf("%d",safe[i]); printf(">\
n");
}
else{
printf("the system is in unsafe state!!\n");
}
}
OUTPUT:-
[cse22151@cseserver os]$./a.out
Enter the system state information
Enter the number of processes
5
Enter the number of resources
3
Enter the max matrix for all
processes: Enter max info for process
[0][0]
7
Enter max info for process [0][1]
5
Enter max info for process [0][2]
3
Enter max info for process [1][0]
3
Enter max info for process [1][1]
2
Enter max info for process [1][2]
2
Enter max info for process [2][0]
9
Enter max info for process [2][1]
0
Enter max info for process [2][2]
2
Enter max info for process [3][0]
2
Enter max info for process [3][1]
2
Enter max info for process [3][2]
2
Enter max info for process [4][0]
4
Enter max info for process [4][1]
3
Enter max info for process [4][2]
3
Enter allocation info for process [0]
[0] 0
Enter allocation info for process [0]
[1] 1
Enter allocation info for process [0]
[2] 0
Enter allocation info for process [1]
[0] 2
Enter allocation info for process [1]
[1] 0
Enter allocation info for process [1]
[2] 0
Enter allocation info for process [2]
[0] 3
Enter allocation info for process [2]
[1] 0
Enter allocation info for process [2]
[2] 2
Enter allocation info for process [3]
[0] 2
Enter allocation info for process [3]
[1] 1
Enter allocation info for process [3][2]
1
Enter allocation info for process [4]
[0] 0
Enter allocation info for process [4]
[1] 0
Enter allocation info for process [4]
[2] 2
Enter the available resources in the system:
available[0]=
3
available[1]=
3
available[2]=
2
Available resources are: 332
max matrix: allocation matrix:
753 010
322 200
902 302
605 211
433 002
Available resources are: 532
max matrix: allocation matrix:
753 010
000 000
902 302
222 211
433 002
Available resources are: 743
max matrix: allocation matrix:
753 010
000 000
902 302
000 000
433 002
Available resources are: 753
max matrix: allocation matrix:
000 000
000 000
902 302
000 000
433 002
Available resources are: 1055
max matrix: allocation matrix:
000 000
000 000
000 000
000 000
433 002
the system is in a safe state
Safe sequence:<24135>
[cse22151@cseserver os]$
//8.a fifo page replacement
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,n,rs[50],f[10],nf,k=0,avail,pf=0;
float hr,mr;
system("clear");
printf("Enter number of pages\n");
scanf("%d",&n);
printf("Enter the reference string\n");
for(i=1;i<=n;i++){
scanf("%d",&rs[i]);
}
printf("Enter frame size\n");
scanf("%d",&nf);
for(i=0;i<nf;i++)
f[i]=-1;
printf("page frames\n");
for(i=1;i<=n;i++){
avail=0; for(j=0;j<nf;j+
+){
if(f[j]==rs[i])
avail=1;
}
if(avail==0){
f[k]=rs[i];
k=(k+1)%nf; pf+
+; for(j=0;j<nf;j+
+)
printf("%d\t",f[j]);
printf("pf no is %d",pf);
}
printf("\n");
}
printf("Total pf is %d\n",pf);
mr=(float)pf/n;
hr=(float)(n-pf)/n;
printf("hr,mr %.2f %.2f\n",hr,mr);
}
OUTPUT:-
[cse22151@cseserver os]$
Enter number of pages
12
Enter the reference string
123412512345
Enter frame size
3
page frames
1 -1 -1 pf no is 1
1 2 -1 pf no is 2
1 2 3 pf no is 3
4 2 3 pf no is 4
4 1 3 pf no is 5
4 1 2 pf no is 6
5 1 2 pf no is 7
5 3 2 pf no is 8
5 3 4 pf no is 9
Total pf is 9
hr,mr 0.25 0.75
[cse22151@cseserver os]$
//8.b LRU page replacement
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,n,rs[50],f[10],nf,k,min,count[10],flag[20],pf=0,next=1;
float hr,mr;
system("clear");
printf("Enter number of pages\n");
scanf("%d",&n);
printf("Enter the reference string\n");
for(i=0;i<n;i++){
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter frame size\n");
scanf("%d",&nf);
for(i=0;i<nf;i++){
count[i]=0;
f[i]=-1;
}
printf("page frame\n");
for(i=0;i<n;i++){
for(j=0;j<nf;j++){
if(f[j]==rs[i]){
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0){
if(i<nf){
f[i]=rs[i];
count[i]=next;
next++;
}
else{
min=0; for(j=1;j<nf;j+
+)
if(count[min]>count[j])
min=j;
f[min]=rs[i];
count[min]=next; next+
+;
}
pf++;
}
for(j=0;j<nf;j++)
printf("%d\t",f[j]);
if(flag[i]==0)
printf("pf no is %d",pf);
printf("\n");
}
printf("Total pf is %d\n",pf);
mr=(float)pf/n;
hr=(float)(n-pf)/n;
printf("hr,mr %.2f %.2f\n",hr,mr);
}
OUTPUT:-
[cse22151@cseserver os]$./a.out
Enter number of pages
12
Enter the reference string
123412512345
Enter frame size
3
page frame
1 -1 -1 pf no is 1
1 2 -1 pf no is 2
1 2 3 pf no is 3
4 2 3 pf no is 4
4 1 3 pf no is 5
4 1 2 pf no is 6
5 1 2 pf no is 7
5 1 2
5 1 2
3 1 2 pf no is 8
3 4 2 pf no is 9
3 4 5 pf no is 10
Total pf is 10
hr,mr 0.17 0.83
[cse22151@cseserver os]$
//9.a fcfs disk scheduling
#include<stdio.h>
#include<stdlib.h>
int main(){
int req[20],n,hdpos,tseek=0,i;
float avgmv;
printf("Enter the number of requests\n");
scanf("%d",&n);
printf("Enter the disk read requests\n");
for(i=0;i<n;i++){
//temp[i]=req[i];
scanf("%d",&req[i]);
}
printf("Enter head position\n");
scanf("%d",&hdpos);
printf("FCFS sequence is\n");
for(i=0;i<n;i++){
printf("%d->",req[i]);
tseek=tseek+abs(req[i]-hdpos);
hdpos=req[i];
}
avgmv=(float)tseek/n; printf("\
nTST is %d\n",tseek);
printf("Avgmv is %.2f\n",avgmv);
}
OUTPUT:-
[cse22151@cseserver os]$ ./a.out
Enter the number of requests
3
Enter the disk read requests
30 60 40
Enter head position
50
FCFS sequence is
30->60->40->
TST is 70
Avgmv is 23.33
[cse22151@cseserver os]$
//9.b sstf disk scheduler
#include<stdlib.h>
#include<limits.h>
int main(){
int req[20],n,hdpos,tseek=0,i,temp[20],min,dist,index,cpos,cnt=0;
float avgmv;
printf("Enter the number of requests\n");
scanf("%d",&n);
printf("Enter the disk read requests\n");
for(i=0;i<n;i++){
temp[i]=req[i];
scanf("%d",&req[i]);
}
printf("Enter head position\n");
scanf("%d",&hdpos);
printf("SSTF sequence is\n");
cpos=hdpos;
while(cnt<n){
min=INT_MAX;
for(i=0;i<n;i++){
if(temp[i]!=-1){
dist=abs(req[i]-cpos);
if(min>dist){
min=dist;
index=i; }
}
}
tseek+=min;
temp[index]=-1;
cnt++;
cpos=req[index];
printf("%d->",req[index]);
if(cnt==n)
break;
}
avgmv=(float)tseek/n; printf("\
nTST is %d\n",tseek);
printf("Avgmv is %.2f\n",avgmv);
}
OUTPUT:-
[cse22151@cseserver os]$ ./a.out
Enter the number of requests
3
Enter the disk read requests
30 60 40
Enter head position
50
SSTF sequence is
60->40->30->TST is 40
Avgmv is 13.33
[cse22151@cseserver os]$