0% found this document useful (0 votes)
15 views9 pages

Split 2739392403791560704

The document contains several programming exercises related to operating systems, including shared memory management, the Banker's algorithm for deadlock avoidance, deadlock detection algorithms, threading and synchronization applications, and the first fit algorithm for memory management. Each section includes an aim, algorithm steps, and C program implementations with sample outputs. The results indicate successful execution and implementation of the respective algorithms and concepts.

Uploaded by

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

Split 2739392403791560704

The document contains several programming exercises related to operating systems, including shared memory management, the Banker's algorithm for deadlock avoidance, deadlock detection algorithms, threading and synchronization applications, and the first fit algorithm for memory management. Each section includes an aim, algorithm steps, and C program implementations with sample outputs. The results indicate successful execution and implementation of the respective algorithms and concepts.

Uploaded by

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

#include <sys/types.

h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
for (s = shm; *s != '\0'; s++)
putchar(*s);
putchar('\n');
*shm = '*';
exit(0);
}

Output:
[gokul@localhost ~]$ ipcs -m

------ Shared Memory Segments --------


key shmid owner perms bytes nattch status
0x0000162e 98307 gokul 666 27 1

[gokul@localhost ~]$ cc shmclient1.c


[gokul@localhost ~]$ ./a.out
abcdefghijklmnopqrstuvwxyz

Result :
Thus the above program executed successfully.

28
Ex. No: 8 BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE

Aim:
To Simulate 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 it‟s possible to allocate.
Step 6: If it is possible then the system is in safe state.
Step 7: Else system is not in safety state.
Step 8: If the new request comes then check that the system is in safety or not if we allow the request.
Step 9: Stop the program.
Program :
#include<stdio.h>
main()
{
int r[1][10],av[1][10];
int all[10][10],max[10][10],ne[10][10],w[10],safe[10];
int i=0,j=0,k=0,l=0,np=0,nr=0,count=0,cnt=0;
printf("enter the number of processes in a system");
scanf("%d",&np);
printf("enter the number of resources in a system");
scanf("%d",&nr);
for(i=1;i<=nr;i++){
printf("\n enter the number of instances of resource R%d " ,i);
scanf("%d",&r[0][i]);
av[0][i]=r[0][i];
}
for(i=1;i<=np;i++)
for(j=1;j<=nr;j++)
all[i][j]=ne[i][j]=max[i][j]=w[i]=0;
printf(" \nEnter the allocation matrix");
for(i=1;i<=np;i++)
{ printf("\n");
for(j=1;j<=nr;j++)
{
scanf("%d",&all[i][j]);
av[0][j]=av[0][j]-all[i][j];
}
}
printf("\nEnter the maximum matrix");
for(i=1;i<=np;i++)
{ printf("\n");
for(j=1;j<=nr;j++)
{
scanf("%d",&max[i][j]);
}
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nr;j++)
{
ne[i][j]=max[i][j]-all[i][j];

29
}
}
for(i=1;i<=np;i++)
{
printf("pocess P%d",i);
for(j=1;j<=nr;j++)
{
printf("\n allocated %d\t",all[i][j]);
printf("maximum %d\t",max[i][j]);
printf("need %d\t",ne[i][j]);
}
printf("\n_________________________\n");
}
printf("\nAvailability");
for(i=1;i<=nr;i++)
printf("R%d %d\t",i,av[0][i]);

printf("\n____________");
printf("\n safe sequence");
for(count=1;count<=np;count++)
{
for(i=1;i<=np;i++)
{ cnt=0;
for(j=1;j<=nr;j++)
{
if(ne[i][j]<=av[0][j] && w[i]==0)
cnt++;
}
if(cnt==nr)
{
k++;
safe[k]=i;
for(l=1;l<=nr;l++)
av[0][l]=av[0][l]+all[i][l];
printf("\n P%d ",safe[k]);
printf("\t Availability");
for(l=1;l<=nr;l++)
printf("R%d %d\t",l,av[0][l]);
w[i]=1;
}
}
}
}

Output:
Enter the number of resources in a system 4
Enter the number of instances of resource R1 2
Enter the number of instances of resource R2 2
Enter the number of instances of resource R3 2
Enter the number of instances of resource R4 2
Enter the allocation matrix
10110

11000
00010
00000

30
Enter the maximum matrix
10100
10000
00001
pocess P1
allocated 1 maximum 0 need -1
allocated 0 maximum 0 need 0
allocated 1 maximum 0 need -1
allocated 1 maximum 0 need -1
_________________________
pocess P2
allocated 0 maximum 1 need 1
allocated 1 maximum 0 need -1
allocated 1 maximum 1 need 0
allocated 0 maximum 0 need 0
_________________________
pocess P3
allocated 0 maximum 0 need 0
allocated 0 maximum 1 need 1
allocated 0 maximum 0 need 0
allocated 0 maximum 0 need 0
_________________________
pocess P4
allocated 0 maximum 0 need 0
allocated 1 maximum 0 need -1
allocated 0 maximum 0 need 0
allocated 0 maximum 0 need 0
_______________________
AvailabilityR1 1 R2 0 R3 0 R4 1
____________
safe sequence
P1 AvailabilityR1 2 R2 0 R3 1 R4 2
P2 AvailabilityR1 2 R2 1 R3 2 R4 2
P3 AvailabilityR1 2 R2 1 R3 2 R4 2
P4 AvailabilityR1 2 R2 2 R3 2 R4 2

