Professional Documents
Culture Documents
Os All
Os All
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
int main()
int n,fd;
char buff[50];
return 0;
Output:
gpw@gpw-virtual-machine:~$ ./a.out
GPW
GPW
Program to implement concept of Multiprocessing application:
#include<unistd.h>
#include<stdio.h>
int main()
{
int n1 = fork();
int n2 = fork();
OUTPUT:
gpw@gpw-virtual-machine:~$ gcc OS-3.1.c
gpw@gpw-virtual-machine:~$ ./a.out
Parent
27762777
My id is 2775
First Child
02778
My id is 2776
Second Child
27760
My id is 2777
gpw@gpw-virtual-machine:~$ Third Child
00
My id is 2778
Program to implement concept of Multithreading:
#include<unistd.h>
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#define NUM_THREADS 5
void *PrintHello(void*threadid)
{
long tid;
tid = (long) threadid;
printf("Hello World! Thread Id,%ld\n",tid);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
int rc;
int i;
for(i=0;i<NUM_THREADS;i++)
{
printf("main(): creating Thread,\n",i);
rc = pthread_create(&threads[i],NULL,PrintHello,(void*)i);
if(rc < 0)
{
printf("Error : unable to create thread,%d\n",rc);
exit(-1);
}
}
pthread_exit(NULL);
}
OUTPUT:
#include<malloc.h>
void main()
scanf("%d", &n);
bt = (int*)malloc(n*sizeof(int));
wt = (int*)malloc(n*sizeof(int));
tat = (int*)malloc(n*sizeof(int));
scanf("%d", &bt[i]);
wt[0] = 0;
tat[0] = bt[0];
avgwt += wt[i];
avgtat += tat[i];
avgwt = avgwt/n;
avgtat = avgtat/n;
printf("--------------------------------------------------------------\n");
printf("\n Average Waiting Time = %f \n Average Turnaround Time = %f \n", avgwt, avgtat);
printf("---------------\n");
Output:
gpw@gpw-virtual-machine:~$ ./a.out
--------------------------------------------------------------
P0 10 0 10
P1 1 10 11
P2 2 11 13
P3 1 13 14
P4 5 14 19
GAANT CHART
---------------
0 || P0 || 10
10 || P1 || 11
11 || P2 || 13
13 || P3 || 14
14 || P4 || 19
Program on Process Synchronization:
#include<stdio.h>
#include<unistd.h>
int main()
int pid,fd[2];
char c;
pipe(fd);
pid=fork();
if(pid==0)
close(fd[1]);
read(fd[0],&c,1);
while(c!=';')
write(1,&c,1);
read(fd[0],&c,1);
close(fd[0]);
else
close(fd[0]);
printf("\nEnter data=>");
while(c!=';')
c=getchar();
write(fd[1],&c,1);
}
close(fd[1]);
Output:
gpw@gpw-virtual-machine:~$ ./a.out
Enter data=>GPW
GPW
Program on Thread Synchronization:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond;
pthread_mutex_lock(&thr);
while(count % 2 != 0) {
pthread_cond_wait(&cond, &thr);
printf("%d\n", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
pthread_exit(0); }
pthread_mutex_lock(&thr);
while(count % 2 != 1) {
pthread_cond_wait(&cond, &thr); }
printf("%d\n", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
pthread_exit(0);
int main(){
pthread_t thread1;
pthread_t thread2;
pthread_mutex_init(&thr, 0);
pthread_cond_init(&cond, 0);
pthread_join(thread1, 0);
pthread_join(thread2, 0);
pthread_mutex_destroy(&thr);
pthread_cond_destroy(&cond);
return 0;
Output:
gpw@gpw-virtual-machine:~$ ./a.out
10
Program for Bankers Algorithm:
#include <stdio.h>
int main()
scanf("%d", &processes);
running[i] = 1;
counter++;
scanf("%d", &resources);
scanf("%d", &maxres[i]);
{
for(j = 0; j < resources; j++)
scanf("%d", ¤t[i][j]);
scanf("%d", &maximum_claim[i][j]);
printf("\t%d", maxres[i]);
printf("\t%d", current[i][j]);
printf("\n");
}
printf("\nThe Maximum Claim Table:\n");
printf("\t%d", maximum_claim[i][j]);
printf("\n");
allocation[j] += current[i][j];
printf("\nAllocated resources:");
printf("\t%d", allocation[i]);
printf("\nAvailable resources:");
printf("\t%d", available[i]);
printf("\n");
while (counter != 0)
safe = 0;
if (running[i])
exec = 1;
exec = 0;
break;
if (exec)
running[i] = 0;
counter--;
safe = 1;
available[j] += current[i][j];
}
break;
if (!safe)
break;
else
printf("\nAvailable vector:");
printf("\t%d", available[i]);
printf("\n");
return 0;
Output:
For process P1 : 1 0 1 2
For process P2 : 1 5 3 2
For process P3 : 2 6 5 6
For process P4 : 2 7 5 4
For process P5 : 0 0 2 6
For process P1 : 0 0 1 2
For process P2 : 0 0 0 1
For process P3 : 0 4 3 4
For process P4 : 1 7 5 2
For process P5 : 0 0 1 4
Need Matrix:-
1 0 0 0
1 5 3 1
2 2 2 2
1 0 0 2
0 0 1 2
#include<process.h>
void main()
int a[20],p[20],i,j,n,m;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&p[i]);
printf("\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(p[j]<=a[i])
p[j]=10000;
break;
}
for(j=0;j<m;j++)
if(p[j]!=10000)
getch();
Output:
#include<conio.h>
#define max 25
void main()
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
clrscr();
scanf("%d",&nb);
scanf("%d",&nf);
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
for(i=1;i<=nf;i++)
printf("P%d:",i);
scanf("%d",&f[i]);
for(i=1;i<=nf;i++)
for(j=1;j<=nb;j++)
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
ff[i]=j;
lowest=temp;
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
printf("\nP%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
Output:
Block 1:500
Block 2:400
Block 3:300
Block 4:200
Block 5:100
P1:100
P2:350
P3:400
P4:150
P5:200
P1 100 5 100 0
P2 350 2 400 50
P4 150 4 200 50
#include<conio.h>
void main()
int i,j,n,a[50],frame[10],no,k,avail,count=0;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\nString\t\tPage frames\n");
for(i=1;i<=n;i++)
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
}
if(avail==0)
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
else
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
printf("\n");
getch();
}
Output:
70120304230321201701
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0 2 0 1
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3 0 2 3
2 0 2 3
1 0 1 3
2 0 1 2
0 0 1 2
1 0 1 2
7 7 1 2
0 7 0 2
1 7 0 1
Page fault = 15
Program on Disk Sheduling(FCFS):
#include<stdio.h>
#include<stdlib.h>
int main()
int RQ[100],i,n,TotalHeadMoment=0,initial;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
scanf("%d",&initial);
for(i=0;i<n;i++)
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
return 0;
Output:
gpw@gpw-virtual-machine:~$ ./a.out
do
read ch
case $ch in
read rno
read name
;;
read rno
then
rm file
my temp file
fi
;;
read rno
echo "Enter roll to delete"
read rno
then
rm file
rm temp file
fi
read rno
read name
echo $rno
$name>>file
;;
cat file
;;
5) exit
;;
esac
done
Output:
gpw@gpw-virtual-machine:~$ ./rc.sh
1. add
2. delete
3. modify
4. display
5. exit
Enter rno:
26
enter name
GPW
1. add
2. delete
3. modify
4. display
5. exit
26 GPW
1. add
2. delete
3. modify
4. display
5. exit