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

Computer Networks Lab - 2: Name: D.K.Karthik Varma Roll No: 421132 Sec: A

The document discusses different methods of inter-process communication including FIFOs, message queues, and shared memory. Code examples are provided to demonstrate writing and reading from FIFOs, sending and receiving messages using message queues, and writing to and reading from shared memory.

Uploaded by

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

Computer Networks Lab - 2: Name: D.K.Karthik Varma Roll No: 421132 Sec: A

The document discusses different methods of inter-process communication including FIFOs, message queues, and shared memory. Code examples are provided to demonstrate writing and reading from FIFOs, sending and receiving messages using message queues, and writing to and reading from shared memory.

Uploaded by

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

Computer Networks lab -2

Name : D.K.Karthik Varma


Roll no: 421132
sec : A

Inter Process communication :

USING FIFO :
l
h

fifo read:

/*
fifo read
Program to read messages first:
*/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char str1[80], str2[80];
while (1)
{
fd = open(myfifo, O_RDONLY);
read(fd, str1,80);
printf("user1: %s\n",str1);
close(fd);
fd = open(myfifo, O_WRONLY);
fgets(str2, 80, stdin);
write(fd, str2, strlen(str2)+1);
close(fd);
}
return 0;
}

fifo write:

/*
FIFO :
Program to write messages first:
fifo write
*/

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
char arr1[80], arr2[80];
while (1)
{
fd = open(myfifo, O_WRONLY);
fgets(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:
USING Message Queue:
message queue write :

/*
Message Queue:
Program to send messages:
*/

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 80
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 : ");
fgets(message.mesg_text,MAX,stdin);
msgsnd(msgid, &message, sizeof(message), 0);
printf("Data send is : %s \n", message.mesg_text);
return 0;
}

message queue read :

/*Program to receive messages:

*/

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

y gi
USING Shared Memory:

l
write using shared memory :

/*
Shared Memory:
Program to store message in shared memory:

*/

#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 : ");
gets(str);
printf("Data written in memory: %s\n",str);
shmdt(str);
return 0;
}

read using shared memory:

/*Program to read message from shared memory:

*/

#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;
}
output:

You might also like