Result:
Thus the banker‟s algorithm for deadlock avoidance was simulated.

31
Ex. No: 9 IMPLEMENT AN ALGORITHM FOR DEAD LOCK DETECTION

Aim:
To implement an algorithm for deadlock detection.

Algorithm :
Simply detects the existence of a Cycle:
1. Start at any vertex finds all its immediate neighbors.
2. From each of these find all immediate neighbors, etc.
3. Until a vertex repeats (there is a cycle) or one cannot continue (there is no cycle).
4. Stop.
On a copy of the graph:
1. See if any Processes NEEDs can all be satisfied.
2. If so satisfy the needs with holds and remove that Process and all the Resources it holds from the graph.
3. If any Process are left Repeat step a
4. If all Processes are finally removed by this procedure there is no Deadlock in the original graph, if not
there is.
5. Stop.
Program :
#include<stdio.h>
static int mark[20];
int i,j,np,nr;

int main()
{
int alloc[10][10],request[10][10],avail[10],r[10],w[10];

printf("\nEnter the no of process: ");


scanf("%d",&np);
printf("\nEnter the no of resources: ");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
printf("\nTotal Amount of the Resource R%d: ",i+1);
scanf("%d",&r[i]);
}
printf("\nEnter the request matrix:");
for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&request[i][j]);

printf("\nEnter the allocation matrix:");


for(i=0;i<np;i++)
for(j=0;j<nr;j++)
scanf("%d",&alloc[i][j]);
/*Available Resource calculation*/
for(j=0;j<nr;j++)
{
avail[j]=r[j];
for(i=0;i<np;i++)
{
avail[j]-=alloc[i][j];

}
}

32
//marking processes with zero allocation
for(i=0;i<np;i++)
{
int count=0;
for(j=0;j<nr;j++)
{
if(alloc[i][j]==0)
count++;
else
break;
}
if(count==nr)
mark[i]=1;
}
// initialize W with avail
for(j=0;j<nr;j++)
w[j]=avail[j];
//mark processes with request less than or equal to W
for(i=0;i<np;i++)
{
int canbeprocessed=0;
if(mark[i]!=1)
{
for(j=0;j<nr;j++)
{
if(request[i][j]<=w[j])
canbeprocessed=1;
else
{
canbeprocessed=0;
break;
}
}
if(canbeprocessed)
{
mark[i]=1;

for(j=0;j<nr;j++)
w[j]+=alloc[i][j];
}
}
}
//checking for unmarked processes
int deadlock=0;
for(i=0;i<np;i++)
if(mark[i]!=1)
deadlock=1;
if(deadlock)
printf("\n Deadlock detected");
else
printf("\n No Deadlock possible");
}

33
Output:
Enter the no of process: 4
Enter the no of resources: 5

Total Amount of the Resource R1: 2


Total Amount of the Resource R2: 1
Total Amount of the Resource R3: 1
Total Amount of the Resource R4: 2
Total Amount of the Resource R5: 1

Enter the request matrix:0 1 0 0 1


00101
00001
10101

Enter the allocation matrix:1 0 1 1 0


11000
00010
00000

Deadlock detected

Result:
Thus the deadlock detection algorithm was implemented.

34
Ex. No: 10 IMPLEMENT THREADING & SYNCHRONIZATION APPLICATIONS

Aim:
To implement threading & synchronization applications using c
Algorithm :
1. Start the program
2. Read the Input
3. Allocate the memory
4. Process the input
5. Checking error
6. Print result
Program :
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
int counter;
void* trythis(void *arg)
{
unsigned long i = 0;
counter += 1;
printf("\n Job %d has started\n", counter);
for(i=0; i<(0xFFFFFFFF);i++);
printf("\n Job %d has finished\n", counter);
return NULL;
}
int main(void)
{
int i = 0;
int error;
while(i < 2)
{
error = pthread_create(&(tid[i]), NULL, &trythis, NULL);
if (error != 0)
printf("\nThread can't be created : [%s]", strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
Output :
gokul@localhost ~]$ cc filename.c -lpthread
Job 1 has started
Job 2 has started
Job 2 has finished
Job 2 has finished

Result
Thus the threading and synchronization concept was implemented

35
Ex: No: 11a IMPLEMENTATION OF FIRST FIT ALGORITHM

Aim:

To write a program to implement first fit algorithm for memory management.


Algorithm:
Step1: Start the process.
Step 2: Declare the size.
Step 3: Get the number of processes to be inserted.
Step 4: Allocate the first hole that is big enough for searching.
Step 5: Start the beginning of the set of holes.
Step 6: If not start at the hole, which is sharing the previous first fit search end.
Step 7: Compare the hole.
Step 8: If large enough, then stop searching in the procedure.
Step 9: Display the values.
Step 10: Terminate the process.
Program:
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m;
clrscr();
printf("Enter no of blocks:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %dst block size:",i);
scanf("%d",&a[i]);
}
printf("\nEnter the no of process:");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nEnter the size of %dst process:",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
printf("The process %d allocated %d\n",j,i);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("\n The process is not allocated \n",j);
}

36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy