this Codings in word format

advertisement
1.
Develop Application using Inter-Process-Communication (Using shared memory, pipes or
message queues).
MESSAGE QUEUE :
Aim :
To write a C Program to implement interprocess communication using message Queues
Algorithm:
1)
Start the program
2)
Create a message queue using msgget system call
3)
If the message queue,is not created goto Step1
4)
Copy the string “Hai’ to mtext
5)
Create a child process
a)
If the Child process is successfully created then send the message otherwise go
to step 8
6)
If the parent process got the focus receive the message sent by the child
7)
Stop the Program
Result:
The program for inter process communication using message Queues
was implemented and hence verified.
MESSAGE QUEUE
#include<stdio.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>
int main()
{
int pid,len,qid;
struct
www.Technicalsymposium.com
{
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 %s”,qid);
strcpy(message.mtext,”priya”);
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);
}
}
OUTPUT :
Inter process communication using message queue
The message queue is created
The value of qid is 32769
The value of pid is 0
The value of qid is 32769
The value of pid is 8269
Message is Priya
Message is received
Message is sent
www.Technicalsymposium.com
PIPE
Aim :
To write a C Program to implement interprocess communication using Pipe
Algorithm:
1.
Start the program
2.
Create a pipe using pipe() system call
3.
Create a child process.If the child process is created successfully then write the message into the
queue otherwise goto step2
4.
Read the message from the pipe and display the message
5.
Stop the program
Result :
The program for inter process communication using Pipe was implemented and hence verified.
www.Technicalsymposium.com
#include<stdio.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;
}
OUTPUT:Enter the string to enter into pipe: LOKESH
The string retrieved from the pipe is LOKESH
www.Technicalsymposium.com
INTERPROCESS COMMUNICATION USING SHARED MEMORY
Aim
To write a program to implement a shared memory
Algorithm
1. Start the Program
2. Declare the size and data variables
3. Get the number of processes to be inserted
4. Get the value
5. Start and identify the process with process id
6. Make a common function to communicate with each process running in
shared memory
7. Make the variables and arguments to be a global while in communication
8. Write a separate routine for creating memory delete memory in separate region
9. Give permission that a process can kill shared memory
10. Display the values
11. Stop the program
Result
The program for creation of shared memory was implemented and hence verified.
www.Technicalsymposium.com
Program
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
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);
}
Output
First SHMID Succeded id=589836
Second SHMID failed
www.Technicalsymposium.com
Third SHMID Succeded id=589836
www.Technicalsymposium.com
Download