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

Round Robin and Scheduling Algorithms

Uploaded by

gopi960067
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 views116 pages

Round Robin and Scheduling Algorithms

Uploaded by

gopi960067
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

1(a) ROUND ROBIN SCHEDULING

AIM:
To write a program for Round Robin scheduling

ALGORITHM:
Step-1:Start the program

Step-2:Declare the variable

Step-3:Get the date from the user

Step-4:Schedule the process based on the timeline

Step-5:Print the round robin scheduling

Step-6:Stop the program

1
CODING:
#include<stdio.h>

Void main( )

{
Int i,n, pid[15],bst[15],wait=0,tarnd,ts;
printf("\n Enter the no of process:");
scanf("%d",&n);
printf("\nEnter the values:");

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

{
printf("\n Enter the process id:");

scanf("%d",&pid[i]);
printf("\nEnter the burst time:");

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

}
printf("\nEnter the value for time slice:");

scanf("%d",&ts);

printf("\n pid\tburst\twait\ttarnd"); for(i=1;i<=n;i++)


{

if(bst[i]>ts)
{

n=n+1;

bst[n]=bst[i]-ts;

bst[i]=bst[i]-bst[n];

pid[n]=pid[i];
}
tarnd=wait+bst[i];

2
printf("\n%d\t%d\t%d\t%d\t",pid[i],bst[i],wait,tarnd);
wait=tarnd;

3
OUTPUT:
Enter the no ofprocess:3

Enter the values:

Enter the process id:1

Enter the burst time:6

Enter the process id:2

Enter the burst time:5

Enter the process id:3

Enter the burst time:4

Enter the value for times lice:4


pid burst wait tarnd
1 4 0 4
2 4 4 8

3 4 8 12
1 2 12 14

4
RESULT:

Thus the Program was Execute and Output Verified Successfully.

5
1(b) SJFS CHEDULING

AIM:
To write a program for Shortest job first algorithm

ALGORITHM:

Step 1: Start the program


Step 2: Declare the variable
Step 3: Get the required data using a for loop
Step 4: Check the shortest job in the list
Step 5: Print the shortest job
Step 6: Stop the program

6
CODING:

#include <stdio.h>

void main() {

int i, j, n, pid[20], bst[20], wait = 0, tarnd, t;

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

scanf("%d", &n);

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

printf("Enter the process ID: ");

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

printf("Enter the burst time: ");

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

// Sorting by burst time using SJF (Shortest Job First)

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

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

if (bst[i] > bst[j]) {

// Swap burst time

t = bst[i];

bst[i] = bst[j];

bst[j] = t;

7
// Swap process ID to keep track

t = pid[i];

pid[i] = pid[j];

pid[j] = t;

printf("\nPID\tBurst\tWait\tTurnaround");

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

tarnd = wait + bst[i];

printf("\n%d\t%d\t%d\t%d", pid[i], bst[i], wait, tarnd);

wait = tarnd;

8
OUTPUT:

Enter the number of process: 5

Enter the process ID: 1

Enter the burst time: 9

Enter the process ID: 2

Enter the burst time: 7

Enter the process ID: 3

Enter the burst time: 5

Enter the process ID: 4

Enter the burst time: 3

Enter the process ID: 5

Enter the burst time: 1

PID Burst Wait Turnaround

5 1 0 1

4 3 1 4

3 5 4 9

2 7 9 16

1 9 16 25

9
RESULT:

Thus the Program was Executed and Output Verified Successfully.

10
1(c) FCFS SCHEDULING

AIM:

To write the program for First Come First serves scheduling

ALGORITHM:

Step 1: Start the program


Step 2: Declare the variables
Step 3: Get the required values using a for loop
Step 4: Print the turnaround time using addition
Step 5: Stop the program

11
CODING:

#include <stdio.h>

void main() {

int i, n, pid[5], bst[5], wait = 0, tarnd;

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

scanf("%d", &n);

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

printf("Enter the process ID: ");

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

printf("Enter the burst time: ");

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

printf("\nPID\tBurst\tWait\tTurnaround");

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

tarnd = wait + bst[i];

printf("\n%d\t%d\t%d\t%d", pid[i], bst[i], wait, tarnd);

wait = tarnd;

12
OUTPUT:

Enter the number of process id: 5

Enter the process id: 1

Enter the burst time: 5

Enter the process id: 2

Enter the burst time: 5

Enter the process id: 3

Enter the burst time: 5

Enter the process id: 4

Enter the burst time: 5

Enter the process id: 5

Enter the burst time: 5

PID Burst Wait Turnaround

1 5 0 5

2 5 5 10

3 5 10 15

4 5 15 20

5 5 20 25

13
RESULT:

Thus the Program was Executed and Output Verified Successfully.

14
1(d) PRIORITY SCHEDULING

AIM:

To write the program for Priority scheduling

ALGORITHM:

Step 1: Start the program


Step 2: Declare the variables
Step 3: Get the data from the user
Step 4: Find out the average burst time and turnaround time
Step 5: Enter the priority based process
Step 6: Print the average burst time and turnaround time
Step 7: Stop the program

15
CODING:
#include<stdio.h>
void main()
{
int bt[20], p[20], wt[20], pr[20], i, j, n, total = 0, pos, temp, avg_wt, avg_tat, tat[20];
printf("enter the number of process:");
scanf("%d", &n);

printf("ente rburst time and priority:\n");


for (i = 0; i < n; i++)
{
printf("\np[%d]\n", i + 1);
printf("burst time");
scanf("%d", &bt[i]);
printf("priority");
scanf("%d", &pr[i]);
p[i] = i + 1;
}

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


{
pos = i;
for (j = i + 1; j < n; j++)
{
if (pr[j] < pr[pos])
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;

16
}

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


{
wt[i] = 0;
for (j = 0; j < i; j++)
wt[i] += bt[j];
total += wt[i];
}

avg_wt = total / n;
total = 0;

printf("\nprocess\tbursttime\twaitingtime\tturnaroundtime");
for (i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
total += tat[i];
printf("\np[%d]\t%d\t%d\t%d\t", p[i], bt[i], wt[i], tat[i]);
}

avg_tat = total / n;
printf("\n Average Waiting time=%d", avg_wt);
printf("\nAverageturnaroundtime=%d", avg_tat);
}

17
OUTPUT:
Enter the number of process: 4

Enter burst time and priority:

p[1]

burst time: 2

priority: 3

p[2]

burst time: 4

priority: 6

p[3]

burst time: 7

priority: 5

p[4]

burst time: 6

priority: 4

Process Burst Time Waiting Time Turnaround Time

p[1] 2 0 2

p[4] 6 2 8

p[3] 7 8 15

p[2] 4 15 19

Average Waiting time = 6

Average Turnaround time = 11

18
RESULT:

Thus the Program was Executed and Output Verified Successfully.

19
2(a) FILE ALLOCATION STRATEGIES(SEQUANTIAL FILE)

AIM:

To write the program for File allocation strategies Sequential file

ALGORITHM:

Step 1: Start the program

Step 2: Get the number of files

Step 3: Get the memory requirements of each file

Step 4: Allocate the required locations to each in sequential order

a) Randomly select a location from available locations: S1 = random(100);


b) Check whether the required locations are from the selected location
c) Allocate and set flag = 1 to the allocated location

Step 5: Print the results — file no, length, blocks allocated

Step 6: Stop the program

20
CODING:
#include <stdio.h>
#include <stdlib.h>

void main()
{
int f[50], i, st, j, len, c, k;

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


f[i] = 0;
x:
printf("\n enter the starting block and length of file: ");
scanf("%d%d", &st, &len);

for (j = st; j < (st + len); j++)


{
if (f[j] == 0)
{
f[j] = 1;
printf("\n%d -> %d", j, f[j]);
}
else
{
printf("Block already allocated");
break;
}
}
if (j == (st + len))
printf("\nthe file is allocated to disk");

printf("\nif you want to enter more files? (y-1 / n-0): ");


scanf("%d", &c);
if (c == 1)
goto x;
else
exit(0);
}

21
OUTPUT:

Enter the starting block and length of file: 3 10

3 -> 1

4 -> 1

5 -> 1

6 -> 1

7 -> 1

8 -> 1

9 -> 1

10 -> 1

11 -> 1

12 -> 1

The file is allocated to disk.

If you want to enter more files? (y-1 / n-0): 0

22
RESULT:

Thus the Program was Executed and Output Verified Successfully.

23
2(b) FILE ALLOCATION STRATEGIES( INDEXED)

AIM:

To write the program for File allocation strategies Indexted

ALGORITHM:

Step 1:Start the program

Step 2:Get the number of files

Step 3:Get the memory requirement of each file

Step 4:Allocate the required location by selecting allocation randomly

Step 5:Print the results — file number, length, blocks allocated

Step 6:Stop the program

24
CODING:
#include <stdio.h>

void main()
{
Int f[50], i, k, j, inde[50], n, c, count = 0, p;

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


f[i] = 0;

printf("enter index block\n");


scanf("%d", &p);

if (f[p] == 0)
{
f[p] = 1;
printf("enter no of files on index: ");
scanf("%d", &n);
}
else
{
printf("Block already allocated\n");
goto x;
}

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


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

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


{
if (f[inde[i]] == 1)
{
printf("Block already allocated");
goto x;
}
}

25
for (j = 0; j < n; j++)
f[inde[j]] = 1;

printf("\nallocated");

printf("\nfile indexed");

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


printf("\n %d -> %d : %d", p, inde[k], f[inde[k]]);

printf("enter 1 to enter more files and 0 to exit\t");


scanf("%d", &c);

if (c == 1)
goto x;
}

26
OUTPUT:

Enter index block: 5

Enter no of files on index: 4

Allocated

File Indexed:

5 -> 6 : 1

5 -> 7 : 1

5 -> 8 : 1

5 -> 9 : 1

Enter 1 to enter more files and 0 to exit: 0

27
RESULT:

Thus the Program was Executed and Output Verified Successfully.

28
2(c) FILE ALLOCATION STRATEGIES( LINKED)

AIM:

To write the program for File allocation strategies Linked

ALGORITHM:

Step-1: Start the program

Step-2: Get the number of files

Step-3: Allocate required location by selecting allocation randomly

Step-4: Check whether the selected location is free

Step-5: If the location is free, allocate it and se

29
CODING:
#include <stdio.h>

void main() {
int f[50], p, i, j, k, a, st, len, n, c;

// Initialize all blocks to 0 (unallocated)


for (i = 0; i < 50; i++)
f[i] = 0;

// Get number of blocks already allocated


printf("\n Enter how many blocks are already allocated: ");
scanf("%d", &p);

// Get the block numbers that are already allocated


printf("\n Enter the block numbers that are already allocated:\n");
for (i = 0; i < p; i++) {
scanf("%d", &a);
f[a] = 1;
}
// Get starting block and length for the new file
printf("\n Enter the starting index block & length: ");
scanf("%d%d", &st, &len);

k = len;
for (j = st; j < (k + st); j++) {
if (f[j] == 0) {
f[j] = 1;
printf("\n%d -> Allocated", j);
} else {
printf("\n %d -> File is already allocated", j);
k++; // Increase k to compensate for already allocated block
}
}

// Ask user if they want to allocate another file


printf("\n Do you want to enter one more file? (yes-1 / no-0): ");

30
scanf("%d", &c);
if (c == 1)
goto x;
}
for (j = 0; j < n; j++)
f[inde[j]] = 1;

printf("\nallocated");

printf("\nfile indexed");

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


printf("\n %d -> %d : %d", p, inde[k], f[inde[k]]);

printf("enter 1 to enter more files and 0 to exit\t");


scanf("%d", &c);

if (c == 1)
goto x;
}

31
OUTPUT:

Enter how many blocks that are already allocated: 3

Enter the blocks no. sprint are already allocated: 6 8 10

Enter the starting index block & length: 3 10

3->1

4->1

5->1

6->file is already allocated

7->1

8->file is already allocated

9->1

10->file is already allocated

11->1

12->1

13->1

14->1

15->1

If you want to enter one more file? (yes-1 / no-0): 0

32
RESULT:

Thus the Program was Executed and Output Verified Successfully.

33
3) SEMAPHORES

AIM:

To write the program for Semaphores

ALGORITHM:

Step 1: Start the program

Step 2: Declare the variable

Step 3: Get producer name from user

Step 4: Sale the products one by one

Step 5: Print the message

Step 6: Stop the program

34
CODING:

#include <stdio.h>

void main() {

int n, a[5], in, out, pr, cr, ch;

in = out = pr = cr = 0;

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

scanf("%d", &n);

do {

printf("\n1. Producer\n2. Customer\n3. Exit");

printf("\n Enter the choice: ");

scanf("%d", &ch);

switch (ch) {

case 1:

if (in >= n) {

printf("\n The buffer is overflowing");

} else {

pr = 1;

in++;

printf("\n Enter the element: ");

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

if (in == n)

pr = 0;

break;

case 2:

35
if (in == out) {

printf("\n The buffer is under flowing");

} else if (pr == 1) {

printf("\n The Producer process is going on");

} else {

cr = 1;

printf("\n The element %d is consumed", a[in]);

in--;

if (in == 0)

cr = 0;

break;

} while (ch != 3);

36
OUTPUT:

Enter the number of elements: 3

1. Producer

2. Customer

3. Exit

Enter the choice: 1

Enter the element: 20

1. Producer

2. Customer

3. Exit

Enter the choice: 1

Enter the element: 40

1. Producer

2. Customer

3. Exit

Enter the choice: 1

Enter the element: 56

1. Producer

2. Customer

3. Exit

Enter the choice: 1

The buffer is overflowing

1. Producer

2. Customer

3. Exit

Enter the choice: 2

37
The element 56 is consumed

1. Producer

2. Customer

3. Exit

Enter the choice: 2

The element 40 is consumed

1. Producer

2. Customer

3. Exit

Enter the choice: 2

The element 20 is consumed

1. Producer

2. Customer

3. Exit

Enter the choice: 2

The buffer is underflowing

1. Producer

2. Customer

3. Exit

Enter the choice: 3

38
RESULT:

Thus the Program was Executed and Output Verified Successfully.

39
4(a) FILE ORGANION TECHIQUES (Single Level Directory)

AIM:

To write the program for File Organization Techniques Single Level Directory

ALGORITHM:

Step 1: Start the Program

Step 2: Initialize values gd = DETECT, gm, count, i, j, mid, cir_x

Step 3: Initialize graph function

Step 4: Set background color with setbkcolor();

Step 5: Read number of files into variable 'count'

Step 6: Check i; mid = 640 / count;

Step 7: Stop the program

40
CODING:

#include <stdio.h>

#include <string.h>

#include <stdlib.h> // for exit()

struct {

char dname[10], fname[10][10];

int fcnt;

} dir;

void main() {

int i, ch;

char f[30];

[Link] = 0;

printf("\n Enter name of directory -- ");

scanf("%s", [Link]);

while (1) {

printf("\n\n1. Create File\t2. Delete File\t3. Search File\n");

printf("4. Display Files\t5. Exit\n");

printf("Enter your choice -- ");

scanf("%d", &ch);

switch (ch) {

case 1:

41
printf("\nEnter the name of the file -- ");

scanf("%s", [Link][[Link]]);

[Link]++;

break;

case 2:

printf("\nEnter the name of the file -- ");

scanf("%s", f);

for (i = 0; i < [Link]; i++) {

if (strcmp(f, [Link][i]) == 0) {

printf("File %s is deleted", f);

strcpy([Link][i], [Link][[Link] - 1]); // Replace with last

[Link]--;

break;

if (i == [Link])

printf("File %s not found", f);

break;

case 3:

printf("\nEnter the name of the file -- ");

scanf("%s", f);

for (i = 0; i < [Link]; i++) {

if (strcmp(f, [Link][i]) == 0) {

printf("File %s is found", f);

break;

42
}

if (i == [Link])

printf("File %s not found", f);

break;

case 4:

if ([Link] == 0)

printf("\nDirectory Empty");

else {

printf("\nThe Files are -- ");

for (i = 0; i < [Link]; i++)

printf("\t%s", [Link][i]);

break;

case 5:

exit(0);

default:

printf("Invalid choice. Try again.");

43
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

44
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

45
RESULT:

Thus the Program was Executed and Output Verified Successfully.

46
4(b) FILE ORGANION TECHIQUES (Two Level Directory)

AIM:

To write the program for File Organization Techniques Two Level Directory

ALGORITHM:

Step 1: Start the Program

Step 2: Initialize structure elements

Step 3: Start main function

Step 4: Set variables gd = DETECT, gm;

Step 5: Create structure using create(&root, 0, "null", 0, 639, 320);

Step 6: Initialize graphics mode using initgraph(&gd, &gm, "C:\\TC\\BGI");

Step 7: Stop the Program

47
CODING:

#include <stdio.h>

#include <string.h>

#include <stdlib.h> // for exit()

struct {

char dname[10], fname[10][10];

int fcnt;

} dir[10];

void main() {

int i, ch, dcnt, k;

char f[30], d[30];

dcnt = 0;

while (1) {

printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");

printf("\n4. Search File\t\t5. Display\t6. Exit");

printf("\nEnter your choice -- ");

scanf("%d", &ch);

switch (ch) {

case 1:

printf("\nEnter name of directory -- ");

scanf("%s", dir[dcnt].dname);

dir[dcnt].fcnt = 0;

48
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++) {

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

50
goto jmp1;

printf("Directory %s not found", d);

jmp1:

break;

case 5:

if (dcnt == 0)

printf("\nNo Directories");

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;

case 6:

exit(0);

default:

printf("Invalid choice!");

// getch(); // Uncomment if using Turbo C

51
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

52
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 DIR not found

53
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

54
RESULT:

Thus the Program was Executed and Output Verified Successfully.

55
4(c) FILE ORGANION TECHIQUES
(HIRARCHICAL DIRECTORY)

AIM:

To write the program for File Organization Techniques Hirarchical Directory

ALGORITHM:

Step 1: Start the Program

Step 2: Define structure and declare structure variables

Step 3: Start main and declare variables

Step 4: Check a directory tree structure

Step 5: Display the directory tree in graphical mode

Step 6: Stop the program

56
CODING:

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>

struct tree_element {
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element* link[5];
};

typedef struct tree_element node;

void create(node** root, int lev, char* dname, int lx, int rx, int x);
void display(node* root);

void main() {
int gd = DETECT, gm;
node* root = NULL;

clrscr();
create(&root, 0, "root", 0, 639, 320);

clrscr();
initgraph(&gd, &gm, "c:\\tc\\BGI");

display(root);

getch();
closegraph();
}

void create(node** root, int lev, char* dname, int lx, int rx, int x) {
int i, gap;
if (*root == NULL) {
*root = (node*)malloc(sizeof(node));
printf("Enter name of dir/file (under %s): ", dname);

57
fflush(stdin);
gets((*root)->name);

printf("Enter 1 for Dir / 2 for file: ");


scanf("%d", &(*root)->ftype);

(*root)->level = lev;
(*root)->y = 50 + lev * 50;
(*root)->x = x;
(*root)->lx = lx;
(*root)->rx = rx;

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


(*root)->link[i] = NULL;
}

if ((*root)->ftype == 1) { // Directory
printf("No of subdirectories/files (for %s): ", (*root)->name);
scanf("%d", &(*root)->nc);

if ((*root)->nc == 0)
gap = rx - lx;
else
gap = (rx - lx) / (*root)->nc;

for (i = 0; i < (*root)->nc; i++) {


create(&((*root)->link[i]), lev + 1, (*root)->name, lx + gap * i, lx + gap * i + gap, lx + gap * i + gap
/ 2);
}
} else {
(*root)->nc = 0; // File
}
}
}

void display(node* root) {


int i;
settextstyle(2, 0, 4);

58
settextjustify(1, 1);
setfillstyle(1, BLUE);
setcolor(14);

if (root != NULL) {
for (i = 0; i < root->nc; i++) {
line(root->x, root->y, root->link[i]->x, root->link[i]->y);
}

if (root->ftype == 1) { // Directory - draw 3D bar


bar3d(root->x - 20, root->y - 10, root->x + 20, root->y + 10, 0, 0);
} else { // File - draw ellipse
fillellipse(root->x, root->y, 20, 20);
}

outtextxy(root->x, root->y, root->name);

for (i = 0; i < root->nc; i++) {


display(root->link[i]);
}
}

59
OUTPUT:

Enter Name of dir/file (under root): ROOT

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files (for ROOT): 2

Enter Name of dir/file (under ROOT): USER1

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for USER1): 1

Enter Name of dir/file (under USER1): SUBDIR

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for SUBDIR): 2

Enter Name of dir/file (under SUBDIR): JAVA

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for JAVA): 0

Enter Name of dir/file (under SUBDIR): VB

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for VB): 0

Enter Name of dir/file (under ROOT): USER2

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for USER2): 2

Enter Name of dir/file (under ROOT): A

60
Enter 1 for Dir / 2 for file: 2

Enter Name of dir/file (under USER2): SUBDIR2

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for SUBDIR2): 2

Enter Name of dir/file (under SUBDIR2): PPL

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for PPL): 2

Enter Name of dir/file (under PPL): B

Enter 1 for Dir / 2 for file: 2

Enter Name of dir/file (under PPL): C

Enter 1 for Dir / 2 for file: 2

Enter Name of dir/file (under SUBDIR): AI

Enter 1 for Dir / 2 for file: 1

No of subdirectories/files (for AI): 2

Enter Name of dir/file (under AI): D

Enter 1 for Dir / 2 for file: 2

Enter Name of dir/file (under AI): E

Enter 1 for Dir / 2 for file: 2

61
RESULT:

Thus the Program was Executed and Output Verified Successfully.

62
4(d) FILE ORGANION TECHIQUES
(DAG)

AIM:

To write the program for File Organization Techniques DAG

ALGORITHM:

Step-1: Start the program.


Step-2: Get the name of the directories.
Step-3: Get the number of files.
Step-4: Get the name of each file.
Step-5: Now, represent each file in the form of a filled circle.
Step-6: Connect every file with its respective directory.
Step-7: Display the connected graphical structure with names using graphics.
Step-8: Stop the program.

63
CODING:

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <string.h>
#include <stdlib.h>

struct tree_element {
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element *link[5];
};

typedef struct tree_element node;

typedef struct {
char from[20];
char to[20];
} link;

link L[10];
int nofl;
node *root;

void create(node **root, int lev, char *dname, int lx, int rx, int x);
void display(node *root);
void read_links();
void draw_link_lines();
void search(node *root, char *s, int *x, int *y);

void main() {
int gd = DETECT, gm;
root = NULL;
clrscr();

64
create(&root, 0, "root", 0, 639, 320);
read_links();
clrscr();
initgraph(&gd, &gm, "c:\\tc\\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
}

void read_links() {
int i;
printf("How many links: ");
scanf("%d", &nofl);
for (i = 0; i < nofl; i++) {
printf("File/dir: ");
fflush(stdin);
gets(L[i].from);
printf("Username: ");
fflush(stdin);
gets(L[i].to);
}
}

void draw_link_lines() {
int i, x1, y1, x2, y2;
for (i = 0; i < nofl; i++) {
search(root, L[i].from, &x1, &y1);
search(root, L[i].to, &x2, &y2);
setcolor(LIGHTGREEN);
setlinestyle(3, 0, 1);
line(x1, y1, x2, y2);
setcolor(YELLOW);
setlinestyle(0, 0, 1);
}

65
}

void search(node *root, char *s, int *x, int *y) {


int i;
if (root != NULL) {
if (strcmpi(root->name, s) == 0) {
*x = root->x;
*y = root->y;
return;
} else {
for (i = 0; i < root->nc; i++) {
search(root->link[i], s, x, y);
}
}
}
}

void create(node **root, int lev, char *dname, int lx, int rx, int x) {
int i, gap;
if (*root == NULL) {
*root = (node *)malloc(sizeof(node));
printf("Enter name of dir/file (under %s): ", dname);
fflush(stdin);
gets((*root)->name);

printf("Enter 1 for dir / 2 for file: ");


scanf("%d", &(*root)->ftype);

(*root)->level = lev;
(*root)->y = 50 + lev * 50;
(*root)->x = x;
(*root)->lx = lx;
(*root)->rx = rx;

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

66
(*root)->link[i] = NULL;

if ((*root)->ftype == 1) {
printf("No of subdirectories/files (for %s): ", (*root)->name);
scanf("%d", &(*root)->nc);

if ((*root)->nc == 0)
gap = rx - lx;
else
gap = (rx - lx) / (*root)->nc;

for (i = 0; i < (*root)->nc; i++)


create(&((*root)->link[i]), lev + 1, (*root)->name,
lx + gap * i, lx + gap * i + gap, lx + gap * i + gap / 2);
} else {
(*root)->nc = 0;
}
}
}

/* Displays the constructed tree in graphics mode */


void display(node *root) {
int i;
settextstyle(2, 0, 4);
settextjustify(1, 1);
setfillstyle(1, BLUE);
setcolor(14);

if (root != NULL) {
for (i = 0; i < root->nc; i++) {
line(root->x, root->y, root->link[i]->x, root->link[i]->y);
}

if (root->ftype == 1)
bar3d(root->x - 20, root->y - 10, root->x + 20, root->y + 10, 0, 0);

67
else
fillellipse(root->x, root->y, 20, 20);

outtextxy(root->x, root->y, root->name);

for (i = 0; i < root->nc; i++) {


display(root->link[i]);
}
}
} setfillstyle(1, BLUE);
setcolor(14);

if (root != NULL) {
for (i = 0; i < root->nc; i++) {
line(root->x, root->y, root->link[i]->x, root->link[i]->y);
}

if (root->ftype == 1) { // Directory - draw 3D bar


bar3d(root->x - 20, root->y - 10, root->x + 20, root->y + 10, 0, 0);
} else { // File - draw ellipse
fillellipse(root->x, root->y, 20, 20);
}

outtextxy(root->x, root->y, root->name);

for (i = 0; i < root->nc; i++) {


display(root->link[i]);
}
}

68
OUTPUT:

Enter Name of dir/file (under root): ROOT

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files (for ROOT): 2

Enter Name of dir/file (under ROOT): USER1

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files (for USER1): 2

Enter Name of dir/file (under USER1): VB

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files (for VB): 2

Enter Name of dir/file (under VB): A

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file (under VB): B

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file (under USER1): C

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file (under ROOT): USER2

69
Enter 1 for Dir / 2 for File: 1

No of subdirectories/files (for USER2): 1

Enter Name of dir/file (under USER2): JAVA

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files (for JAVA): 2

Enter Name of dir/file (under JAVA): D

Enter 1 for Dir / 2 for File: 2

Enter Name of dir/file (under JAVA): HTML

Enter 1 for Dir / 2 for File: 1

No of subdirectories/files (for HTML): 0

How many links: 2

File/Dir: B

UserName: USER2

File/Dir: HTML

UserName: USER1

70
RESULT:

Thus the Program was Executed and Output Verified Successfully.

71
5 ) BANKERS ALGORITHM FOR DEADLOCK
AVOIDANCE

AIM:

To write the program for Bankers Algorithm for Deadlock Avoidance

ALGORITHM:

Step-1: Start the program.


Step-2: Declare the memory for the process.
Step-3: Read the number of processes, resources, allocation matrix, and available matrix.
Step-4: Compare each and every process using the Banker’s algorithm.
Step-5: If the process is in a safe state, then it is safe; otherwise, it is a deadlock process.
Step-6: Produce the result of the state of each process.
Step-7: Stop the program.

72
CODING:

#include <stdio.h>

#include <conio.h> // For getch(), if you're using Turbo C; otherwise remove

int max[20][20], all[20][20], need[20][20], avail[20];

int n, r;

void input();

void show();

void call();

void main() {

int i, j;

printf("Deadlock Avoidance\n");

input();

show();

call();

getch(); // For Turbo C, remove if not needed

void input() {

int i, j;

printf("Enter number of processes: ");

scanf("%d", &n);

printf("Enter number of resource instances: ");

73
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", &all[i][j]);

printf("Enter the Available resources:\n");

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

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

void show() {

int i, j;

printf("\nProcess Allocation Maximum Available\n");

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

printf("P%d\t", i);

74
for (j = 0; j < r; j++) {

printf("%d ", all[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]);

printf("\n");

void call() {

int finish[20], flag = 1, i, j, k;

int safe[20], c1 = 0;

// Calculate Need matrix

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

finish[i] = 0; // Initialize finish array to 0

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

need[i][j] = max[i][j] - all[i][j];

75
}

printf("\nSafe Sequence is: ");

while (flag) {

flag = 0;

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

int c = 0;

if (finish[i] == 0) {

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

if (need[i][j] <= avail[j]) {

c++;

if (c == r) {

// This process can be allocated resources

printf("P%d ", i);

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

avail[k] += all[i][k];

finish[i] = 1;

flag = 1;

76
}

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

if (finish[i] == 1) {

c1++;

if (c1 == n) {

printf("\nThe system is in a Safe State.\n");

} else {

printf("\nProcesses are in Deadlock.\n");

printf("The system is in an Unsafe State.\n");

77
OUTPUT:

DEADLOCK AVOIDANCE

Enter number of processes:

Enter number of resource instances:

Enter the Max matrix:

102

221

Enter the Allocation matrix:

101

111

Enter the available resources:

111

Process Allocation Maximum Available

P0 101 102 111

P1 111 221

Safe sequence is: P0 -> P1 ->

The system is in Safe State.

78
RESULT:

Thus the Program was Executed and Output Verified Successfully.

79
6) DEADLOCK DETECTION

AIM:

To write the program for Deadlock Detection

ALGORITHM:

Step-1: Start the program.


Step-2: Declare the memory for the processes.
Step-3: Read the number of processes, resources, allocation matrix, and available matrix.
Step-4: Compare each process using the Banker’s algorithm.
Step-5: If the process is in safe state, then it is not a deadlock process; otherwise, it is a deadlock
process.
Step-6: Produce the result indicating the state of the processes.
Step-7: Stop the program.

80
CODING:

#include <stdio.h>

int max[20][20], all[20][20], need[20][20], avail[20];

int n, r;

void input();

void show();

void call();

void main() {

int i, j;

printf("Deadlock Detection\n");

input();

show();

call();

getch();

void input() {

int i, j;

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

scanf("%d", &n);

printf("Enter no of resource instances: ");

scanf("%d", &r);

81
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", &all[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\tAllocation\tMaximum\t\tAvailable\n");

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

printf("\nP%d:\t", i);

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

printf("%d ", all[i][j]);

printf("\t\t");

82
for (j = 0; j < r; j++)

printf("%d ", max[i][j]);

if (i == 0) {

printf("\t\t");

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

printf("%d ", avail[j]);

printf("\n");

void call() {

int finish[20], i, j, k, flag = 1, c;

int dead[20], deadCount = 0;

// Calculate the need matrix

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

finish[i] = 0;

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

need[i][j] = max[i][j] - all[i][j];

while (flag) {

flag = 0;

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

83
if (finish[i] == 0) {

c = 0;

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

if (need[i][j] <= avail[j])

c++;

if (c == r) { // All resource needs are satisfied

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

avail[k] += all[i][k]; // Release allocated resources

finish[i] = 1;

flag = 1;

// Identify deadlocked processes

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

if (finish[i] == 0) {

dead[deadCount++] = i;

if (deadCount > 0) {

printf("\nSystem is in deadlock and the deadlocked processes are:\n");

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

84
printf("P%d ", dead[i]);

printf("\n");

} else {

printf("\nNo deadlock occurs.\n");

85
OUTPUT:

DEADLOCK DETECTION

Enter no of processes:

Enter no of resource instances:

Enter the max matrix:

111

212

Enter the allocation matrix:

100

201

Enter the available resources:

000

Process Allocation Maximum Available

P0: 100 111 000

P1: 201 212

System is in deadlock and the deadlock processes are:

P0 P1

86
RESULT:

Thus the Program was Executed and Output Verified Successfully.

87
7(a) PAGE REPLACEMENT ALGORITHM (FIFO)

AIM:
To write the program for Page replacement algorithm FIFO

ALGORITHM:
Step-1: Start the program.
Step-2: Read the number of pages n.
Step-3: Read the number of page numbers.
Step-4: Read the page numbers into an array a[i].
Step-5: Initialize a val[i] = 0 array to check page hits.
Step-6: Print the results.
Step-7: Stop the program

88
CODING:

#include <stdio.h>

void main() {

int a[5], b[20], n, p = 0, q = 0, m = 0, h, k, i, q1 = 1;

char f = 'F';

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

scanf("%d", &n);

printf("Enter the %d page numbers: ", n);

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

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

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

if (p == 0) {

if (q >= 3)

q = 0;

a[q] = b[i];

q++;

if (q1 < 3) {

q1 = q;

89
printf("\n%d\t", b[i]);

for (h = 0; h < q1; h++)

printf("%d ", a[h]);

if ((p == 0) && (q <= 3)) {

printf("--> %c", f); // Page fault

m++;

p = 0;

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

if (b[i + 1] == a[k])

p = 1; // Page hit for next reference

printf("\nNumber of page faults: %d\n", m);

90
OUTPUT:

Enter the number of pages: 6

Enter the 6 page numbers: 1 2 3 2 4 1

1 1 --> F

2 12 --> F

3 123 --> F

2 123

4 423 --> F

1 413 --> F

Number of page faults: 5

91
RESULT:

Thus the Program was Executed and Output Verified Successfully.

92
7(b) PAGE REPLACEMENT ALGORITHM (LRU)

AIM:
To write the program for Page replacement algorithm LRU

ALGORITHM:
Step-1: Start the program

Step-2: Declare the size of the memory (number of frames)

Step-3: Get the number of pages to be issued

Step-4: Declare the necessary data structures:

- Page array

- Counter to track usage

- Stack or array to hold pages in memory

Step-5: For each page reference:

- Check if the page is already in memory (page hit)

- If not present (page fault), find the least recently used page using counter values

- Replace it with the new page

Step-6: Update the stack/memory to reflect the current pages in frames

Step-7: Display the total number of page faults

Step-8: Stop the program

93
CODING:

#include <stdio.h>

void main()

int g = 0, a[5], b[20], p = 0, q = 0, m = 0, n;

int k, i, q1 = 1, j, u, h;

char f = 'F';

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

scanf("%d", &n);

printf("Enter %d page numbers: ", n);

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

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

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

if (p == 0) {

if (q >= 3) q = 0;

a[q] = b[i];

q++;

if (q1 < 3) {

q1 = q;

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

for (h = 0; h < q1; h++)

printf("%d", a[h]);

94
if ((p == 0) && (q <= 3)) {

printf(" --> %c", f); // Page fault

m++;

p = 0;

g = 0;

if (q1 == 3) {

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

if (b[i + 1] == a[k])

p = 1;

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

u = 0;

k = i;

while (k >= (i - 1) && k >= 0) {

if (b[k] == a[j])

u++;

k--;

if (u == 0)

q = j;

} else {

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

if (b[i + 1] == a[k])

95
p = 1;

printf("\nNo. of page faults: %d", m);

96
OUTPUT:

Enter the number of pages: 12

Enter 12 page numbers: 2 3 2 1 5 2 4 5 3 2 3 2

Page Frames Status

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

2 2 --> F

3 23 --> F

2 23

1 231 --> F

5 531 --> F

2 531

4 431 --> F

5 431

3 431 --> F

2 231 --> F

3 231

2 231

No. of page faults: 7

97
RESULT:

Thus the Program was Executed and Output Verified Successfully.

98
7(c) PAGE REPLACEMENT ALGORITHM (LFU)

AIM:
To write the program for Page replacement algorithm LFU

ALGORITHM:

Step-1: Start the program

Step-2: Read Number Of Pages And Frames

Step-3: Read Each Page Value

Step-4: Search For Page In The Frames

Step-5: If Not Available Allocate Free Frame

Step-6: If No Frame Is Free, Replace The Page That Is Least Used

Step-7: Print Page Number Of Page Faults

Step-8: Stop the program

99
CODING:

#include <stdio.h>

void main() {

int rs[50], i, j, k, m, f;

int cntr[20], a[20], min, pf = 0;

printf("Enter no. of page references: ");

scanf("%d", &m);

printf("Enter the reference string: ");

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

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

printf("Enter the available number of frames: ");

scanf("%d", &f);

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

cntr[i] = 0;

a[i] = -1;

printf("\nPage Replacement Process:\n");

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

100
for(j = 0; j < f; j++) {

if(rs[i] == a[j]) {

cntr[j]++;

break;

if(j == f) { // Page fault

min = 0;

for(k = 1; k < f; k++) {

if(cntr[k] < cntr[min])

min = k;

a[min] = rs[i];

cntr[min] = 1;

pf++;

// Print current frame state

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

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

if(j == f)

printf("PF No: %d", pf);

printf("\n");

printf("\nTotal number of page faults: %d\n", pf);

101
OUTPUT:

Enter no. of page references: 12

Enter the reference string: 1 2 3 4 5 2 5 2 5 1 4 3

Enter the available number of frames: 3

Page Replacement Process:

1 -1 -1 PF No: 1

1 2 -1 PF No: 2

1 2 3 PF No: 3

4 2 3 PF No: 4

4 5 3 PF No: 5

4 5 3

4 5 3

4 5 3

4 5 3

1 5 3 PF No: 6

1 4 3 PF No: 7

1 4 5 PF No: 8

Total number of page faults: 8

102
RESULT:

Thus the Program was Executed and Output Verified Successfully.

103
8 )SHARED MEMORIES AND IPC

AIM:

To write the program for SHARED MEMORIES AND IPC

ALGORITHM:

Step-1: Start the program

Step-2: Declare the variables in the structure

Step-3: Declare the variables in the main function

Step-4: Get the values for the variables

Step-5: Get the data from the user

Step-6: Print the data received from the user

Step-7: Stop the program

104
CODING:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/shm.h>

#include <string.h>

void main() {

int i;

void* shared_memory;

char buff[100];

int shmid;

shmid = shmget((key_t)2345, 1024, 0666 | IPC_CREAT);

printf("Key of shared memory is %d\n", shmid);

// Attaching the shared memory segment to process

shared_memory = shmat(shmid, NULL, 0);

printf("Process attached at %p\n", shared_memory);

// Getting data from user

printf("Enter some data to write to shared memory: ");

read(0, buff, sizeof(buff));

// Copying data to shared memory

strcpy(shared_memory, buff);

// Displaying written data

printf("You wrote: %s\n", (char*)shared_memory);

105
OUTPUT:

Key of shared memory is 60

Process attached at 0x7f1bf34b3000

Enter some data to write to shared memory: Hello World

You wrote: Hello World

106
RESULT:

Thus the Program was Executed and Output Verified Successfully.

107
9 ) PAGE TECHNIQUES AND MEMORY MANAGEMENT

AIM:

To write the program for Page techniques and memory management

ALGORITHM:

Step-1: Start the program.

Step-2: Read all the necessary input from the keyboard.

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

Step-4: Frames - Physical memory is broken into fixed-sized blocks.

Step-5: Calculate the physical address using:

Physical Address = (Frame Number * Frame Size) + Offset

Step-6: Display the physical address.

Step-7: Stop the program.

108
CODING:

#include <stdio.h>

#include <stdlib.h>

void main() {

int np, ps, i; // np = number of pages, ps = page size

int *sa; // sa = starting addresses of pages

printf("Enter how many pages:\n");

scanf("%d", &np);

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

scanf("%d", &ps);

// Allocate memory to store starting addresses

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

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

sa[i] = (int) malloc(ps); // Simulate starting address

printf("Page %d\tAddress: %u\n", i + 1, sa[i]);

109
OUTPUT:

Enter how many pages:3

Enter the page size:

1024

Page 1 Address: 17876672

Page 2 Address: 17877712

Page 3 Address: 17878752

110
RESULT:

Thus the Program was Executed and Output Verified Successfully.

111
10 ) PAGE TECHNIQUE AND MEMORY MANAGEMENT

AIM:

To write the program for Page techniques and memory management

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 thread creation; if it fails, print an error message.

Step-7: Stop the program.

112
CODING:

#include <stdio.h>

#include <string.h>

#include <pthread.h>

#include <stdlib.h>

#include <unistd.h>

pthread_t tid[2]; // Thread IDs

int counter = 0;

void* do_something(void* arg) {

unsigned long i = 0;

counter += 1;

printf("\nJob %d started\n", counter);

// Simulate some work

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

printf("Job %d finished\n", counter);

return NULL;

int main(void) {

113
int i = 0;

int err;

while (i < 2) {

err = pthread_create(&(tid[i]), NULL, &do_something, NULL);

if (err != 0)

printf("\nCannot create thread [%s]\n", strerror(err));

i++;

// Wait for both threads to finish

pthread_join(tid[0], NULL);

pthread_join(tid[1], NULL);

return 0;

114
OUTPUT:

Job 1 started

Job 2 started

Job 2 finished

Job 1 finished

115
RESULT:

Thus the Program was Executed and Output Verified Successfully.

116

You might also like