0% found this document useful (0 votes)
3 views

s Hatred Memory

The document contains various C programming examples demonstrating inter-process communication techniques such as shared memory, message queues, FIFO, pipes, paging, segmentation, deadlock avoidance, deadlock prevention, and producer-consumer problem. Each section includes code snippets that illustrate the implementation of these concepts. The examples cover memory management, synchronization, and process coordination in operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

s Hatred Memory

The document contains various C programming examples demonstrating inter-process communication techniques such as shared memory, message queues, FIFO, pipes, paging, segmentation, deadlock avoidance, deadlock prevention, and producer-consumer problem. Each section includes code snippets that illustrate the implementation of these concepts. The examples cover memory management, synchronization, and process coordination in operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 19

SHARED MEMORY

#include<stdio.h>

#include<sys/types.h>

#include<sys/shm.h>

#include<sys/ipc.h>

int main()

int shmid;

key_t key=0x10;

shmid=shmget(key,100,IPC_CREAT|0666);

if(shmid<0 )

printf("\nFirst SHMID failed\n");

else

printf("\nFirst SHMID Succeded id=%d \n",shmid);

shmid=shmget(key,101,IPC_CREAT|0666);

if(shmid<0)

printf("\nSecond SHMID failed\n");

else

printf("\nSecond SHMID Succeded id=%d \n",shmid);

shmid=shmget(key,90,IPC_CREAT|0666);

if(shmid<0)

printf("\nThird SHMID failed\n");

else

printf("\n Third SHMID Succeded id=%d \n",shmid);

}
MESSAGE QUEUE

#include<stdio.h>

#include<string.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#include<stdlib.h>

#include<unistd.h>

int main()

int pid,len,qid;

struct

long mtype;

char mtext[10];

message;

system("clear");

printf(" \n Inter Process communication using message queue \n");

qid = msgget((key_t)13,IPC_CREAT|0666);

printf("message queue is created \n");


if(qid==-1)

printf("message queue is not created \n");

exit(1);

printf("the value of qid is %d",qid);

strcpy(message.mtext,"LIKITH");

message.mtype=1;

len=strlen(message.mtext);

pid=fork();

printf("\n the value of pid is %d \n",pid);

if(pid==0)

msgsnd(qid,&message,len,IPC_NOWAIT);

printf("\n Message is sent \n");

if(pid>0)

msgrcv(qid,&message,strlen(message.mtext),0,IPC_NOWAIT|MSG_NOERROR);

printf("\n Message is %s \n",message.mtext);

printf("message is received \n");

if(pid=-1)

printf("error in creating a child \n");


exit(1);

FIFO

#include<stdio.h>

#include<conio.h>

int fr[3];

void main()

void display();

int i,j,page[12]={2,3,2,1,5,2,4,5,3,2,5,2};

int flag1=0,flag2=0,pf=0,frsize=3,top=0;

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

fr[i]=-1;

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

flag1=0;

flag2=0;

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

if(fr[i]==page[j])
{

flag1=1;flag2=1;

break;

if(flag1==0)

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

if(fr[i]==-1)

fr[i]=page[j];flag2=1;

break;

if(flag2==0)

fr[top]=page[j];

top++;

pf++;

if(top>=frsize)

top=0;

display();
}

printf("Number of page faults:%d",pf+frsize);

getch();

void display()

int i;

printf("\n");

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

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

PIPES

#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

#include<sys/wait.h>

int main()

int fd[2],child;

char a[10];

printf("\n Enter the string to enter into the pipe");


scanf("%s",&a);

pipe(fd);

child=fork();

if(! child)

close(fd[0]);

write(fd[1],a,5);

wait(0);

else

close(fd[1]);

read(fd[0],a,5);

printf("\n\n The string retrieved from the pipe is %s",a);

return 0;

PAGING

#include<stdio.h>

#include<math.h>

int main()

