0% found this document useful (0 votes)
25 views44 pages

Os and CD All Lab Exp

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)
25 views44 pages

Os and CD All Lab Exp

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/ 44

CD 1

//Implementation of Lexical Analyzer using Lex tool

%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
#.* {printf("\n%s is a preprocessor directive",yytext);}
int |
float |
char |
double |
while |
for |
struct |
typedef |
do |
if |
break |
continue |
void |
switch |
return |
else |
goto {printf("\n\t%s is a keyword",yytext);}
"/*" {COMMENT=1;}{printf("\n\t %s is a COMMENT",yytext);}
{identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);}
\{ {if(!COMMENT)printf("\n BLOCK BEGINS");}
\} {if(!COMMENT)printf("BLOCK ENDS ");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER ",yytext);}
\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%

int main(int argc, char **argv)


{
FILE *file;
file=fopen("var.c","r");
if(!file)
CD 1

{
printf("could not open the file");
exit(0);
}
yyin=file;
yylex();
printf("\n");
return(0);
}
int yywrap()
{
return(1);
}
INPUT:
//var.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
}

Output:
EX-2

Write a C Program to simulate Lexical Analyzer to validate a given input string


%{
int len=0;
%}

//Rules to identify if a character apart from alphabets


//occurs in a string

%%
[a-zA-Z]+ {printf("No character other than alphabets");}

/* here . will match any other character than alphabets


because alphabets are already matched above
* will matches 0 or more characters in front of it.
*/

.* {printf("character other than alphabets present"); }


%%

// code section
int yywrap() { }

int main()
{
yylex();
return 0;
}
Ex-3

Write a C Program to implement Brute force technique of Top down parsing.

/* Program to Implement Recursive Descent Parsing */

#include<stdio.h>

//#include<conio.h>

#include<string.h>

char input[100];

int i,l;

int main()

printf("recursive decent parsing for the grammar");

printf("\n E->TEP|\nEP->+TEP|@|\nT->FTP|\nTP->*FTP|@|\nF->(E)|ID\n");

printf("enter the string to check:");

scanf("%s",input);

