Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Os Lavanya

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 28

Pratical -7

Banker’s Algorithm
Code:
#include<stdio.h>
#include<stdlib.h>
void print(int x[][10],int n,int m){
int i,j;
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<m;j++){
printf("%d\t",x[i][j]);
}
}
}
void res_request(int A[10][10],int N[10][10],int AV[10][10],int pid,int m)
{
int reqmat[1][10];
int i;
printf("\n Enter additional request :- \n");
for(i=0;i<m;i++){
printf(" Request for resource %d : ",i+1);
scanf("%d",&reqmat[0][i]);
}

for(i=0;i<m;i++)
if(reqmat[0][i] > N[pid][i]){
printf("\n Error encountered.\n");
exit(0);
}

for(i=0;i<m;i++)
if(reqmat[0][i] > AV[0][i]){
printf("\n Resources unavailable.\n");
exit(0);
}

for(i=0;i<m;i++){
AV[0][i]-=reqmat[0][i];
A[pid][i]+=reqmat[0][i];
N[pid][i]-=reqmat[0][i];
}
}
int safety(int A[][10],int N[][10],int AV[1][10],int n,int m,int a[]){

int i,j,k,x=0;
int F[10],W[1][10];
int pflag=0,flag=0;
for(i=0;i<n;i++)
F[i]=0;
for(i=0;i<m;i++)
W[0][i]=AV[0][i];
printf("Avail Matrix");
for(k=0;k<n;k++){
for(i=0;i<n;i++){
if(F[i] == 0){
flag=0;
for(j=0;j<m;j++){
if(N[i][j] > W[0][j])
flag=1;
}
if(flag == 0 && F[i] == 0){
printf("\n ");
for(j=0;j<m;j++){
W[0][j]+=A[i][j];
printf("%d ",W[0][j]);
}
F[i]=1;
pflag++;
a[x++]=i;
}
}
}
if(pflag == n)
return 1;
}
return 0;
}
void accept(int A[][10],int N[][10],int M[10][10],int W[1][10],int *n,int *m){
int i,j;
printf("\n Enter total no. of processes : ");
scanf("%d",n);
printf("\n Enter total no. of resources : ");
scanf("%d",m);
printf("\nEnter Allocation Matrix \n");
for(i=0;i<*n;i++){
for(j=0;j<*m;j++){
scanf("%d",&A[i][j]);
}
}
printf("\nEnter Max Matrix \n");
for(i=0;i<*n;i++){
for(j=0;j<*m;j++){
scanf("%d",&M[i][j]);
}
}
printf("\n Available resources : \n");
for(i=0;i<*m;i++){
printf(" Resource %d : ",i+1);
scanf("%d",&W[0][i]);
}
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
N[i][j]=M[i][j]-A[i][j];
printf("\n Need Matrix");
print(N,*n,*m);
}
int banker(int A[][10],int N[][10],int W[1][10],int n,int m){
int j,i,a[10];
j=safety(A,N,W,n,m,a);
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");
if(j != 0 ){
printf("\n\n");
for(i=0;i<n;i++)
printf(" P%d ",a[i]);
printf("\n A safety sequence has been detected.\n");
return 1;
}else{
printf("\n Deadlock has occured.\n");
return 0;
}
}
int main(){
int ret;
int A[10][10];
int M[10][10];
int N[10][10];
int W[1][10];
int n,m,pid,ch;
printf("\n DEADLOCK AVOIDANCE USING BANKER'S ALGORITHM\n");
accept(A,N,M,W,&n,&m);
ret=banker(A,N,W,n,m);
return 0;
}
Output:
DEADLOCK AVOIDANCE USING BANKER'S ALGORITHM
Enter total no. of processes : 5
Enter total no. of resources : 3
Enter Allocation Matrix
010
200
302
211
002
Enter Max Matrix
753
322
902
422
533
Available resources :
Resource 1 : 3
Resource 2 : 3
Resource 3 : 2
Need Matrix
7 4 3
1 2 2
6 0 0
2 1 1
5 3 1
Avail Matrix
532
743
745
755
10 5 7

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