{
int size,m,n,pgno,pagetable[3]={5,6,7},i,j,frameno;

double m1;

int ra=0,ofs;

printf("Enter process size (in KB of max 12KB):");/*reading memeory size*/

scanf("%d",&size);

m1=size/4;

n=ceil(m1);

printf("Total No. of pages: %d",n);

printf("\nEnter relative address (in hexadecimal notation eg.0XRA) \n");

//printf(“The length of relative Address is : 16 bits \n\n The size of offset is :12 bits\n”);

scanf("%d",&ra);

pgno=ra/1000; /*calculating physical address*/ofs=ra%1000;

printf("page no=%d\n",pgno);

printf("page table");

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

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

frameno=pagetable[pgno];

printf("\n Equivalent physical address : %d%d",frameno,ofs);

return 0;

SEGMENTATION

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

int 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 segment 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 bsegmentation 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");

deadlock_avoidance

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int alloc[10][10],max[10][10];

int avail[10],work[10],total[10];

int i,j,k,n,need[10][10];

int m;

int count=0,c=0;

char finish[10];

printf("Enter the no. of processes and resources:");

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

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

finish[i]='n';

printf("Enter the claim matrix:\n");

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

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

printf("Enter the allocation matrix:\n");

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

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

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

printf("Resource vector:");

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

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

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

avail[i]=0;

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

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

avail[j]+=alloc[i][j];

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

work[i]=avail[i];

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

work[j]=total[j]-work[j];

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

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

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

A:

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

c=0;

for(j=0;j<m;j++)
if((need[i][j]<=work[j])&&(finish[i]=='n'))c++;

if(c==m)

printf("All the resources can be allocated to Process %d",i+1);

printf("\n\nAvailable resources are:");

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

work[k]+=alloc[i][k];

printf("%4d",work[k]);

printf("\n");

finish[i]='y';

printf("\nProcess %d executed?:%c\n",i+1,finish[i]);

count++;

if(count!=n)

goto A;

else

printf("\nSystem is in safemode");

printf("\n The given state is safe state");

getch();

}
deadlock_prevention

#include<stdio.h>

#include<conio.h>

void main()

char job[10][10];

int time[10],avail,tem[10],temp[10];

int safe[10];

int ind=1,i,j,q,n,t;

printf("Enternoofjobs:");

scanf("%d",&n);

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

printf("Enter name and time: ");

scanf("%s%d",&job[i],&time[i]);

printf("Enter the available resources:");

scanf("%d",&avail);

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

temp[i]=time[i];

tem[i]=i;

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

if(temp[i]>temp[j])

t=temp[i];

temp[i]=temp[j];

temp[j]=t;

t=tem[i];

tem[i]=tem[j];

tem[j]=t;

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

q=tem[i];

if(time[q]<=avail)

safe[ind]=tem[i];

avail=avail-tem[q];

printf("%s",job[safe[ind]]);

ind++;

else

printf("Nosafesequence\n");
}

printf("Safe sequence is:");

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

printf("%s %d\n",job[safe[i]],time[safe[i]]);

getch();

producerconsumer

#include<stdio.h>

int mutex=1,full=0,empty=3,x=0;

void main()

int n;

void producer();

void consumer();

int wait(int);

int signal(int);

printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");

while(1)

printf("\nENTER YOUR CHOICE\n");

scanf("%d",&n);

switch(n)
{

case 1:

if( (mutex==1)&&(empty!=0) )

producer();

else printf("BUFFER IS FULL");

break;

case 2:

if((mutex==1)&&(full!=0))

consumer();

else printf("BUFFER IS EMPTY");

break;

case 3:

exit(0);

break;

int wait(int s)

return(--s);

int signal(int s)

return(++s);

}
void producer()

mutex=wait(mutex);

full=signal(full);

empty=wait(empty);

x++;

printf("\nproducer produces the item%d",x);

mutex=signal(mutex);

void consumer()

mutex=wait(mutex);

full=wait(full);

empty=signal(empty);

printf("\n consumer consumes item%d",x);

x--;

mutex=signal(mutex);

You might also like