0% found this document useful (0 votes)
27 views22 pages

Os Lab 6,7 Exp

The document provides C programs to illustrate various Inter-Process Communication (IPC) mechanisms including Pipes, FIFOs, Message Queues, and Shared Memory. Additionally, it includes programs simulating memory management techniques like Paging and Segmentation, as well as page replacement policies such as FCFS, LRU, and Optimal. Each section contains code examples, objectives, and expected outputs for better understanding.

Uploaded by

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

Os Lab 6,7 Exp

The document provides C programs to illustrate various Inter-Process Communication (IPC) mechanisms including Pipes, FIFOs, Message Queues, and Shared Memory. Additionally, it includes programs simulating memory management techniques like Paging and Segmentation, as well as page replacement policies such as FCFS, LRU, and Optimal. Each section contains code examples, objectives, and expected outputs for better understanding.

Uploaded by

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

5.

Write C programs to illustrate the following IPC mechanisms a) Pipes b) FIFOs c) Message Queues

d) Shared Memory

Write a C program to illustrate the following IPC mechanisms

a) Pipes b) FIFOs c) Message Queues d) Shared Memory

OBJECTIVE:

Write a C program to illustrate the Pipes IPC mechanism

PROGRAM

#include <stdio.h>

#include <unistd.h>

#define MSGSIZE 16

char* msg1 = "hello, world #1";

char* msg2 = "hello, world #2";

char* msg3 = "hello, world #3";

int main()

char inbuf[MSGSIZE];

int p[2], i;

if (pipe(p) < 0)

exit(1);

/* continued */

/* write pipe */
write(p[1], msg1, MSGSIZE);

write(p[1], msg2, MSGSIZE);

write(p[1], msg3, MSGSIZE);

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

/* read pipe */

read(p[0], inbuf, MSGSIZE);

printf("% s\n", inbuf);

return 0;

Output:

hello, world #1

hello, world #2

hello, world #3

b) Write a C program to illustrate the FIFO IPC mechanism

PROGRAM

#include <stdio.h>

#include <string.h>

#include <fcntl.h>

#include <sys/stat.h>

#include <sys/types.h>

#include <unistd.h>

int main()

{
int fd;

// FIFO file path

char * myfifo = "/tmp/myfifo";

// Creating the named file(FIFO)

// mkfifo(<pathname>, <permission>)

mkfifo(myfifo, 0666);

char arr1[80], arr2[80];

while (1)

// Open FIFO for write only

fd = open(myfifo, O_WRONLY);

// Take an input arr2ing from user.

// 80 is maximum length

fgets(arr2, 80, stdin);

// Write the input arr2ing on FIFO

// and close it

write(fd, arr2, strlen(arr2)+1);

close(fd);

// Open FIFO for Read only

fd = open(myfifo, O_RDONLY);

// Read from FIFO

read(fd, arr1, sizeof(arr1));

// Print the read message


printf("User2: %s\n", arr1);

close(fd);

return 0;

output:
c).Message Queue:

MESSAGE QUEUE FOR WRITER PROCESS


// C Program for Message Queue (Writer Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;

int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);

// msgget creates a message queue


// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;

printf("Write Data : ");


fgets(message.mesg_text,MAX,stdin);

// msgsnd to send message


msgsnd(msgid, &message, sizeof(message), 0);

// display the message


printf("Data send is : %s \n", message.mesg_text);

return 0;
}
MESSAGE QUEUE FOR READER PROCESS
// C Program for Message Queue (Reader Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);

// msgget creates a message queue


// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);

// msgrcv to receive message


msgrcv(msgid, &message, sizeof(message), 1, 0);

// display the message


printf("Data Received is : %s \n",
message.mesg_text);

// to destroy the message queue


msgctl(msgid, IPC_RMID, NULL);

return 0;
}
Output:

d) Write a C program to illustrate the Shared Memory IPC mechanism

PROGRAM:

#include <iostream>

#include <sys/ipc.h>

#include <sys/shm.h>
#include <stdio.h>

using namespace std;

int main()

// ftok to generate unique key

key_t key = ftok("shmfile",65);

// shmget returns an identifier in shmid

