Os Lab
Os Lab
Aim
To generate 25 fibonacci numbers and determine prime amongst them using pipe.
Algorithm
1. Declare a array to store fibonacci numbers.
2. Declare a array pfd with two elements for pipe descriptors.
3. Create pipe on pfd using pipe function call.
a. If return value is -1 then stop
4. Using fork system call, create a child process.
5. Let the child process generate 25 fibonacci numbers and store them in a array.
6. Write the array onto pipe using write system call.
7. Block the parent till child completes using wait system call.
8. Store fibonacci numbers written by child from the pipe in an array using read system call
9. Inspect each element of the fibonacci array and check whether they are prime
a. If prime then print the fibonacci term.
10. Stop
Coding
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
main()
{
pid_t pid;
int pfd[2];
int i,j,flg,f1,f2,f3;
static unsigned int ar[25],br[25];
if(pipe(pfd) == -1)
{
printf("Error in pipe");
exit(-1);
}
pid=fork();
if (pid == 0)
{
printf("Child process generates Fibonacci series\n" );
f1 = -1;
f2 = 1;
for(i = 0;i < 25; i++)
{
f3 = f1 + f2;
printf("%d\t",f3);
f1 = f2;
f2 = f3;
ar[i] = f3;
}
write(pfd[1],ar,25*sizeof(int));
}
else if (pid > 0)
{
wait(NULL);
read(pfd[0], br, 25*sizeof(int));
printf("\nParent prints Fibonacci that are Prime\n");
for(i = 0;i < 25; i++)
{
flg = 0;
if (br[i] <= 1)
flg = 1;
for(j=2; j<=br[i]/2; j++)
{
if (br[i]%j == 0)
{
flg=1;
break;
}
}
if (flg == 0)
printf("%d\t", br[i]);
}
printf("\n");
}
else
{
printf("Process creation failed");
exit(-1);
}
}
Output
[14CSE84@localhost Desktop]$ vi pipes.c
[14CSE84@localhost Desktop]$ cc pipes.c
[14CSE84@localhost Desktop]$ ./a.out
Child process generates Fibonacci series
0 1 1 2 3 5 8 13 21 34 55 89
144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657
46368
Parent prints Fibonacci that are Prime
2 3 5 13 89 233 1597 28657
Result
Thus fibonacci numbers that are prime is determined using IPC pipe.
Ex No: 4b INTER PROCESS COMMUNICATION USING MESSAGE QUEUE
Aim
To exchange message between server and client using message queue.
Algorithm
Server
1. Declare a structure mesgq with type and text fields.
2. Initialize key to 2013 (some random value).
3. Create a message queue using msgget with key & IPC_CREAT as parameter.
a. If message queue cannot be created then stop.
4. Initialize the message type member of mesgq to 1.
5. Do the following until user types Ctrl+D
a. Get message from the user and store it in text member.
b. Delete the newline character in text member.
c. Place message on the queue using msgsend for the client to read.
d. Retrieve the response message from the client using msgrcv function
e. Display the text contents.
6. Remove message queue from the system using msgctl with IPC_RMID as parameter.
7. Stop
Client
1. Declare a structure mesgq with type and text fields.
2. Initialize key to 2013 (same value as in server).
3. Open the message queue using msgget with key as parameter.
a. If message queue cannot be opened then stop.
4. Do while the message queue exists
a. Retrieve the response message from the server using msgrcv function
b. Display the text contents.
c. Get message from the user and store it in text member.
d. Delete the newline character in text member.
e. Place message on the queue using msgsend for the server to read.
5. Print "Server Disconnected".
6. Stop
Coding
Server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesgq
{
long type;
char text[200];
} mq;
main()
{
int msqid, len;
key_t key = 2013;
if((msqid = msgget(key, 0644|IPC_CREAT)) == -1)
{
perror("msgget");
exit(1);
}
printf("Enter text, ^D to quit:\n");
mq.type = 1;
while(fgets(mq.text, sizeof(mq.text), stdin) != NULL)
{
len = strlen(mq.text);
if (mq.text[len-1] == '\n')
mq.text[len-1] = '\0';
msgsnd(msqid, &mq, len+1, 0);
msgrcv(msqid, &mq, sizeof(mq.text), 0, 0);
printf("From Client: \"%s\"\n", mq.text);
}
msgctl(msqid, IPC_RMID, NULL);
}
Output
[14CSE84@localhost Desktop]$ vi mq.c
[14CSE84@localhost Desktop]$ cc mq.c
[14CSE84@localhost Desktop]$ ./a.out
Enter text, ^D to quit:
hellooo sakthi
Client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct mesgq
{
long type;
char text[200];
} mq;
main()
{
int msqid, len;
key_t key = 2013;
if ((msqid = msgget(key, 0644)) == -1)
{
printf("Server not active\n");
exit(1);
}
printf("Client ready :\n");
while (msgrcv(msqid, &mq, sizeof(mq.text), 0, 0) != -1)
{
printf("From Server: \"%s\"\n", mq.text);
fgets(mq.text, sizeof(mq.text), stdin);
len = strlen(mq.text);
if (mq.text[len-1] == '\n')
mq.text[len-1] = '\0';
msgsnd(msqid, &mq, len+1, 0);
}
printf("Server Disconnected\n");
}
Output
[14CSE84@localhost Desktop]$ vi mqq.c
[14CSE84@localhost Desktop]$ cc mqq.c
[14CSE84@localhost Desktop]$ ./a.out
Client ready :
From Server: "hellooo sakthi"
Result
Thus chat session between client and server was done using message queue.
Aim
To demonstrate communication between process using shared memory.
Algorithm
Server
1. Initialize size of shared memory shmsize to 27.
2. Initialize key to 2013 (some random value).
3. Create a shared memory segment using shmget with key & IPC_CREAT as parameter.
a. If shared memory identifier shmid is -1, then stop.
4. Display shmid.
5. Attach server process to the shared memory using shmmat with shmid as parameter.
a. If pointer to the shared memory is not obtained, then stop.
6. Clear contents of the shared region using memset function.
7. Write a–z onto the shared memory.
8. Wait till client reads the shared memory contents
9. Detach process from the shared memory using shmdt system call.
10. Remove shared memory from the system using shmctl with IPC_RMID argument
11. Stop
Client
1. Initialize size of shared memory shmsize to 27.
2. Initialize key to 2013 (same value as in server).
3. Obtain access to the same shared memory segment using same key.
a. If obtained then display the shmid else print "Server not started"
4. Attach client process to the shared memory using shmmat with shmid as parameter.
a. If pointer to the shared memory is not obtained, then stop.
5. Read contents of shared memory and print it.
6. After reading, modify the first character of shared memory to '*'
7. Stop
Coding
Server
#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define shmsize 27
main()
{
char c;
int shmid;
key_t key = 2013;
char *shm, *s;
if ((shmid = shmget(key, shmsize, IPC_CREAT|0666)) < 0)
{
perror("shmget");
exit(1);
}
printf("Shared memory id : %d\n", shmid);
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}
memset(shm, 0, shmsize);
s = shm;
printf("Writing (a-z) onto shared memory\n");
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = '\0';
while (*shm != '*');
printf("Client finished reading\n");
if(shmdt(shm) != 0)
fprintf(stderr, "Could not close memory segment.\n");
shmctl(shmid, IPC_RMID, 0);
}
Client
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define shmsize 27
main()
{
int shmid;
key_t key = 2013;
char *shm, *s;
if ((shmid = shmget(key, shmsize, 0666)) < 0)
{
printf("Server not started\n");
exit(1);
}
else
printf("Accessing shared memory id : %d\n",shmid);
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}
printf("Shared memory contents:\n");
for (s = shm; *s != '\0'; s++)
putchar(*s);
putchar('\n');
*shm = '*';
}
Output Output
[14CSE84@localhost Desktop]$ vi sm.c [14CSE84@localhost Desktop]$ vi smc.c
[14CSE84@localhost Desktop]$ cc sm.c [14CSE84@localhost Desktop]$ cc smc.c
[14CSE84@localhost Desktop]$ ./a.out [14CSE84@localhost Desktop]$ ./a.out
Shared memory id : 884753 Accessing shared memory id : 884753
Writing (a-z) onto shared memory Shared memory contents:
Client finished reading abcdefghijklmnopqrstuvwxyz
Result Thus contents written onto shared memory by the server process is read by the client
process.
Ex No:5 PRODUCER CONSUMER PROBLEM USING SEMAPHORE
Aim
To write the program for producer and consumer using semaphore
Algorithm
Step 1: Start the program.
Step 2: Declare all the preprocessor directives for the program.
Step 3: Define a structure containing buffer size,empty,full and mutex.
Step 4: Define two functions memory and init.
Step 5: Create a program named producer.c to produce the item.
Step 6: Create another program named consumer.c to consume the item.
Step 7: Stop the program.
Coding
Problem.h
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <fcntl.h>
#define BUFFER_SIZE 10
#define CONSUMER_SLEEP_SEC 3
#define PRODUCER_SLEEP_SEC 1
#define KEY 1010
Typedef struct
{
int buff[BUFFER_SIZE];
sem_t mutex, empty, full;
} MEM;
MEM *memory()
{
key_t key = KEY;
int shmid;
shmid = shmget(key, sizeof(MEM), IPC_CREAT | 0666);
return (MEM *) shmat(shmid, NULL, 0);
}
Void init()
{
MEM *M = memory();
sem_init(&M->mutex,1,1);
sem_init(&M->empty,1,BUFFER_SIZE);
sem_init(&M->full,1,0);
}
Producer.c
#include"problem.h"
void producer()
{
Int i=0,n;
MEM *S = memory();
while(1)
{
i++;
sem_wait(&S->empty);
sem_wait(&S->mutex);
sem_getvalue(&S->full,&n);
(S->buff)[n] = i;
printf("[PRODUCER] Placed item [%d]\n", i);
sem_post(&S->mutex);
sem_post(&S->full);
sleep(PRODUCER_SLEEP_SEC);
}
}
main()
{
init();
producer();
}
Consumer.c
#include"problem.h"
void consumer()
{
int n;
MEM *S = memory();
while(1)
{
sem_wait(&S->full);
sem_wait(&S->mutex);
sem_getvalue(&S->full,&n);
printf("[CONSUMER] Removed item [%d]\n",(S->buff)[n]);
sem_post(&S->mutex);
sem_post(&S->empty);
sleep(CONSUMER_SLEEP_SEC);
}
}
main()
{
consumer();
}
Output
Problem.h
[14cse84@localhost Desktop]$ vi sk.h
[14cse84@localhost Desktop]$ cc sk.h
Producer.c
[14cse84@localhost Desktop]$ vi pro.c
[14cse84@localhost Desktop]$ cc pro.c -o pro -lrt
[14cse84@localhost Desktop]$ ./pro
[PRODUCER]placed item [1]
[PRODUCER]placed item [2]
[PRODUCER]placed item [3]
[PRODUCER]placed item [4]
[PRODUCER]placed item [5]
[PRODUCER]placed item [6]
^Z
[1]+ Stopped ./pro
[14cse84@localhost Desktop]$
Consumer.c
[14cse84@localhost Desktop]$ vi con.c
[14cse84@localhost Desktop]$ cc con.c -o con -lrt
[14cse84@localhost Desktop]$ ./con
[CONSUMER]removed item[6]
[CONSUMER]removed item[5]
[CONSUMER]removed item[4]
[CONSUMER]removed item[3]
[CONSUMER]removed item[2]
[CONSUMER]removed item[1]
Result
Thus the program for producer and consumer using semaphore has been executed and
verified successfully.
Ex.No.:6 IMPLEMENTATION OF THREADS AND SYNCHRONIZATION
Aim
To write a program for the implementation of threads and synchronization.
Algorithm
Step 1: Start the program.
Step 2: Declare all the preprocessor directives for the program.
Step 3: Define the functions named as ” dosomething” to create and end the thread of jobs.
Step 4: Inside the main() function call the ” dosomething” function to create and terminate
the jobs.
Step 5: Stop the program.
Coding
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_ttid[2];
int counter;
pthread_mutex_t lock;
void* doSomeThing(void *arg)
{
pthread_mutex_lock(&lock);
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);
pthread_mutex_unlock(&lock);
return NULL;
}
int main(void)
{
Int i = 0;
int err;
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}
while(i< 2)
{
err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
if (err != 0)
printf("\ncan't create thread :[%s]", strerror(err));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);
return 0;
}
Output
[14cse84@localhost Desktop]$ vi thread.c
[14cse84@localhost Desktop]$ cc thread.c -o thread -lpthread
[14cse84@localhost Desktop]$ ./thread
Job 1 started
Job 1 finished
Job 2 started
Job 2 finished
Result
Thus the program for the implementation of thread for synchronization.
Ex.No: 7
IMPLEMENTATION OF DEADLOCK AVOIDANCE ALGORITHM
AIM:
To write a ‘C’ program by avoiding the deadlock using banker’s algorithm.
ALGORITHM:
• Get the allocated and maximum resources and process name from the user.
• Calculate the available matrix from the maximum and allocated resources.
• Then calculate the need matrix.
• Then apply the banker’s algorithm to allocate the requested resource from available
resources.
• Release the allocated resource from completed process.
• This step is repeated to find the safe state among the available process.
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}}}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}}}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
OUTPUT:
[14cse02@localhost Desktop]$ vi d.c
[14cse02@localhost Desktop]$ cc d.c
[14cse02@localhost Desktop]$ ./a.out
Enter the no of processes : 5
Enter the no of resources :3
Enter the Max Matrix for each process : Enter the allocation for each process :
For process 1 : 7 5 3 For process 1 : 0 1 0
For process 2 : 3 2 2 For process 2 : 2 0 0
For process 3 : 9 0 2 For process 3 : 3 0 2
For process 4 : 2 2 2 For process 4 : 2 1 1
For process 5 : 4 3 3 For process 5 : 0 0 2
RESULT: Thus the ‘C’ program to implement for avoiding the deadlock using banker’s
algorithm is executed successfully and the output is verified.
ExNo: 8 IMPLEMENTATION OF DEADLOCK DETECTION ALGORITHM
AIM:
To write a C Program to implement Deadlock Detection Algorithm.
ALGORITHM:
• Get the allocated and resources requested by the process and process name from the user.
• Calculate the available matrix from the request and allocated resources.
• Then apply the banker’s algorithm to allocate the requested resource from available
resources.
• If it is lesser than available resource, the process is granted.Otherwise the process is put
to wait.
• This step is repeated to find the safe state among the available processes.
PROGRAM:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
int req[10][10],alloc[10][10],avail[10],completed[10],safeSequence[10],p,r,i,j,process,count;
count = 0;
clrscr();
printf("Enter the no of processes");
scanf("%d",&p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("Enter the no of resources : ");
scanf("%d", &r);
printf("Enter the Request Matrix for each process \n ");
for(i = 0; i < p; i++)
{
printf("For process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &req[i][j]);
}
printf("Enter the allocation for each process \n ");
for(i = 0; i < p; i++)
{
printf("For process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("Enter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
printf("\n Process \t Max matrix \t Allocation matrix\n");
for(i = 0; i < p; i++)
{
printf("P%d\t",i+1);
for( j = 0; j < r; j++)
printf("%d \t", req[i][j]);
for(j=0;j<r;j++)
printf("%d \t", alloc[i][j]);
printf("\n");
}
do
{
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] <req[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
req[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUTPUT:
[14cse84@localhost Desktop]$ videad.c
[14cse84@localhost Desktop]$ cc dead.c
[14cse84@localhost Desktop]$ ./a.out
Enter the no of processes 5
Enter the no of resources : 3
Enter the Request Matrix for each process
For process 1 : 0 0 0
For process 2 : 2 0 2
For process 3 : 0 0 0
For process 4 : 1 0 0
For process 5 : 0 0 2
Enter the allocation for each process
For process 1 : 0 1 0
For process 2 : 2 0 0
For process 3 : 3 0 3
For process 4 : 2 1 1
For process 5 : 0 0 2
Enter the Available Resources : 0 0 0
Process Request matrix Allocation matrix
P1 0 0 0 0 1 0
P2 2 0 2 2 0 0
P3 0 0 0 3 0 3
P4 1 0 0 2 1 1
P5 0 0 2 0 0 2
Process 1 runs to completion!
Process 3 runs to completion!
Process 2 runs to completion!
Process 4 runs to completion!
Process 5 runs to completion!
The system is in a safe state!!
Safe Sequence :< 1 3 2 4 5 >
RESULT:
Thus the C Program that implements Deadlock Detection using resource allocation algorithm.
Ex.no.9a PAGE REPLACEMENT ALGORITHM (FIFO)
AIM:
To write a c program to implement FIFO page replacement algorithm
ALGORITHM:
1. Start the process
2. Declare the size with respect to page length
3. Check the need of replacement from the page to memory
4. Check the need of replacement from old page to new page in memory
5. Forma queue to hold all pages
6. Insert the page require memory into the queue
7. Check for bad replacement and page fault
8. Get the number of processes to be inserted
9. Display the values
10. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page 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]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
OUTPUT:
ENTER THE NUMBER OF PAGES: 20
ENTER THE PAGE NUMBER : 70120304230321201701
ENTER THE NUMBER OF FRAMES :3
ref string page frames 0 0 2 3
7 7 -1 -1 3
0 7 0 -1 2
1 7 0 1 1 0 1 3
2 2 0 1 2 0 1 2
0 0
3 2 3 1 1
0 2 3 0 7 7 1 2
4 4 3 0 0 7 0 2
2 4 2 0 1 7 0 1
3 4 2 3 Page Fault Is 15
RESULT:
Thus the implementation of FIFO page replacement algorithm has been executed and
output was verified successfully.
Ex.no.9b
PAGE REPLACEMENT ALGORITHM (LRU)
AIM:
To write a c program to implement LRU page replacement algorithm
ALGORITHM:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the least recently used page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}}}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}}}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7 4 3 7
7 5 9 3 7
7 5 9 9 6 7
4 5 9 9 6 2
4 3 9 1 6 2
AIM:
To write a c program to implement OPTIMAL page replacement algorithm
ALGORITHM:
1. Start the process
2. Declare the size
3. Get the number of pages to be inserted
4. Get the value
5. Declare counter and stack
6. Select the Least future use page by counter value
7. Stack them according the selection.
8. Display the values
9. Stop the process
PROGRAM:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=0;j<n;j++)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}}}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}}}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
7 4 9
7 3 9
7 3 9
7 3 9
6 3 9
6 2 9
6 2 1
The no of page faults is 7
RESULT:
Thus the optimal page replacement algorithm has been implemented and outputs were
verified successfully.
Exno:10a
frameno=pagetable[pgno];
if(frameno==-1||flag==1)
printf("\nThe required page is not valid");
else
{
pa=frameno*s+ofs;
printf("\n Equivalent physical address : %d",pa);
}
getch();
}
Output:
[14cse75@localhost Desktop]$ cc paging.c -o paging -lm
[14cse75@localhost Desktop]$ ./paging
Enter process size (in KB)15
Enter the page size(in KB)4
Total No. of pages: 4
Enter Logical Address
31
page no=3
Page offset=1
Enter page table Information
2
3
4
5
Page Table
---------------
Page No Frame No
----------------
0 2
1 3
2 4
3 5
4 -1
5 -1
6 -1
7 -1
8 -1
9 -1
RESULT: Thus the paging concept algorithm has been implemented and outputs were verified
successfully.
CONTIGUOUS FILE ALLOCATION
Ex no.10b
Aim
To write a C Program the implement the contiguous memory allocation techniques.
Algorithm
1Get the number of files from the user to be allocated along with its capacity and starting
location of the each file.
2Create a disk with 100 blocks.
3Compare the available block address with starting address along with its capacity.
4If block is contiguous then allocated it
5Otherwise report the error message the file is not allocated.
Program
#include<stdio.h>
main()
{
int nf, fc[20], mb[100], i, j, k, fb[100], fs[20], mc=0;
clrscr();
printf("\nEnter the number of files: ");
scanf("%d",&nf);
for(i=0;i<nf;i++)
{
printf("\nEnter the capacity of file %d: ",i+1);
scanf("%d",&fc[i]);
printf("\nEnter the starting address of file %d: ",i+1);
scanf("%d",&fs[i]);
}
printf("\n---CONTIGUOUS MEMORY ALLOCATION---\n");
for(i=0;i<100;i++)
fb[i]=1;
for(i=0;i<nf;i++)
{
j=fs[i];
{
if(fb[j]==1)
{
for(k=j;k<(j+fc[i]);k++)
{
if(fb[k]==1)
mc++;
}
if(mc==fc[i])
{
for(k=fs[i];k<(fs[i]+fc[i]);k++)
{
fb[k]=0;
}
printf("\nFile %d allocated in memory %d to %d...",i+1,fs[i],fs[i]+fc[i]-1);
}
}
else
printf("\nFile %d not allocated since %d contiguous memory not available from
%d...",i+1,fc[i],fs[i]);
}
mc=0;
}}
Output
Enter the number of files: 3
Enter the capacity of file 1: 3
Enter the starting address of file 1: 1
Enter the capacity of file 2: 4
Enter the starting address of file 2: 5
Enter the capacity of file 3: 3
Enter the starting address of file 3: 2
---CONTIGUOUS MEMORY ALLOCATION---
File 1 allocated in memory 1 to 3...
File 2 allocated in memory 5 to 8...
File 3 not allocated since 3 contiguous memory not available from 2
Result
Thus the program that implements the contiguous file allocation techniques was written,
compiled ,executed and outputs were noted.
Algorithm
1. Declare structures hole and process to hold information about set of holes and processes
respectively.
2. Get number of holes, say nh.
3. Get the size of each hole
4. Get number of processes, say np.
5. Get the memory requirements for each process.
6. Allocate processes to holes, by examining each hole as follows:
a. Sort the holes according to their sizes in ascending order
b. If hole size > process size then
i. Mark process as allocated to that hole.
ii. Decrement hole size by process size.
c. Otherwise check the next from the set of sorted hole
7. Print the list of process and their allocated holes or unallocated status.
8. Print the list of holes, their actual and current availability.
9. Stop
Program
#include <stdio.h>
struct process
{
int size;
int flag;
int holeid;
} p[10];
struct hole
{
int hid;
int size;
int actual;
} h[10];
main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
h[i].hid = i;
}
printf("\nEnter number of process : " );
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}
for(i=0; i<np; i++)
{
bsort(h, nh);
or(j=0; j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1;
p[i].holeid = h[j].hid;
h[j].size -= p[i].size;
}}}}
printf("\n\tBest fit\n");
printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual,
h[i].size);
printf("\n");
}
void bsort(struct hole bh[], int n)
{
struct hole temp;
int i,j;
for(i=0; i<n-1; i++){
for(j=i+1; j<n; j++){
if(bh[i].size > bh[j].size){
temp = bh[i];
bh[i] = bh[j];
bh[j] = temp;
}}}}
Output
[exam01@Vcetlinux ~]$ cc prg25.c Best fit
[exam01@Vcetlinux ~]$ ./a.out Process PSize Hole
Enter the number of Holes : 5 P0 212 H3
Enter size for hole H0 : 100 P1 417 H1
Enter size for hole H1 : 500 P2 112 H2
Enter size for hole H2 : 200 P3 426 H4
Enter size for hole H3 : 300 Hole Actual Available
Enter size for hole H4 : 600 H1 500 83
Enter number of process : 4 H3 300 88
enter the size of process P0 : 212 H2 200 88
enter the size of process P1 : 417 H0 100 100
enter the size of process P2 : 112 H4 600 17
enter the size of process P3 : 426
Result: Thus processes were allocated memory using best fit method.
WORST FIT
Aim
To write a ‘C’ program in UNIX to implement Dynamic Storage Allocation Strategy for
Worst Fit.
Algorithm
1. Start
2. Read the number of free blocks and the size of each free block.
3. Get the process block size to be loaded.
4. Allocate the largest hole that is big enough to load the process
5. If no hole is big enough to load the process, print process cannot be allocated.
6. Display the size of all the free blocks.
7. Stop.
Program
#include <stdio.h>
struct process
{
int size;
int flag;
int holeid;
} p[10];
struct hole
{
int hid;
int size;
int actual;
} h[10];
main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
h[i].hid = i;
}
printf("\nEnter number of process : " );
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);
scanf("%d", &p[i].size);
p[i].flag = 0;
}
for(i=0; i<np; i++)
bsort(h, nh);
for(j=0; j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1;
p[i].holeid = h[j].hid;
h[j].size -= p[i].size;
}}}}
printf("\n\tworst fit\n");
printf("\nProcess\tPSize\tHole");
for(i=0; i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);
else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual,
h[i].size);
printf("\n");
}
void bsort(struct hole bh[], int n)
{
struct hole temp;
int i,j;
for(i=0; i<n-1; i++)
{
{
for(j=i+1; j<n; j++)
{
if(bh[i].size < bh[j].size)
{
temp = bh[i];
bh[i] = bh[j];
bh[j] = temp;
}}}}
Output
Enter the number of Holes : 5 Process PSize Hole
Enter size for hole H0 : 100 P0 212 H4
Enter size for hole H1 : 500 P1 417 H1
Enter size for hole H2 : 200 P2 112 H4
Enter size for hole H3 : 300 P3 426 Not allocated
Enter size for hole H4 : 600 Hole Actual Available
Enter number of process : 4 H3 300 300
enter the size of process P0 : 212 H4 600 276
enter the size of process P1 : 417 H2 200 200
enter the size of process P2 : 112 H0 100 100
enter the size of process P3 : 426 H1 500 83
worst fit
Result
Thus processes were allocated memory using worst fit method.
Exno:11a
SINGLE LEVEL DIRECTORY ORGANIZATION
Aim
To Write a C program to simulate the following file organization techniques Single level
directory
Algorithm:
Step1:start
Step 2: create a structure to hold different values.
Step3: then enter directory name.
Step4:give the condition on each cases of switch state.
Step 5: check the status by producing output.
Step6: stop
Program:
#include<stdio.h>
struct
{ char dname[10],fname[10][10];
int fcnt; }dir;
void main()
{
int i,ch;
char f[30];
clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{ printf("\n\n1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your
choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f); for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\nDirectory Empty");
else
{
printf("\nThe Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}
OUTPUT:
Enter name of directory -- CSE
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit
Enter your choice – 1
Enter the name of the file -- A
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- B
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- C
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit
Enter your choice – 4 The Files are -- A B C
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit
Enter your choice – 3
Enter the name of the file – ABC
File ABC not found
1. Create File 2. Delete File 3. Search File 4. Display Files 5.
Exit Enter your choice – 2
Enter the name of the file – B
File B is deleted
1. Create File 2. Delete File 3. Search File 4. Display Files 5. Exit Enter your choice – 5
Result
Thus the program that implements the Single level directory techniques was written,
compiled ,executed and outputs were noted.
Exno:11b)
To Write a C program to simulate the following file organization techniques Two level
directory.
Algorithm:
Step1:start
Step 2: create a structure to hold different values.
Step3: then enter directory name.
Step4:give the condition on each cases of switch state.
Step 5: check the status by producing output.
Step6: stop
Program
#include<stdio.h>
Struct
{ char dname[10],fname[10][10]; int fcnt;
}dir[10];
void main()
{ int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File"); printf("\n4. Search File\t\t5.
Display\t6. Exit\t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else {
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
getch();
}
OUTPUT:
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 1
Enter name of directory -- DIR1
Directory created
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 1
Enter name of directory -- DIR2
Directory created
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2 Enter name of the directory – DIR1
Enter name of the file -- A1
File created
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 2
Enter name of the directory – DIR1
Enter name of the file -- A2
File created
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice – 2
Enter name of the directory – DIR2
Enter name of the file -- B1
File created
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 5 Directory Files DIR1 A1 A2 DIR2 B1
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice -- 4
Enter name of the directory – DIR
Directory not found
1.Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice – 3
Enter name of the directory – DIR1
Enter name of the file -- A2
File A2 is deleted
1. Create Directory 2. Create File 3. Delete File 4. Search File 5. Display 6. Exit
Enter your choice – 6
Result
Thus the program that implements the Two level directory techniques was written,
compiled ,executed and outputs were noted.
INDEXED FILE ALLOCATION
Exno:12b
Aim
To write a C Program the implement the indexed memory allocation techniques.
Algorithm
1.Get the number of files from the user to be allocated along with its capacity of each file.
2.Enter no of block needed for the file
3.Enter the name of the file and display the file allocation table.
Program
#include<stdio.h>
main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter starting block and size of file%d:",i+1);
scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{ printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}
for(i=0;i<n;i++)
{
printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%3d",b[i][j]);
}
}
Output
[14cse75@localhost Desktop]$ cc index.c
[14cse75@localhost Desktop]$ ./a.out
Enter no. of files:2
Enter starting block and size of file1:2 5
Enter blocks occupied by file1:10
enter blocks of file1:3
254672647
Enter starting block and size of file2:3 4
Enter blocks occupied by file2:5
enter blocks of file2:2 3 4 5 6
File index length
1 2 10
2 3 5
Enter file name:1
file name is:1
Index is:2Block occupied are: 3 2 5 4 6 7 2 6 4 7
Enter file name:2
file name is:2
Index is:3Block occupied are: 2 3 4 5 6[14cse75@localhost Desktop]$
Result
Thus the program that implements the indexed file allocation techniques was written,
compiled ,executed and outputs were noted.
Algorithm
Output
Enter no. of files:2
Enter file name:nandy
Enter starting block:20
Enter no.of blocks:6
Enter block numbers:4
12
15
45
32
25
Enter file name:vnan
Enter starting block:12
Enter no.of blocks:5
Enter block numbers:6
5
4
3
2
File start size block
nandy 20 6 4--->12--->15--->45--->32--->25
vnan 12 5 6--->5--->4--->3--->2
Result
Thus the program that implements the Linked file allocation techniques was written,
compiled ,executed and outputs were noted.
Slno Name of the Experiment
1 Basic Linux Commands
2 Shell programming
Process Scheduling
3a FCFS
3b SJF
3c Priority Based Scheduling
3d Round Robin
Inter process Communication
4a Pipes
4b Message Queue
4c Shared Memory
Process Synchronization
5 Producer consumer Problem using Semaphore
6 Threading &Synchronization Applications
Deadlock Management Techniques
7 Deadlock avoidance
8 Deadlock Detection
Page Replacement Algorithm
9a FIFO
9b LRU
9c Optimal
Memory Management
10a Paging
10b Contiguous Allocation
File organization Techniques
11a Single level directory
11b Two level directory
File Allocation Techniques
12a Sequential
12b Indexed
12c Linked