if(E()){

if(input[i]=='$')

printf("\n string is accepted\n");

else

printf("\n string is not accepted\n");

E(){

if(T()){

if(EP())

return(1);

else
return(0);

else

return(0);

EP(){

if(input[i]=='+'){

i++;

if(T()){

if(EP())

return(1);

else

return(0);

else

return(1); }}

T(){

if(F()){

if(TP())

return(1);

else

return(0); }

else

return(0);

printf("String is not accpeted\n");

TP(){
if(input[i]=='*'){

i++;

if(F()){

if(TP())

return(1)

else

return(0); }

else

return(0);

printf("The string is not accepted \n");

else

return(1); }

F(){

if(input[i]=='('){

i++;

if(E()){

if(input[i]==')'){

i++;

return(1); }

else

return(0);

printf("String is not accepted\n");

}
else

return(0);

else

if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z')

i++;

return(1);

else

return(0);

}
OUTPUT:

$ cc rdp.c

$ ./a.out

recursive decent parsing for the grammar

E->TEP|

EP->+TEP|@|

T->FTP|

TP->*FTP|@|

F->(E)|ID

enter the string to check:(i+i)*i

string is accepted
EX-4

Write a C Program to compute the First and Follow sets for the given Grammar.

// C program to calculate the First and

// Follow sets of a given grammar

#include<stdio.h>

#include<ctype.h>

#include<string.h>

// Functions to calculate Follow

void followfirst(char, int, int);

void follow(char c);

// Function to calculate First

void findfirst(char, int, int);

int count, n = 0;

// Stores the final result

// of the First Sets

char calc_first[10][100];

// Stores the final result

// of the Follow Sets

char calc_follow[10][100];

int m = 0;
// Stores the production rules

char production[10][10];

char f[10], first[10];

int k;

char ck;

int e;

int main(int argc, char **argv)

int jm = 0;

int km = 0;

int i, choice;

char c, ch;

count = 8;

// The Input grammar

strcpy(production[0], "E=TR");

strcpy(production[1], "R=+TR");

strcpy(production[2], "R=#");

strcpy(production[3], "T=FY");

strcpy(production[4], "Y=*FY");

strcpy(production[5], "Y=#");
strcpy(production[6], "F=(E)");

strcpy(production[7], "F=i");

int kay;

char done[count];

int ptr = -1;

// Initializing the calc_first array

for(k = 0; k < count; k++) {

for(kay = 0; kay < 100; kay++) {

calc_first[k][kay] = '!';

int point1 = 0, point2, xxx;

for(k = 0; k < count; k++)

c = production[k][0];

point2 = 0;

xxx = 0;

// Checking if First of c has

// already been calculated

for(kay = 0; kay <= ptr; kay++)


if(c == done[kay])

xxx = 1;

if (xxx == 1)

continue;

// Function call

findfirst(c, 0, 0);

ptr += 1;

// Adding c to the calculated list

done[ptr] = c;

printf("\n First(%c) = { ", c);

calc_first[point1][point2++] = c;

// Printing the First Sets of the grammar

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

int lark = 0, chk = 0;

for(lark = 0; lark < point2; lark++) {

if (first[i] == calc_first[point1][lark])

chk = 1;

break;
}

if(chk == 0)

printf("%c, ", first[i]);

calc_first[point1][point2++] = first[i];

printf("}\n");

jm = n;

point1++;

printf("\n");

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

char donee[count];

ptr = -1;

// Initializing the calc_follow array

for(k = 0; k < count; k++) {

for(kay = 0; kay < 100; kay++) {


calc_follow[k][kay] = '!';

point1 = 0;

int land = 0;

for(e = 0; e < count; e++)

ck = production[e][0];

point2 = 0;

xxx = 0;

// Checking if Follow of ck

// has already been calculated

for(kay = 0; kay <= ptr; kay++)

if(ck == donee[kay])

xxx = 1;

if (xxx == 1)

continue;

land += 1;

// Function call
follow(ck);

ptr += 1;

// Adding ck to the calculated list

donee[ptr] = ck;

printf(" Follow(%c) = { ", ck);

calc_follow[point1][point2++] = ck;

// Printing the Follow Sets of the grammar

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

int lark = 0, chk = 0;

for(lark = 0; lark < point2; lark++)

if (f[i] == calc_follow[point1][lark])

chk = 1;

break;

if(chk == 0)

printf("%c, ", f[i]);


calc_follow[point1][point2++] = f[i];

printf(" }\n\n");

km = m;

point1++;

void follow(char c)

int i, j;

// Adding "$" to the follow

// set of the start symbol

if(production[0][0] == c) {

f[m++] = '$';

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

for(j = 2;j < 10; j++)

{
if(production[i][j] == c)

if(production[i][j+1] != '\0')

// Calculate the first of the next

// Non-Terminal in the production

followfirst(production[i][j+1], i, (j+2));

if(production[i][j+1]=='\0' && c!=production[i][0])

// Calculate the follow of the Non-Terminal

// in the L.H.S. of the production

follow(production[i][0]);

void findfirst(char c, int q1, int q2)

{
int j;

// The case where we

// encounter a Terminal

if(!(isupper(c))) {

first[n++] = c;

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

if(production[j][0] == c)

if(production[j][2] == '#')

if(production[q1][q2] == '\0')

first[n++] = '#';

else if(production[q1][q2] != '\0'

&& (q1 != 0 || q2 != 0))

// Recursion to calculate First of New

// Non-Terminal we encounter after epsilon

findfirst(production[q1][q2], q1, (q2+1));


}

else

first[n++] = '#';

else if(!isupper(production[j][2]))

first[n++] = production[j][2];

else

// Recursion to calculate First of

// New Non-Terminal we encounter

// at the beginning

findfirst(production[j][2], j, 3);

void followfirst(char c, int c1, int c2)

{
int k;

// The case where we encounter

// a Terminal

if(!(isupper(c)))

f[m++] = c;

else

int i = 0, j = 1;

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

if(calc_first[i][0] == c)

break;

//Including the First set of the

// Non-Terminal in the Follow of

// the original query

while(calc_first[i][j] != '!')

if(calc_first[i][j] != '#')

{
f[m++] = calc_first[i][j];

else

if(production[c1][c2] == '\0')

// Case where we reach the

// end of a production

follow(production[c1][0]);

else

// Recursion to the next symbol

// in case we encounter a "#"

followfirst(production[c1][c2], c1, c2+1);

j++;

}
}

Output :

First(E)= { (, i, }
First(R)= { +, #, }
First(T)= { (, i, }
First(Y)= { *, #, }
First(F)= { (, i, }

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

Follow(E) = { $, ), }
Follow(R) = { $, ), }
Follow(T) = { +, $, ), }
Follow(Y) = { +, $, ), }
Follow(F) = { *, +, $, ), }
Ex-5

Write a C Program to check the validity of input string using predictive parser.

/* Program to Implement Predictive Parsing */

#include<stdio.h>

#include<string.h>

int spt=0,ipt=0;

char s[20],ip[15];

char
*m[5][6]={{"TG","\0","\0","TG","\0","\0"},{"\0","+TG","\0","\0","e","e"},{"FH","\0","\0","
FH","\0","\0"},{"\0","e","FH","\0","e","e"},{"i","\0","\0","(E)","\0","\0"}};

char nt[5]={'E','G','T','H','F'};

char t[6]={'i','+','*','(',')','$'};

int nti(char c) {

int i;

for(i=0;i<5;i++){

if(nt[i]==c)

return(i); }

return(6); }

int ti(char c) {

int i;

for(i=0;i<6;i++) {

if(t[i]==c)

return(i); }

return(7); }

int main()

{
char prod[4],temp[4];

int l,k,j;

printf(

"enter input string:");

scanf("%s",ip);

strcat(ip,"$");

s[0]='$';

s[1]='E';

s[2]='\0';

spt=1;

while(1) {

if(ip[ipt]=='$'&&s[spt]=='$')

break;

if(ti(s[spt])<5||s[spt]=='$') {

if(s[spt]==ip[ipt]) {

spt--

ipt++; }

else

error();

else if(nti(s[spt]<6))

py(prod,m[nti(s[spt])][ti(ip[ipt])]);

if(prod=='\0')
perror();

l=strlen(prod);

for(k=l

-1,j=0;k>=0&&j<=l;k--,j++)

temp[j]=prod[k];

for(k=0;k<l;k++)

prod[k]=temp[k];

s[spt--]='\0';

strcat(s,prod);

spt=spt+l;

if(s[spt]=='e')

s[spt--]='\0';

else

error(); }

if(s[spt]=='$'&&ip[ipt]=='$')

printf("\n input is parsed \n");

else

error();

return 0; }

error()

printf("input is not parsed\n");

exit(1);

return 0;

$ cc preparsing.c
$ ./a.out

enter input string:i+i*i$

input is parsed
OS 1(A)

#include <stdio.h>

int main() {

int count, i, n, time, remain, flag = 0, time_quantum;

int wait_time = 0, turnaround_time = 0, at[10], bt[10], rt[10];

printf("Enter Total Processes: ");

scanf("%d", &n);

remain = n;

for (count = 0; count < n; count++) {

printf("Enter Arrival Time & Burst Time for process number %d: ", count + 1);

scanf("%d", &at[count]);

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

rt[count] = bt[count]; // Remaining time initialized to burst time

printf("Enter Time Quantum: ");

scanf("%d", &time_quantum);

printf("\nProcess\tTurnaround Time\tWaiting Time\n");

for (time = 0; remain != 0; ) {

for (count = 0; count < n; count++) {

if (rt[count] > 0) { // Check if process has remaining time

if (rt[count] <= time_quantum) {

time += rt[count]; // Add remaining time to total time

rt[count] = 0; // Process finished

flag = 1;

remain--;

printf("P[%d]\t%d\t\t%d\n", count + 1, time - at[count], time - at[count] - bt[count]);

wait_time += time - at[count] - bt[count];

} else {

rt[count] -= time_quantum; // Subtract time quantum

time += time_quantum; // Increase total time

}
OS 1(A)

if (flag == 0) {

time++;

printf("\nAverage Waiting Time = %.2f", (float)wait_time / n);

printf("\nAverage Turnaround Time = %.2f", (float)(wait_time + (turnaround_time / n)) / n);

return 0;

OS 1 (B)

#include <stdio.h>

int main() {

int bt[20], P[20], wt[20], tat[20], n, total = 0, pos;

float avg_wt, avg_tat;

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

scanf("%d", &n);

printf("Enter Burst Time:\n");

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

printf("Process %d: ", i + 1);

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

P[i] = i + 1; // Process number


OS 1(B)

} for (int i = 0; i < n; i++) {

pos = i;

for (int j = i + 1; j < n; j++) {

if (bt[j] < bt[pos]) {

pos = j;

int temp = bt[i];

bt[i] = bt[pos];

bt[pos] = temp;

temp = P[i];

P[i] = P[pos];

P[pos] = temp;

wt[0] = 0;

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

wt[i] = 0;

for (int j = 0; j < i; j++) {

wt[i] += bt[j];

total += wt[i];

avg_wt = (float)total / n;

total = 0;

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

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

total += tat[i];

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

}
OS 1(B)

avg_tat = (float)total / n;

printf("\nAverage Waiting Time: %.2f", avg_wt);

printf("\nAverage Turnaround Time: %.2f\n", avg_tat);

return 0;

OS 1(C)

#include <stdio.h>

int main() {

int bt[20], p[20], wt[20], tat[20], pr[20], n, total = 0, pos, temp;

float avg_wt, avg_tat;

printf("Enter Total number of Processes: ");

scanf("%d", &n);

printf("Enter Burst Time and Priority:\n");

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

printf("Process P[%d]:\n", i + 1);

printf("Burst Time: ");

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

printf("Priority: ");

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

p[i] = i + 1; // Process number

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


OS 1(C)

pos = i;

for (int j = i + 1; j < n; j++) {

if (pr[j] < pr[pos]) { // Lower number means higher priority

pos = j;

temp = pr[i];

pr[i] = pr[pos];

pr[pos] = temp;

temp = bt[i];

bt[i] = bt[pos];

bt[pos] = temp;

temp = p[i];

p[i] = p[pos];

p[pos] = temp;

wt[0] = 0;

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

wt[i] = 0;

for (int j = 0; j < i; j++) {

wt[i] += bt[j];

total += wt[i];

avg_wt = (float)total / n;

total = 0;

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

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


OS 1(C)

total += tat[i];

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

avg_tat = (float)total / n;

printf("\nAverage Waiting Time: %.2f", avg_wt);

printf("\nAverage Turnaround Time: %.2f\n", avg_tat);

return 0;

}
OS 2-FORK()

#include <stdio.h>

#include <unistd.h>

void fork_example() {

if (fork() == 0) {

printf("Hello from Child!\n");

} else {

printf("Hello from Parent!\n");

int main() {

fork_example();

return 0;

OS 2 WAIT()

#include <stdio.h>

#include <stdlib.h>

#include <sys/wait.h>

#include <unistd.h>

int main() {

pid_t cpid;

if (fork() == 0) {

exit(0);

} else {

cpid = wait(NULL); // Wait for child process to terminate

printf("Parent PID = %d\n", getpid());

printf("Child PID = %d\n", cpid);

return 0;

}
OS 2-EXEC()

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

int main() {

pid_t pid = fork();

if (pid < 0) {

printf("Can't fork, error occurred\n");

exit(EXIT_FAILURE);

} else if (pid == 0) {

// Child process

printf("Child Process, PID = %d\n", getpid());

char *args[] = {"ls", NULL}; // Example command

execvp(args[0], args); // Execute ls command

perror("exec failed");

exit(EXIT_FAILURE);

} else {

printf("Parent Process, PID = %d\n", getpid());

int status;

if (waitpid(pid, &status, 0) > 0) {

if (WIFEXITED(status)) {

printf("Program execution successful with exit status %d\n", WEXITSTATUS(status));

} else {

printf("Program didn't terminate normally\n");

return 0;

}
OS 2-EXIT()

#include <stdio.h>

#include <stdlib.h>

int main(void)

Printf(“Start”);

Exit(0);

Printf(“End of program”);

OS 3(A)

#include <stdio.h>

#include <conio.h>

int main() {

int ms, bs, nob, i, mp[10], total_internal_fragmentation = 0, total_external_fragmentation = 0;

int allocated_processes = 0

clrscr();

printf("Enter the total memory available (in Bytes): ");

scanf("%d", &ms);

printf("Enter the block size (in Bytes): ");

scanf("%d", &bs);

nob = ms / bs; // Number of blocks available

total_external_fragmentation = ms % (nob * bs); // Remaining memory after allocating blocks

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

scanf("%d", &nob);

for (i = 0; i < nob; i++) {

printf("Enter memory required for Process %d (in Bytes): ", i + 1);

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

printf("\nNo. of Blocks available in memory --> %d\n", nob);

printf("PROCESS IF MEMORY REQUIRED IS ALLOCATED\n");


OS 3(A)

if (mp[i] > bs) {

printf("Memory Required exceeds Block Size\n");

} else {

total_internal_fragmentation += (bs - mp[i]); // Calculate internal fragmentation

allocated_processes++;

printf("Process %d allocated: Yes (Internal Fragmentation: %d Bytes)\n", i + 1, bs - mp[i]);

if (allocated_processes < nob) {

printf("Memory is full, Remaining Processes can't be allocated\n");

printf("Total Internal Fragmentation is: %d Bytes\n", total_internal_fragmentation);

printf("Total External Fragmentation is: %d Bytes\n", total_external_fragmentation);

getch();

return 0;

OS 3 (B)

#include <stdio.h>

#include <conio.h>
OS 3 (B)

int main() {

int ms, mp[10], temp, i;

char ch = 'y';

clrscr();

printf("Enter the total memory available (in Bytes): ");

scanf("%d", &ms);

temp = ms; // Store total memory available

for (i = 0; ch == 'y' || ch == 'Y'; i++) {

printf("Enter memory required for Process %d (in Bytes): ", i + 1);

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

if (mp[i] <= temp) {

temp -= mp[i]; // Decrease available memory

printf("Process %d allocated\n", i + 1);

} else {

printf("Memory is full\n");

break; // Exit loop if memory is full

printf("Do you want to continue? (y/n): ");

scanf(" %c", &ch); // Space before %c to consume newline

printf("\nTotal memory Available: %d Bytes\n", ms - temp);

printf("Processes and their allocated memory:\n");

for (int j = 0; j < i; j++) {

printf("Process %d: %d Bytes\n", j + 1, mp[j]);

printf("Total Extended Fragmentation: %d Bytes\n", temp);

getch();

return 0;

}
OS 4

#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 number of processes: ");
scanf("%d", &p);

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


completed[i] = 0;

printf("\n\nEnter the number 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; // Break if we found a process that can
run
}

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

return 0;
}
OS 5

#include <stdio.h>

void main() {
int max[10][10], a1[10][10], av[10], i, j, k, m, n, ne[10][10], finish[10],
flag = 0, safe = 0;
int work[10];

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


scanf("%d%d", &m, &n);

printf("\nEnter the maximum matrix:\n");


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &max[i][j]);
}
}

printf("\nEnter allocated matrix:\n");


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a1[i][j]);
}
}

printf("\nThe need matrix:\n");


for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
ne[i][j] = max[i][j] - a1[i][j];
printf("\t%d", ne[i][j]);
}
printf("\n");
}

printf("\nEnter available matrix:\n");


for (i = 0; i < n; i++) {
scanf("%d", &av[i]);
}

printf("\nMaximum matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\t%d", max[i][j]);
}
printf("\n");
}

printf("\nAllocated matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("\t%d", a1[i][j]);
}
printf("\n");
}

printf("\nAvailable matrix:\n");
for (i = 0; i < n; i++) {
printf("%d\t", av[i]);
}
printf("\n");

// Initialize work and finish arrays


for (i = 0; i < n; i++) {
work[i] = av[i];
}
for (i = 0; i < m; i++) {
finish[i] = 0; // Initialize finish array
}

// Check for safe state


while (safe < m) {
flag = 0; // Reset flag for each pass
for (i = 0; i < m; i++) {
if (finish[i] == 0) { // If process is not finished
int canAllocate = 1; // Assume we can allocate
for (j = 0; j < n; j++) {
if (ne[i][j] > work[j]) {
canAllocate = 0; // Can't allocate resources
break;
}
}
if (canAllocate) {
for (j = 0; j < n; j++) {
work[j] += a1[i][j]; // Simulate allocation
}
finish[i] = 1; // Mark process as finished
flag = 1; // We were able to allocate
safe++; // Increase safe process count
}
}
}
if (flag == 0) {
printf("\nUnsafe state\n");
return; // Exit if we can't find a safe process
}
}

printf("\nSafe state\n");
}
OS 6
Simulate the following page replacement algorithms.
a) First In First Out
#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;
}

b) Least Recently Used


#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);
}
OS 7
Simulate the following File allocation strategies
a) Sequenced
#include < stdio.h>
#include<conio.h>
void main()
{
int f[50],i,st,len,j,c,k,count = 0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf("The file is allocated to disk\n");
}
else
printf("The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}

b) Indexed
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk
: \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}

You might also like