P1 P3 P4 P0 P2
A safety sequence has been detected.
Practical -9
FIFO
Code:
#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("\nNAME : Nishtha Srivastava ");
printf("\nROLL NO. 2200321530121");
printf("\nPage Fault Is %d",count);
printf("\nPage Hit Is : %d",(n-count));
float temp=n;
printf("\nPage Hit Ratio : %.2f",(temp-count)/temp);
return 0;
}
Output:

ENTER THE NUMBER OF PAGES:


20

ENTER THE PAGE NUMBER :


70120304230321201701

ENTER THE NUMBER OF FRAMES :3

Ref String Frame


7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
0 2 0 1
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
3 0 2 3
2 0 2 3
1 0 1 3
2 0 1 2
0 0 1 2
1 0 1 2
7 7 1 2
0 7 0 2
1 7 0 1

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

Page Fault Is 15
Page Hit Is : 5
Page Hit Ratio : 0.25
Practical -10
LRU
Code:
#include<stdio.h>
#include<limits.h>
int checkHit(int incomingPage, int queue[], int occupied){
for(int i = 0; i < occupied; i++){
if(incomingPage == queue[i])
return 1;
}
return 0;
}
void printFrame(int queue[], int occupied)
{
for(int i = 0; i < occupied; i++)
printf("%d\t\t\t",queue[i]);
}
int main()
{
int n ;
printf("Enter no of pages:");
scanf("%d", &n);
printf("Enter the reference string:");
int incomingStream[n];
for(int i=0;i<n;i++){
scanf("%d",&incomingStream[i]);
}
int frames ;
printf("Enter the no. of frames");
scanf("%d", &frames);
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0,pagehit;
printf("Ref String\t Page Frames\n");
for(int i = 0;i < n; i++)
{
printf("%d: \t\t",incomingStream[i]);
if(checkHit(incomingStream[i], queue, occupied)){
printFrame(queue, occupied);
}
else if(occupied < frames){
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;
printFrame(queue, occupied);
}
else{
int max = INT_MIN;
int index;
for (int j = 0; j < frames; j++)
{
distance[j] = 0;
for(int k = i - 1; k >= 0; k--)
{
++distance[j];
if(queue[j] == incomingStream[k])
break;
}
if(distance[j] > max){
max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied);
pagefault++;
}
printf("\n");
}
printf("\nNAME : Nishtha Srivastava ");
printf("\nROLL NO. 2200321530121");

pagehit=n-pagefault;
printf("Page Hit: %d\n",pagehit);
printf("Page Fault: %d\n",pagefault);
float Hit,Fault;
Hit=(pagehit*100)/n;
Fault=(pagefault*100)/n;
printf("Hit Ratio: %.2f\n",Hit);
printf("Page Fault Ratio: %.2f\n",Fault);
return 0;
}
Output:
Enter no of pages:20
Enter the reference string:7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter the no. of frames 33
Ref String Page Frames
7: 7
0: 7 0
1: 7 0 1
2: 2 0 1
0: 2 0 1
3: 2 0 3
0: 2 0 3
4: 4 0 3
2: 4 0 2
3: 4 3 2
0: 0 3 2
3: 0 3 2
2: 0 3 2
1: 1 3 2
2: 1 3 2
0: 1 0 2
1: 1 0 2
7: 1 0 7
0: 1 0 7
1: 1 0 7

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

Page Hit: 8
Page Fault: 12
Hit Ratio: 40.00
Page Fault Ratio: 60.00
Practical :8 (a)
Internal Fragmentation First-Fit
Code:
#include<stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n){
int i, j;
int allocation[n];
for(i = 0; i < n; i++) {
allocation[i] = -1;
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < n; i++)
{
printf(" %i\t\t\t", i+1);
printf("%i\t\t\t\t", processSize[i]);
if (allocation[i] != -1)
printf("%i", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}
int main(){
int m;
int n;
printf("Enter the no. of blocks");
scanf("%d",&m);
printf("Enter the no. of processes");
scanf("%d",&n);
int blockSize[m] ;
int processSize[n] ;
for(int i=0; i<m; i++){
printf("\nEnter the block size %d",i+1);
scanf("%d",&blockSize[i]);
}
for(int i=0; i<n; i++){
printf("\nEnter the process size %d",i+1);
scanf("%d",&processSize[i]);
}
firstFit(blockSize, m, processSize, n);
return 0 ;
}

Output:

Enter the no. of blocks 5


Enter the no. of processes 4

Enter the block size 1 100

Enter the block size 2 500

Enter the block size 3 200

Enter the block size 4 300

Enter the block size 5 600

Enter the process size 1 212

Enter the process size 2 417

Enter the process size 3 96

Enter the process size 4 426

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

Process No. Process Size Block no.


1 212 2
2 417 5
3 96 1
4 426 Not Allocated
Practical :8 (b)
Internal Fragmentation Best-Fit
Code:
#include <stdio.h>

void implimentBestFit(int blockSize[], int blocks, int processSize[], int proccesses){


int allocation[proccesses];
int occupied[blocks];
for(int i = 0; i < proccesses; i++){
allocation[i] = -1;
}
for(int i = 0; i < blocks; i++){
occupied[i] = 0;
}
for (int i = 0; i < proccesses; i++)
{
int indexPlaced = -1;
for (int j = 0; j < blocks; j++) {
if (blockSize[j] >= processSize[i] && !occupied[j])
{
if (indexPlaced == -1)
indexPlaced = j;
else if (blockSize[j] < blockSize[indexPlaced])
indexPlaced = j;
}
}
if (indexPlaced != -1)
{
allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
}
}
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < proccesses; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}
int main()
{
int blocks,proccesses;
printf("Enter the no. of blocks ");
scanf("%d",&blocks);
printf("Enter the no. of processes ");
scanf("%d",&proccesses);
int blockSize[blocks] ;
int processSize[proccesses] ;
for(int i=0; i<blocks; i++){
printf("\nEnter the block size %d ",i+1);
scanf("%d",&blockSize[i]);
}
for(int i=0; i<proccesses; i++){
printf("\nEnter the process size %d ",i+1);
scanf("%d",&processSize[i]);
}
implimentBestFit(blockSize, blocks, processSize, proccesses);
return 0 ;
}

Output:

Enter the no. of blocks 5

Enter the no. of processes 4

Enter the block size 1 100


Enter the block size 2 500
Enter the block size 3 200
Enter the block size 4 300
Enter the block size 5 600
Enter the process size 1 212
Enter the process size 2 417
Enter the process size 3 96
Enter the process size 4 426

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

Process No. Process Size Block no.


1 212 4
2 417 2
3 112 1
4 426 5
Practical :8 (c)
Internal Fragmentation Wrost-Fit
Code:
#include <stdio.h>

void implimentWorstFit(int blockSize[], int blocks, int processSize[], int processes)


{
// This will store the block id of the allocated block to a process
int allocation[processes];

// initially assigning -1 to all allocation indexes


// means nothing is allocated currently
for(int i = 0; i < processes; i++){
allocation[i] = -1;
}

// pick each process and find suitable blocks


// according to its size ad assign to it
for (int i=0; i<processes; i++)
{

int indexPlaced = -1;


for (int j=0; j<blocks; j++)
{
if (blockSize[j] >= processSize[i])
{
// place it at the first block fit to accomodate process
if (indexPlaced == -1)
indexPlaced = j;

// if any future block is larger than the current block where


// process is placed, change the block and thus indexPlaced
else if (blockSize[indexPlaced] < blockSize[j])
indexPlaced = j;
}
}

// If we were successfully able to find block for the process


if (indexPlaced != -1)
{
// allocate this block j to process p[i]
allocation[i] = indexPlaced;

// Reduce available memory for the block


blockSize[indexPlaced] -= processSize[i];
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < processes; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

// Driver code
int main()
{
int blocks,proccesses;
printf("Enter the no. of blocks ");
scanf("%d",&blocks);
printf("Enter the no. of processes ");
scanf("%d",&proccesses);
int blockSize[blocks] ;
int processSize[proccesses] ;
for(int i=0; i<blocks; i++){
printf("\nEnter the block size %d ",i+1);
scanf("%d",&blockSize[i]);
}
for(int i=0; i<proccesses; i++){
printf("\nEnter the process size %d ",i+1);
scanf("%d",&processSize[i]);
}
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");

implimentWorstFit(blockSize, blocks, processSize, proccesses);

return 0 ;
}
Output:
Enter the no. of blocks 5

Enter the no. of processes 4

Enter the block size 1 100


Enter the block size 2 500
Enter the block size 3 200
Enter the block size 4 300
Enter the block size 5 600
Enter the process size 1 212
Enter the process size 2 417
Enter the process size 3 96
Enter the process size 4 426

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

Process No. Process Size Block no.


1 212 5
2 417 2
3 96 4
4 426 Not Allocated
Practical-6
Priority(Preemptive)
Code:
#include<stdio.h>

struct process

int WT,AT,BT,TAT,PT,CT,RT;

};

struct process a[10];

int main()

int n,temp[10],t,count=0,short_p,i;

float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;

printf("Enter the no. of process\n");

scanf("%d",&n);

printf("Input the arrival time , burst time and priority of the process\n");

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

scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);

// adding a duplicate of the burst time

// temporary array for future use

temp[i]=a[i].BT;

// initializing burst time


// of a process with maximum

a[9].PT=10000;

for(t=0;count!=n;t++)

short_p=9;

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

if(a[short_p].PT>a[i].PT && a[i].AT<=t && a[i].BT>0)

short_p=i;

}
if(a[short_p].BT=temp[short_p])
a[short_p].RT=t-a[short_p].AT;
a[short_p].BT=a[short_p].BT-1;

// if condition on any process is completed

if(a[short_p].BT==0)

// one process is completed

// so count increases by 1

count++;

a[short_p].WT=t+1-a[short_p].AT-temp[short_p];

a[short_p].TAT=t+1-a[short_p].AT;

// total cal
a[short_p].CT=t+1;

total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT;
total_RT=total_RT+a[short_p].RT;
}
}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
Avg_RT=total_RT/n;
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");

