Last Programs Os New File Eeeee

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

OS

EXPERIMENT-5

AIM: 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

a) pipes

program

#include<stdio.h>

#include<unistd.h>

#include<stdlib.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);

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


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

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

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

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

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

return 0;

OUTPUT:
[227Y1A0544@localhost ~]$ vi pipes.c

[227Y1A0544@localhost ~]$ cc pipes.c

[227Y1A0544@localhost ~]$ ./a.out

hello, world #1

hello, world #2

hello, world #3
AIM: Write a C program to illustrate the following IPC mechanisms
b) FIFO
OBJECTIVE:
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;
nt main()
{
int fd;
char *myfifo ="myfile.txt";
mkfifo(myfifo, 0666);
char arr1[80], arr2[80];
while (1)
{
fd = open(myfifo, O_WRONLY);
scanf("%s",arr2, 80, stdin);
write(fd, arr2, strlen(arr2)+1);
close(fd);
fd = open(myfifo, O_RDONLY);
read(fd, arr1, sizeof(arr1));
printf("User2: %s\n&", arr1);
close(fd);
}
return 0;
}

OUTPUT:
[227Y1A0544@localhost ~]$ cat>myfile.txt
^Z
[6]+ Stopped cat > myfile.txt
[227Y1A0544@localhost ~]$ vi f1.c
[227Y1A0544@localhost ~]$ cc f1.c
[227Y1A0544@localhost ~]$ ./a.out
hii
User2: hii
AIM: Write a C program to illustrate the following IPC mechanisms
c) MESSAGE QUEUES

OBJECTIVE:
Write a C program to illustrate the MESSAGE QUEUES IPC mechanism

c program for message queue (writer process)

#include <stdio.h>

#include <sys/ipc.h>

#include <sys/msg.h>

struct mesg_buffer

long mesg_type;

char mesg_text[100];

}message;

int main()

key_t key;

int msgid;

key=ftok("progfile",65);

msgid=msgget(key,0666|IPC_CREAT);

message.mesg_type=1;

printf("Write Data:");

scanf("%s",message.mesg_text);

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


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

return 0;

OUTPUT:

[227Y1A0544@localhost ~]$ vi 5c.c

[227Y1A0544@localhost ~]$ cc 5c.c

[227Y1A0544@localhost ~]$ ./a.out

Write Data: aaaa

Data send is : aaaa

c program for message queue (Reader process)

#include <stdio.h>

#include <sys/ipc.h>

#include <sys/msg.h>

struct mesg_buffer

long mesg_type;

char mesg_text[100];

}message;

int main()

key_t key;
int msgid;

key = ftok("progfile",65);

msgid = msgget(key,0666|IPC_CREAT);

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

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

msgctl(msgid,IPC_RMID,NULL);

return 0;

OUTPUT:

[227Y1A0544@localhost ~]$ cc 5cc.c

[227Y1A0544@localhost ~]$ ./a.out

Data Received is:aaaa


AIM: Write a C program to illustrate the following IPC mechanisms
d) shared memory

OBJECTIVE:
Write a C program to illustrate the shared memory IPC mechanism

shared memory for writer process


#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

int main()

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

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

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

printf("Write Data:");

scanf("%s",str);

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

shmdt(str);

return 0;

output:

[227Y1A0544@localhost ~]$ vi ff.c

[227Y1A0544@localhost ~]$ cc ff.c

[227Y1A0544@localhost ~]$ ./a.out

Write Data:welcome

Data written in memory: welcome


shared memory for reader process
#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

int main()

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

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

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

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

shmdt(str);

shmctl(shmid,IPC_RMID,NULL);

return 0;

[227Y1A0544@localhost ~]$ vi ff2.c

[227Y1A0544@localhost ~]$ cc ff2.c

[227Y1A0544@localhost ~]$ ./a.out

Data read from memory: welcome


Experiment -5

Viva questions

1. What is FIFO?

2. Differentiate between logical and physical address.

3. What is the criteria for the best page replacement algorithm?

4. What is Shared Memory?

5. What is Message Queue?

6. List the components of message Queue.

