You are on page 1of 13

S.

no

Experiment

date

sign

DISTRIBUTED
SYSTEMS LAB
(IT-407)
Submitted by
Swati Garg
791/IT/13
IT-2

1) IMPLEMENT IPC USING PIPES


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define MSGLEN 64
int main(){
int fd[2];
pid_t pid;
int result;
//Creating a pipe
result = pipe (fd);
if (result < 0) {
//failure in creating a pipe
perror("pipe");
exit (1);
}
//Creating a child process
pid = fork();
if (pid < 0) {
//failure in creating a child
perror ("fork");
exit(2);
}
if (pid == 0) {
//Child process
char message[MSGLEN];
while(1) {
//Clearing the message
memset (message, 0, sizeof(message));
printf ("Enter a message: ");
scanf ("%s",message);
//Writing message to the pipe
write(fd[1], message, strlen(message));
}
exit (0);
}
else {
//Parent Process
char message[MSGLEN];
while (1) {
//Clearing the message buffer

memset (message, 0, sizeof(message));


//Reading message from the pipe
read (fd[0], message, sizeof(message));
printf("Message entered %s\n",message);
}
exit(0);
}
}
OUTPUT
root@ubuntu:~# gcc pipe1.c
root@ubuntu:~# ./a.out
24978 Enter a message: Hello World!
24977 Message entered Hello World!

2. IMPLEMENT IPC MESSAGE PASSING USING FIFO BUFFER.


Read File
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
int fd,fname;
mkfifo("fifo1",0600);
fd=open("fifo1",O_RDONLY);
int d = 0;
while(read(fd,&d,sizeof(int))!=0)
{
printf("%d\n",d);
usleep(10);
}
close(fd);
}
Write File
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
int fd,fd1;
fd=open("fifo1",O_WRONLY);
int data = 1;
while (1){
write(fd,&data,sizeof(int));
usleep(10);
data++;
}
//printf("File Content :%s",s1);
}

3. IMPLEMENT IPC MESSAGE PASSING USING QUEUES.


Read File
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}
typedef struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
key_t key;
struct msgbuf rcvbuffer;
key = 1234;
if ((msqid = msgget(key, 0666)) < 0)
die("msgget()");
//Receive an answer of message type 1.
if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)
die("msgrcv");
printf("%s\n", rcvbuffer.mtext);
exit(0);
}
Write File
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}

struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
key = 1234;
if ((msqid = msgget(key, msgflg )) < 0) //Get the message queue ID for the given key
die("msgget");
//Message Type
sbuf.mtype = 1;
printf("Enter a message to add to message queue : ");
scanf("%[^\n]",sbuf.mtext);
getchar();
buflen = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
die("msgsnd");
}
else
printf("Message Sent\n");
exit(0);
}

4. IMPLEMENT IPC MESSAGE PASSING USING SHARED MEMORY.


Read File
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
//Now read what the server put in the memory.
for (s = shm; *s != '\0'; s++)
putchar(*s);
putchar('\n');
/*
*Change the first character of the
*segment to '*', indicating we have read
*the segment.
*/
*shm = '*';
exit(0);
}
Write File
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE
void die(char *s)
{

27

perror(s);
exit(1);
}
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
/*
*
* Put some things into the memory for the
*
other process to read.
*
*/
s = shm;
/*writing into the memory*/
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
printf("writing in the memory is done\n");
exit(0);
}

5. IMPLEMENT CLIENT SERVER USING SOCKETS


//server chat
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#define MAX_BUFFER_SIZE 100
int main()
{
int sockid,newsockid;
int clientaddrsize,numbytes;
char sendbuffer[MAX_BUFFER_SIZE+1]="connection message\n";
char recvbuffer[MAX_BUFFER_SIZE+1];
struct sockaddr_in myaddr, clientaddr;
sockid=socket(AF_INET, SOCK_STREAM, 0);
if(sockid==-1)
{
perror("socket");
}
printf("socketid: %d\n",sockid);
//cout<<"sockid: "<<sockid<<endl;
memset(&myaddr,0,sizeof(myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(8554);
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sockid, (struct sockaddr *)&myaddr, sizeof(myaddr))==-1)
{
perror("bind");
}
if(listen(sockid,5)==-1)
{
perror("listen");
}
while(1)
{
clientaddrsize=sizeof(clientaddr);
newsockid=accept(sockid,(struct sockaddr *)&clientaddr, &clientaddrsize);
if(newsockid==-1)
{
perror("accept");
}
printf("\nnew socket id: %d\n",newsockid);
//int bufsize= strlen(sendbuffer);
printf("say bye to end conversation\n");
while(1)
{
gets(sendbuffer);
int bufsize= strlen(sendbuffer);

//printf("\nsend data: ");


if(send(newsockid,(void *) sendbuffer, bufsize, MSG_DONTWAIT)<0)
{
perror("send");
}
if(strcmp(sendbuffer,"bye")==0)
{
close(newsockid);
break;
}
numbytes=recv(newsockid, recvbuffer, MAX_BUFFER_SIZE,0);
recvbuffer[numbytes]='\0';
if(numbytes<0)
{
perror("recv");
}
printf("\n%s",recvbuffer);
if(strcmp(recvbuffer,"bye")==0)
{
close(newsockid);
break;
}
}
}
close(sockid);
return 0;
}

//client chat
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#define MAX_BUFFER_SIZE 100
int main()
{
int sockid,newsockid,numbytes;
char sendbuffer[MAX_BUFFER_SIZE+1];
char recvbuffer[MAX_BUFFER_SIZE+1];
int caddrsize;
struct sockaddr_in caddr;
sockid=socket(AF_INET, SOCK_STREAM, 0);
if(sockid==-1)
{
perror("socket");
}
printf("socketid: %d\n",sockid);
memset(&caddr,0,sizeof(caddr));

caddr.sin_family=AF_INET;
caddr.sin_port=htons(8554);
caddr.sin_addr.s_addr=inet_addr("127.0.0.1");
caddrsize=sizeof(caddr);
if(connect(sockid, (struct sockaddr *)&caddr, caddrsize)==-1)
{
perror("connect");
}
/*strcpy(buffer,"connection message");
if(send(sockid,(void *) buffer, strlen(buffer), MSG_DONTWAIT)<0)
{
perror("send");
}*/
while(1)
{
numbytes=recv(sockid, recvbuffer, MAX_BUFFER_SIZE,0);
recvbuffer[numbytes]='\0';
if(numbytes<0)
{
perror("recv");
}
printf("%s\n",recvbuffer);
if(strcmp(recvbuffer,"bye")==0)
{
close(sockid);
break;
}
//printf("send data: ");
gets(sendbuffer);
int bufsize= strlen(sendbuffer);
if(send(sockid,(void *) sendbuffer, bufsize, MSG_DONTWAIT)<0)
{
perror("send");
}
if(strcmp(sendbuffer,"bye")==0)
{
close(sockid);
break;
}
}
close(sockid);
return 0;
}

You might also like