printf("ID\tAT\tBT\tP\tCT\tWT\tTAT\tRT\n");

for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\
n",i+1,a[i].AT,temp[i],a[i].PT,a[i].CT,a[i].WT,a[i].TAT,a[i].RT);
}
printf("Avg waiting time is %f\n",Avg_WT);
printf("Avg turn around time is %f\n",Avg_TAT);
printf("Avg turn around time is %f\n",Avg_RT);
return 0;
}

Output:
Enter the no. of process
4
Input the arrival time , burst time and priority of the process
054
143
222
411

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

ID AT BT P CT WT TAT RT
1 0 5 4 12 7 12 0
2 1 4 3 8 3 7 0
3 2 2 2 4 0 2 0
4 4 1 1 5 0 1 0
Avg waiting time is 2.500000
Avg turn around time is 5.500000
Avg response time is 0.000000

Priority(Non-Preemptive)
Code:

#include<stdio.h>

struct process

int WT,AT,BT,TAT,PT,CT;

};

struct process a[10];

int main()

int n,temp[10],t,count=0,short_p,i;

float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;

printf("Enter the no. of process\n");

scanf("%d",&n);

printf("Input the arrival time , burst time and priority of the process\n");

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

scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);

temp[i]=a[i].BT;

// initializing burst time