int shmid = shmget(key,1024,0666|IPC_CREAT);

// shmat to attach to shared memory

char *str = (char*) shmat(shmid,(void*)0,0);

cout<<"Write Data : ";

gets(str);

printf("Data written in memory: %s\n",str);

//detach from shared memory

shmdt(str);

return 0;

SHARED MEMORY FOR READER PROCESS


#include <iostream>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

using namespace std;

int main()

// ftok to generate unique key

key_t key = ftok("shmfile",65);

// shmget returns an identifier in shmid

int shmid = shmget(key,1024,0666|IPC_CREAT);

// shmat to attach to shared memory

char *str = (char*) shmat(shmid,(void*)0,0);

printf("Data read from memory: %s\n",str);

//detach from shared memory

shmdt(str);

// destroy the shared memory

shmctl(shmid,IPC_RMID,NULL);
return 0;

6. Write C programs to simulate the following memory management techniques a) Paging b)

Segmentation

AIM: To simulate paging technique of memory management.

PROGRAM

#include<stdio.h>

#include<conio.h>

main()

int ms, ps, nop, np, rempages, i, j, x, y, pa, offset; int s[10], fno[10][20];

clrscr();

printf("\nEnter the memory size -- "); scanf("%d",&ms);

printf("\nEnter the page size -- "); scanf("%d",&ps);

nop = ms/ps;

printf("\nThe no. of pages available in memory are -- %d ",nop);

printf("\nEnter number of processes -- "); scanf("%d",&np);

rempages = nop;

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

printf("\nEnter no. of pages required for p[%d]-- ",i); scanf("%d",&s[i]);

if(s[i] >rempages)

printf("\nMemory is Full"); break;

rempages = rempages - s[i];

printf("\nEnter pagetable for p[%d] -,i);

for(j=0;j<s[i];j++)

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

printf("\nEnter Logical Address to find Physical Address ");

printf("\nEnter process no. and pagenumber and offset ");

scanf("%d %d %d",&x,&y, &offset);

if(x>np || y>=s[i] || offset>=ps)

printf("\nInvalid Process or Page Number or offset");

else

pa=fno[x][y]*ps+offset;

printf("\nThe Physical Address is -- %d",pa);

getch();

INPUT

Enter the memory size – 1000


Enter the page size -- 100

The no. of pages available in memory are -- 10

Enter number of processes -- 3

Enter no. of pages required for p[1] -- 4

Enter pagetable for p[1] --- 8 6 9 5

Enter no. of pages required for p[2] -- 5

Enter pagetable for p[2] --- 1 4 5 7 3

Enter no. of pages required for p[3] -- 5

OUTPUT

Memory is Full

Enter Logical Address to find Physical Address

Enter process no. and pagenumber and offset -- 2 3 60

The Physical Address is -- 760

b) OBJECTIVE: To implement the memory management policy-segmentation

SOURCE CODE:

#include<stdio.h>

#include <conio.h>

#include<math.h>

int sost;

void gstinfo();

void ptladdr();

struct segtab

{ int sno;

int baddr;

int limit;
int val[10];

}st[10];

void gstinfo()

{ int i,j;

printf("\n\tEnter the size of the segment table: ");

scanf("%d",&sost);

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

printf("\n\tEnter the information about segment: %d",i);

st[i].sno = i;

printf("\n\tEnter the base Address: ");

scanf("%d",&st[i].baddr);

printf("\n\tEnter the Limit: ");

scanf("%d",&st[i].limit);

for(j=0;j<=sost;i++)

printf("\t\t%d \t\t%d\t\t%d\n\n",st[i].sno,st[i].baddr,st[i].limit);

printf("\n\nEnter the logical Address: ");

scanf("%d",&swd);

n=swd;

while (n != 0)

n=n/10; d++;

MARRI LAXMAN REDDY INSTITUTE OF TECHNOLOGY AND MANAGEMENT OPERATING

SYSTEMS LAB MANUAL

Dept. of CSE 53
}

s = swd/pow(10,d-1);

disp = swd%(int)pow(10,d-1);

if(s<=sost)

if(disp < st[s].limit)

paddr = st[s].baddr + disp;