7. What are advantages of Message Queue.

8. What is page fault?

9. What is demand paging?

10. List various types IPC mechanism.


EXPERIMENT-6
Write a C program to simulate the following techniques of memory management

a) Paging

AIM: To simulate paging technique of memory management.

#include<stdio.h>

main()

int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;

int s[10], fno[10][20];

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

[227Y1A0571@localhost os]$ vi paging.c

[227Y1A0571@localhost os]$ cc paging.c

[227Y1A0571@localhost os]$ ./a.out

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

Memory is Full

Enter Logical Address to find Physical Address

Enter process no. and page number and offset 2 3 60

The Physical Address is -- 760

Write a C program to simulate the following techniques of memory management

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

AIM: To simulate segmentation technique of memory management.

#include<stdio.h>
#include<stdlib.h>
struct list
{
int seg;
int base;
int limit;
struct list *next;
} *p;
void insert(struct list *q,int base,int limit,int seg)
{
if(p==NULL)
{
p=malloc(sizeof(struct list));
p->limit=limit;
p->base=base;
p->seg=seg;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
q=q->next;
printf("yes");
}
q->next=malloc(sizeof(struct list));
q->next ->limit=limit;
q->next ->base=base;
q->next ->seg=seg;
q->next ->next=NULL;
}
}
int find(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->limit;
}
int search(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->base;
}
main()
{
p=NULL;
int seg,offset,limit,base,c,s,physical;
printf("enter segment table\n");
printf("enter -1 as segment value for termination\n");
do
{
printf("enter seg number:");
scanf("%d",&seg);
if(seg!=-1)
{
printf(" enter base value");
scanf("%d",&base);
printf("enter value for limit");
scanf("%d",&limit);
insert(p,base,limit,seg);
}
}
while(seg!=-1);
printf("enter offset");
scanf("%d",&offset);
printf("enter b segmentation number");
scanf("%d",&seg);
c=find(p,seg);
s=search(p,seg);
if(offset<c)
{
physical=s+offset;
printf("address in physical memory%d\n",physical);
}
else
{
printf("error");
}
output:
[227Y1A0541@localhost ~]$ vi segnew.c
[227Y1A0541@localhost ~]$ cc segnew.c
[227Y1A0541@localhost ~]$ ./a.out
enter segment table
enter -1 as segment value for termination
enter seg number:1
enter base value2000
enter value for limit100
enter seg number:2
enter base value2500
enter value for limit100
enter seg number:-1
enter offset90
enter b segmentation number2
address in physical memory2590
(or)

#include <stdio.h>

int main()

int a[100][100],b[1000],i,j,n,x,base,size,seg,off;

printf("Enter The Segment Count:\n");

scanf("%d",&n);

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

printf("Enter The %d Size:\n",i+1);

scanf("%d",&size);

a[i][0]=size;

printf("Enter The Base Address\n");

scanf("%d",&base);

a[i][i]=base;

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

x=0;

scanf("%d",&x);

b[base]=x;

base++;

b[base]=x;

printf("Enter The Segment No And OffSet Value:\n");

scanf("%d %d",&seg,&off);

if(off<a[seg][0])

{
int abs=a[seg][1]+off;

printf("The OffSet Is Less Than :%d",a[seg][0]);

printf("\n %d+%d=%d\n",a[seg][1],off,abs);

printf("The Element %d Is At %d",b[abs+1],abs);

else

printf("Error in Locating");

Output:

[227Y1A0544@localhost ~]$ ./a.out

Enter The Segment Count:

Enter The 1 Size:

Enter The Base Address

12345

Enter The 2 Size:

Enter The Base Address

10 7 8 9

Enter The Segment No And OffSet Value:

12

The OffSet Is Less Than :3

10+2=12

The Element 9 Is At 12
Experiment-6
Viva questions
1. What is memory management?

2. What is Virtual memory?

3. Define thrashing.

4. List various type page replacement alg.

5. Define Swapping.

6. What is Direct Access Method?

7. List the types of memory.

8. Define Segmentation.

9. Define Fragmentation.

10. Explain compaction.

You might also like