// of a process with maximum

a[9].PT=10000;

for(t=0;count!=n;)

{
short_p=9;

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

if(a[short_p].PT>a[i].PT && a[i].AT<=t && a[i].BT>0)

short_p=i;

}
t+=a[short_p].BT;

a[short_p].BT=0;

// if condition on any process is completed

if(a[short_p].BT==0)

count++;

a[short_p].WT=t+1-a[short_p].AT-temp[short_p];

a[short_p].TAT=t+1-a[short_p].AT;

// total cal
a[short_p].CT=t+1;

total_WT=total_WT+a[short_p].WT;

total_TAT=total_TAT+a[short_p].TAT;

}
}

Avg_WT=total_WT/n;

Avg_TAT=total_TAT/n;

// printing of the answer


printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");

printf("ID\tAT\tBT\tP\tCT\tWT\tTAT\tRT\n");

for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\
n",i+1,a[i].AT,temp[i],a[i].PT,a[i].CT,a[i].WT,a[i].TAT,a[i].WT);
}
printf("Avg waiting time is %f\n",Avg_WT);
printf("Avg turn around time is %f\n",Avg_TAT);
return 0;
}

Output
Enter the no. of process
4
Input the arrival time , burst time and priority of the process
054
143
222
411

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

ID AT BT P CT WT TAT RT
1 0 5 4 6 1 6 1
2 1 4 3 13 8 12 8
3 2 2 2 9 5 7 5
4 4 1 1 7 2 3 2
Avg waiting time is 4.000000
Avg turn around time is 7.000000

