0% found this document useful (0 votes)
14 views19 pages

Ex 8

The document outlines various C programs implementing algorithms for deadlock avoidance, detection, shared memory, paging, and threading. It includes detailed algorithms and source code for each implementation, demonstrating how to manage resources and processes in a computing environment. Each section concludes with sample inputs and outputs, confirming the successful execution of the programs.

Uploaded by

KD KARTHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Ex 8

The document outlines various C programs implementing algorithms for deadlock avoidance, detection, shared memory, paging, and threading. It includes detailed algorithms and source code for each implementation, demonstrating how to manage resources and processes in a computing environment. Each section concludes with sample inputs and outputs, confirming the successful execution of the programs.

Uploaded by

KD KARTHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

IMPLEMENT BANKERS ALGORITHM FOR DEADLOCK

AVOIDANCE

Ex. No:7

OBJECTIVE:

To write a C program to implement bankers algorithm for dead lock avoidance

ALGORITHM:

Step 1: Start the Program

Step 2: Get the values of resources and processes.

Step 3: Get the avail value.

Step 4: After allocation find the need value.

Step 5: Check whether its possible to allocate. If possible it is safe state

Step 6: If the new request comes then check that the system is in safety or not if we allow the

request.

Step 7: Stop the execution

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}}
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;i<=n;i++) {
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<=r)
k++;
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++)
for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;break;
}}}
if(j==0)
{
active[i]=0;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock willn't occur");
}
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");

getch();
}
OUTPUT:
Enter the no of resources: 4
Enter the no of resource classes: 3
Enter the total existed resources in each class: 3 2 2
Enter the allocated resources: 1 0 0 5 1 1 2 1 1 0 0 2
Enter the process making the new request: 2
Enter the requested resource: 1 1 2
Enter the processes which are n blocked or running: Process 1: 1 2
Process 3: 1 0
Process 4: 1 0
Deadlock will occur

RESULT :

Thus the program was executed successfully

IMPLEMENT AN ALGORITHM FOR DEAD LOCK DETECTION


Ex. No: 8

OBJECTIVE:

To write a C program to implement Deadlock Detection algorithm

ALGORITHM:

Step 1: Start the Program

Step 2: Get the values of resources and processes.

Step 3: Get the avail value..


Step 4: After allocation find the need value.

Step 5: Check whether its possible to allocate.

Step 6: If it is possible then the system is in safe state.

Step 7: Stop the execution


#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show(); void cal();
void main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
getch();
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}

}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t"); for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t"); if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)

{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{ c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
//printf("\nP%d",i); if(finish[i]==1)
{
i=n;
}}}}}}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i; j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n"); for(i=0;i<n;i++)
{
}
}
else
{
printf("P%d\t",dead[i]);
}
}

output
Enter the no. Of processes 3
Enter the no of resources instances 3
Enter the max matrix
368
433
344
Enter the allocation matrix
333
203
124
Enter the available resources
120
Process allocation max available
P1 333 368 120
P2 203 433
P3 124 344
System is in deadlock and deadlock process are
P0 p1 p2

RESULT :

Thus the program was executed successfully


10. SHARED MEMORY AND IPC

Ex. No. 10

OBJECTIVE:

To write a C program to implement shared memory and inter process communication.

ALGORITHM:

Step 1: Start the Program

Step 2: Obtain the required process id

Step 3: Increment the *ptr=*ptr+1;

Step 4: Print the process identifier.

Step 5: check the values of sem_num, sem_op, sem_flg.

Step 6: Stop the execution.


SOURCE CODE:

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h>
int main()
{
int id,semid,count=0,i=1,j;
int *ptr;
id=shmget(8078,sizeof(int),IPC_CREAT|0666);
ptr=(int *)shmat(id,NULL,0);
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}u;
struct sembuf sem;
semid=semget(1011,1,IPC_CREAT|0666);
ushort a[1]={1};
u.array=a;
semctl(semid,0,SETALL,u);
while(1)
{
sem.sem_num=0;
sem.sem_op=-1;
sem.sem_flg=0;
semop(semid,&sem,1);
*ptr=*ptr+1;
printf("process id:%d countis :%d \n",getpid(),*ptr);
for(j=1;j< =1000000;j++)
{
sem.sem_num=0;
sem.sem_op=+1;
sem.sem_flg=0;
semop(semid,&sem,1);
}
}
shmdt(ptr);
}
SAMPLE INPUT AND OUTPUT:
enter Process id 16338992
pprocess
10
process 1

RESULT :

Thus the program was executed successfully


IMPLEMENT PAGING TECHNIQUE OF MEMORY MANAGEMENT

Ex. No. 11

OBJECTIVE:

To write a C program to implement the concept of Paging

ALGORITHM:

Step 1: Read all the necessary input from the keyboard.

Step 2: Pages – Logical memory is broken into fixed- sized blocks.

Step 3: Frames – physical memory is broken into fixed – sized blocks

Step 4: Calculate the physical address using the following Physical address=(frame number *

Frame size)+ offset

Step 5: Display the physical address.

Step 6: Stop the execution


SOURCE CODE:

#include<stdio.h>

#include<conio.h>

main()

int np,ps,i;

int *sa;

printf("enter how many pages\n");

scanf("%d",&np);

printf("enter the page size \n");

scanf("%d",&ps);

sa=(int*)malloc(2*np);

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

sa[i]=(int)malloc(ps);

printf("page%d\t address %u\n",i+1,sa[i]);

getch();

SAMPLE INPUT AND OUTPUT:

Input:

Enter how many pages: 5


Enter the page size: 4

Output:
Page1 Address: 1894
Page2 Address: 1902
Page3 Address: 1910
Page4 Address: 1918
Page5 Address: 1926

RESULT:

Thus the program was executed successfully.


IMPLEMENT THREADING & SYNCHRONIZATION
APPLICATIONS

EX NO: 12
OBJECTIVE:

To write a C program to implement Threading & Synchronization

ALGORITHM:

Step 1: Start the Program

Step 2: Initialize the process thread array.

Step 3: Print the job started status.

Step 4: Print the job finished status.

Step 5: Start the main function

Step 6: Check for the process creation if not print error

message. Step 7: Stop the execution


SOURCE CODE:
#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("\n can't create thread :[%s]", strerror(err)); i++;

pthread_join(tid[0], NULL);

pthread_join(tid[1], NULL);

return 0;

}
SAMPLE OUTPUT:

Job 1 started

Job 1finished

can't create thread :

RESULT:

Thus the program was executed successfully.

You might also like