printf("\n\t\tLogical Address is: %d",swd);

printf("\n\t\tMapped Physical address is: %d",paddr);

printf("\n\tThe value is: %d",( st[s].val[disp] ) );

Else

printf("\n\t\tLimit of segment %d is high\n\n",s);

else

printf("\n\t\tInvalid Segment Address \n");

void main()

{ char ch;

clrscr();

gstinfo();

do

ptladdr();
printf("\n\t Do U want to Continue(Y/N)");

flushall();

scanf("%c",&ch);

}while (ch == 'Y' || ch == 'y' );

getch();

INPUT AND OUTPUT:

Enter the size of the segment table: 3

Enter the information about segment: 1

Enter the base Address: 4

Enter the Limit: 5

Enter the 4 address Value: 11

Enter the 5 address Value: 12

Enter the 6 address Value: 13

Enter the 7 address Value: 14

Enter the 8 address Value: 15

Enter the information about segment: 2

Enter the base Address: 5

Enter the Limit: 4

Enter the 5 address Value: 21

Enter the 6 address Value: 31

Enter the 7 address Value: 41

MARRI LAXMAN REDDY INSTITUTE OF TECHNOLOGY AND MANAGEMENT OPERATING

SYSTEMS LAB MANUAL

Dept. of CSE 54
Enter the 8 address Value: 51

Enter the information about segment: 3

Enter the base Address: 3

Enter the Limit: 4

Enter the 3 address Value: 31

Enter the 4 address Value: 41

Enter the 5 address Value: 41

Enter the 6 address Value: 51

SEGMENT TABLE SEG.NO BASE ADDRESS LIMIT

145

254

334

Enter the logical Address: 3

Logical Address is: 3

Mapped Physical address is: 3

The value is: 31

Do U want to Continue(Y/N)

SEGMENT TABLE SEG.NO BASE ADDRESS LIMIT

145

254

334

Enter the logical Address: 1

Logical Address is: 1

Mapped Physical address is: 4

The value is: 11 Do U want to Continue(Y/N)


7. Write C programs to simulate Page replacement policies a) FCFS b) LRU c) Optimal

PROGRAM:

OPTIMAL PAGE REPLACEMENT ALGORITHM


#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k,
pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page reference string: ");
for(i = 0; i < no_of_pages; ++i)
scanf("%d", &pages[i]);

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


frames[i] = -1;
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i])
{
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0)
{
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k])
{
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j)
printf("%d\t", frames[j]);
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}

OUTPUT:
Enter number of frames: 3
Enter number of pages: 10
Enter page reference string: 2 3 4 2 1 3 7 5 4 3
2 -1 -1
2 3 -1
234
234
134
134
734
534
534
534
Total Page Fault=6

LRU PAGE REPLACEMENT ALGORITHM


#include<stdio.h>
int findLRU(int time[], int n)
{
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i)
{
if(time[i] < minimum)
{
minimum = time[i];
pos = i;
}
}
return pos;
}
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j,
pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for(i = 0; i < no_of_pages; ++i)
scanf("%d", &pages[i]);
for(i = 0; i < no_of_frames; ++i)
frames[i] = -1;
for(i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0)
{
for(j = 0; j < no_of_frames; ++j)
{
if(frames[j] == -1)
{
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0)
{
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");

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


printf("%d\t", frames[j]);
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
OUTPUT:
Enter number of frames: 3
Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3
5 -1 -1
5 7 -1
5 7 -1
576
576
376
Total Page Faults = 4
FIFO page replacement algorithm
#include<stdio.h>
int main()
{
int incomingStream[] = {4, 1, 2, 4, 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;

pages = sizeof(incomingStream)/sizeof(incomingStream[0]);

printf("Incoming \t Frame 1 \t Frame 2 \t Frame 3");


int temp[frames];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}

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


{
s = 0;

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


{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;

if((pageFaults <= frames) && (s == 0))


{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}

printf("\nTotal Page Faults:\t%d\n", pageFaults);


return 0;
}

Incoming Frame 1 Frame 2 Frame 3


4 4 - -
1 4 1 -
2 4 1 2
4 4 1 2
5 5 1 2
Total Page Faults: 4

You might also like