You are on page 1of 30

Program to implement system calls for file manipulation:

#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];

printf("Enter text to write in the file:\n");

n= read(0, buff, 50);

fd=open("file",O_CREAT | O_RDWR, 0777);

write(fd, buff, n);

write(1, buff, n);

int close(int fd);

return 0;

Output:

gpw@gpw-virtual-machine:~$ gcc OS-2.c

gpw@gpw-virtual-machine:~$ ./a.out

Enter text to write in the file:

GPW

GPW
Program to implement concept of Multiprocessing application:

#include<unistd.h>
#include<stdio.h>
int main()
{
int n1 = fork();
int n2 = fork();

if(n1>0 && n2>0)


{
printf("Parent\n");
printf("%d%d\n",n1,n2);
printf("My id is %d\n",getpid());
}
else if(n1==0 && n2>0)
{
printf("First Child\n");
printf("%d%d\n",n1,n2);
printf("My id is %d\n",getpid());
}
else if(n2==0 && n1>0)
{
printf("Second Child\n");
printf("%d%d\n",n1,n2);
printf("My id is %d\n",getpid());
}
else
{
printf("Third Child\n");
printf("%d%d\n",n1,n2);
printf("My id is %d\n",getpid());
}
return 0;
}

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:

gpw@gpw-virtual-machine:~$ gcc OS-3.2.c


OS-3.2.c: In function ‘main’:
OS-3.2.c:23:14: warning: too many arguments for format [-Wformat-extra-args]
23 | printf("main(): creating Thread,\n",i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
OS-3.2.c:24:55: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
24 | rc = pthread_create(&threads[i],NULL,PrintHello,(void*)i);
| ^
gpw@gpw-virtual-machine:~$ ./a.out
main(): creating Thread,
main(): creating Thread,
Hello World! Thread Id,0
main(): creating Thread,
Hello World! Thread Id,1
main(): creating Thread,
main(): creating Thread,
Hello World! Thread Id,2
Hello World! Thread Id,3
Hello World! Thread Id,4
Program for FCFS:
#include<stdio.h>

#include<malloc.h>

void main()

int i, n, *bt, *wt, *tat;

float avgtat, avgwt;

printf("\n Enter the number of processes : ");

scanf("%d", &n);

bt = (int*)malloc(n*sizeof(int));

wt = (int*)malloc(n*sizeof(int));

tat = (int*)malloc(n*sizeof(int));

printf("\n Enter the burst time for each process \n");

for(i=0; i<n; i++)

printf(" Burst time for P%d : ", i);

scanf("%d", &bt[i]);

wt[0] = 0;

tat[0] = bt[0];

for(i=1; i<n; i++)

wt[i] = wt[i-1] + bt[i-1];

tat[i] = wt[i] + bt[i];

for(i=0; i<n; i++)

avgwt += wt[i];

avgtat += tat[i];

avgwt = avgwt/n;
avgtat = avgtat/n;

printf("\n PROCESS \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n");

printf("--------------------------------------------------------------\n");

for(i=0; i<n; i++)

printf(" P%d \t\t %d \t\t %d \t\t %d \n", i, bt[i], wt[i], tat[i]);

printf("\n Average Waiting Time = %f \n Average Turnaround Time = %f \n", avgwt, avgtat);

printf("\n GAANT CHART \n");

printf("---------------\n");

for(i=0; i<n; i++)

printf(" %d\t|| P%d ||\t%d\n", wt[i], i, tat[i]);

Output:

gpw@gpw-virtual-machine:~$ gcc OS-4.1.c

gpw@gpw-virtual-machine:~$ ./a.out

Enter the number of processes : 5

Enter the burst time for each process

Burst time for P0 : 10

Burst time for P1 : 1

Burst time for P2 : 2

Burst time for P3 : 1

Burst time for P4 : 5


PROCESS BURST TIME WAITING TIME TURNAROUND TIME

--------------------------------------------------------------

P0 10 0 10

P1 1 10 11

P2 2 11 13

P3 1 13 14

P4 5 14 19

Average Waiting Time = 9.600000

Average Turnaround Time = 13.400000

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);

printf("\nServer:Reading data from pipe\n\n");

while(c!=';')

write(1,&c,1);

read(fd[0],&c,1);

printf("\n\nServer:Finished reading data from pipe\n");

close(fd[0]);

else

printf("\nClient:Writing data into pipe\n");

close(fd[0]);

printf("\nEnter data=>");

while(c!=';')

c=getchar();