Practical-11
Simulate Paging Technique of Memory Management
Code:

#include<stdio.h>
#define MAX 50
int main()
{

int page[MAX],i,n,f,ps,off,pno;
int choice=0;
printf("\nEnter the no of peges in memory: ");
scanf("%d",&n);
printf("\nEnter page size: ");
scanf("%d",&ps);
printf("\nEnter no of frames: ");
scanf("%d",&f);
for(i=0;i<n;i++)
page[i]=-1;
printf("\nEnter the page table\n");
printf("(Enter frame no as -1 if that page is not present in any frame)\n\n");
printf("\npageno\tframeno\n-------\t-------");
for(i=0;i<n;i++)
{
printf("\n\n%d\t\t",i);
scanf("%d",&page[i]);
}
do
{
printf("\n\nEnter the logical address(i.e,page no & offset):");
scanf("%d%d",&pno,&off);

if(page[pno]==-1)
printf("\n\nThe required page is not available in any of frames");
else
printf("\n\nPhysical address(i.e,frame no & offset):%d,%d",page[pno],off);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");

return 1;
}

Output:
Enter the no of pages in memory: 4

Enter page size: 10

Enter no of frames: 10

Enter the page table


(Enter frame no as -1 if that page is not present in any frame)

pageno frameno
------- -------

0 -1

1 8

2 -1

3 6

Enter the logical address(i.e,page no & offset):2 200

The required page is not available in any of frames


Do you want to continue(1/0)?:1

Enter the logical address(i.e,page no & offset):1 500

Physical address(i.e,frame no & offset):8,500


Do you want to continue(1/0)?:0

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

Practical-12(a)
IPC using Pipes
Code:
#include<stdio.h>
#include<unistd.h>

int main() {
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");
int pipefds[2];
int returnstatus;
int pid;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}
pid = fork();
// Child process
if (pid == 0) {
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 1 is %s\n", readmessage);
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 2 is %s\n", readmessage);
} else { //Parent process
printf("Parent Process - Writing to pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
printf("Parent Process - Writing to pipe - Message 2 is %s\n", writemessages[1]);
write(pipefds[1], writemessages[1], sizeof(writemessages[1]));
}
return 0;
}

Output:
NAME : Nishtha Srivastava
ROLL NO. 2200321530121

Parent Process - Writing to pipe - Message 1 is Hi


Parent Process - Writing to pipe - Message 2 is Hello
Child Process - Reading from pipe – Message 1 is Hi
Child Process - Reading from pipe – Message 2 is Hello

Practical-12(b)
IPC using Message Queues
Code:
// 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() {
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");

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

Output:
NAME : Nishtha Srivastava
ROLL NO. 2200321530121

Write Data : Hello


Data send is : Hello

// 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()
{
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");

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:
NAME : Nishtha Srivastava
ROLL NO. 2200321530121

Data Received is : Hello

Practical-12(c)
IPC using Shared Memory
Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main() {
printf("\nNAME : Nishtha Srivastava");
printf("\nROLL NO. 2200321530121");
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);

//creates shared memory segment with key 2345, having size 1024 bytes. IPC_CREAT
//is used to create the shared segment if it does not exist. 0666 are the
permissions //on the shared segment

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


shared_memory=shmat(shmid,NULL,0);

//process attached to shared memory segment


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

//this prints the address where the segment is attached with this process
printf("Enter some data to write to shared memory\n");

read(0,buff,100); //get some input from user

strcpy(shared_memory,buff); //data written to shared memory


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

Output:

NAME : Nishtha Srivastava


ROLL NO. 2200321530121

Key of shared memory is 0


Process attached at 0x7ffe040fb000
Enter some data to write to shared memory
Hello World
You wrote: Hello World

You might also like