write(fd[1],&c,1);
}

printf("\nClient:Finished writing data into pipe\n");

close(fd[1]);

Output:

gpw@gpw-virtual-machine:~$ gcc OS-5.1.c

gpw@gpw-virtual-machine:~$ ./a.out

Client:Writing data into pipe

Enter data=>GPW

Server:Reading data from pipe

GPW
Program on Thread Synchronization:
#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

int MAX = 10;

int count = 1;

pthread_mutex_t thr;

pthread_cond_t cond;

void *even(void *arg){

while(count < MAX) {

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); }

void *odd(void *arg){

while(count < MAX) {

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_create(&thread1, 0, &even, NULL);

pthread_create(&thread2, 0, &odd, NULL);

pthread_join(thread1, 0);

pthread_join(thread2, 0);

pthread_mutex_destroy(&thr);

pthread_cond_destroy(&cond);

return 0;

Output:

gpw@gpw-virtual-machine:~$ gcc OS-5.2.c

gpw@gpw-virtual-machine:~$ ./a.out

10
Program for Bankers Algorithm:
#include <stdio.h>

int current[5][5], maximum_claim[5][5], available[5];

int allocation[5] = {0, 0, 0, 0, 0};

int maxres[5], running[5], safe = 0;

int counter = 0, i, j, exec, resources, processes, k = 1;

int main()

printf("\nEnter number of processes: ");

scanf("%d", &processes);

for (i = 0; i < processes; i++)

running[i] = 1;

counter++;

printf("\nEnter number of resources: ");

scanf("%d", &resources);

printf("\nEnter Claim Vector:");

for (i = 0; i < resources; i++)

scanf("%d", &maxres[i]);

printf("\nEnter Allocated Resource Table:\n");

for (i = 0; i < processes; i++)

{
for(j = 0; j < resources; j++)

scanf("%d", &current[i][j]);

printf("\nEnter Maximum Claim Table:\n");

for (i = 0; i < processes; i++)

for(j = 0; j < resources; j++)

scanf("%d", &maximum_claim[i][j]);

printf("\nThe Claim Vector is: ");

for (i = 0; i < resources; i++)

printf("\t%d", maxres[i]);

printf("\nThe Allocated Resource Table:\n");

for (i = 0; i < processes; i++)

for (j = 0; j < resources; j++)

printf("\t%d", current[i][j]);

printf("\n");

}
printf("\nThe Maximum Claim Table:\n");

for (i = 0; i < processes; i++)

for (j = 0; j < resources; j++)

printf("\t%d", maximum_claim[i][j]);

printf("\n");

for (i = 0; i < processes; i++)

for (j = 0; j < resources; j++)

allocation[j] += current[i][j];

printf("\nAllocated resources:");

for (i = 0; i < resources; i++)

printf("\t%d", allocation[i]);

for (i = 0; i < resources; i++)

available[i] = maxres[i] - allocation[i];

printf("\nAvailable resources:");

for (i = 0; i < resources; i++)


{

printf("\t%d", available[i]);

printf("\n");

while (counter != 0)

safe = 0;

for (i = 0; i < processes; i++)

if (running[i])

exec = 1;

for (j = 0; j < resources; j++)

if (maximum_claim[i][j] - current[i][j] > available[j])

exec = 0;

break;

if (exec)

printf("\nProcess%d is executing\n", i + 1);

running[i] = 0;

counter--;

safe = 1;

for (j = 0; j < resources; j++)

available[j] += current[i][j];
}

break;

if (!safe)

printf("\nThe processes are in unsafe state.\n");

break;

else

printf("\nThe process is in safe state");

printf("\nAvailable vector:");

for (i = 0; i < resources; i++)

printf("\t%d", available[i]);

printf("\n");

return 0;

Output:

Enter the number of processes : 5

Enter the number of resources : 4

Enter the Max Matrix for each process :

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

Enter the allocation for each process :

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

Enter the Available Resources : 2 3 1 1

Need Matrix:-

1 0 0 0

1 5 3 1

2 2 2 2

1 0 0 2

0 0 1 2

Process P1 runs to completion!

Process P3 runs to completion!

Process P2 runs to completion!

Process P4 runs to completion!

Process P5 runs to completion!

The system is in a safe state!!

Safe Sequence : < P1 P3 P2 P4 P5 >


Program on First-Fit Algorithm:
#include<stdio.h>

#include<process.h>

void main()

int a[20],p[20],i,j,n,m;

clrscr();

printf("Enter number of Blocks:");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter the B%d Block size:",i);

scanf("%d",&a[i]);

printf("\nEnter number of Process:");

scanf("%d",&m);

for(i=0;i<m;i++)

printf("Enter the size of P%d Process:",i);

scanf("%d",&p[i]);

printf("\n");

for(i=0;i<n;i++)

for(j=0;j<m;j++)

if(p[j]<=a[i])

printf("The Process P%d allocated to %d i.e B%d Block\n",j,a[i],i);

p[j]=10000;

break;
}

for(j=0;j<m;j++)

if(p[j]!=10000)

printf("\nThe Process P%d is not allocated\n",j);

getch();

Output:

Enter number of Blocks:5

Enter the B0 Block size:500

Enter the B1 Block size:400

Enter the B2 Block size:300

Enter the B3 Block size:200

Enter the B4 Block size:100

Enter number of Process:5

Enter the size of P0 Process:100

Enter the size of P1 Process:350

Enter the size of P2 Process:400

Enter the size of P3 Process:150

Enter the size of P4 Process:200

The Process P0 allocated to 500 i.e B0 Block

The Process P1 allocated to 400 i.e B1 Block

The Process P3 allocated to 300 i.e B2 Block

The Process P4 allocated to 200 i.e B3 Block

The Process P2 is not allocated


Program on Best-Fit Algorithm:
#include<stdio.h>

#include<conio.h>

#define max 25

void main()

int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;

static int bf[max],ff[max];

clrscr();

printf("\nEnter the number of blocks:");

scanf("%d",&nb);

printf("Enter the number of process:");

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 process :-\n");

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("\nProcess No\tProcess Size \tBlock No\tBlock Size\tFragment");

for(i=1;i<=nf && ff[i]!=0;i++)

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:

Enter the number of blocks:5

Enter the number of process:5

Enter the size of the blocks:-

Block 1:500

Block 2:400

Block 3:300

Block 4:200

Block 5:100

Enter the size of the process :-

P1:100

P2:350
P3:400

P4:150

P5:200

Process No Process Size Block No Block Size Fragment

P1 100 5 100 0

P2 350 2 400 50

P3 400 1 500 100

P4 150 4 200 50

P5 200 3 300 100


Program on FIFO:
#include<stdio.h>

#include<conio.h>

void main()

int i,j,n,a[50],frame[10],no,k,avail,count=0;

clrscr();

printf("\nEnter the number of pages:");

scanf("%d",&n);

printf("\nEnter the page number:\n");

for(i=1;i<=n;i++)

scanf("%d",&a[i]);

printf("\nEnter the frame size:");

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");

printf("\nPage fault = %d",count);

getch();

}
Output:

Enter the number of pages:20

Enter the page number:

70120304230321201701

Enter the frame size:3

String Page frames

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;

printf("Enter the number of Requests\n");

scanf("%d",&n);

printf("Enter the Requests sequence\n");

for(i=0;i<n;i++)

scanf("%d",&RQ[i]);

printf("Enter initial head position\n");

scanf("%d",&initial);

for(i=0;i<n;i++)

TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);

initial=RQ[i];

printf("Total head moment is %d",TotalHeadMoment);

return 0;

Output:

gpw@gpw-virtual-machine:~$ gcc FCFS.c

gpw@gpw-virtual-machine:~$ ./a.out

Enter the number of Requests


8
Enter the Requests sequence
98 183 37 122 14 124 65 67
Enter initial head position
53
Total head moment is 640
Program on File Handling using Shell Script:
ans='y'

while test $ans='y'

do

echo "1. add"

echo "2. delete"

echo "3. modify"

echo "4. display"

echo "5. exit"

read ch

case $ch in

1) echo "Enter rno:"

read rno

echo "enter name"

read name

echo $rno $name>>file

;;

2) echo "Enter roll to delete"

read rno

if grep $rno file

then

grep -v $rno file>temp

rm file

my temp file

echo "Record is deleted"

fi

;;

3) echo "Enter roll to modify"

read rno
echo "Enter roll to delete"

read rno

if grep $rno file

then

grep -v $rno file>temp

rm file

rm temp file

echo "record delete"

fi

echo "enter roll"

read rno

echo "enter name"

read name

echo $rno

$name>>file

;;

4) echo "the content of file are"

cat file

;;

5) exit

;;

esac

done
Output:

gpw@gpw-virtual-machine:~$ chmod +x rc.sh

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

the content of file are

26 GPW

1. add

2. delete

3. modify

4. display

5. exit

You might also like