Wednesday 13 March 2013


NAME:T.KRISHNA ANANTHI
REG.NO:1041733

INTERPROCESS COMMUNICATIONS USING SHARED MEMORY,
PIPE AND MESSAGE QUEUES.

1.Develop an application for getting a name in parent and convert it into uppercase in
child using shared memory.

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdio_ext.h>
int main()
{
int id,n,pid,i;
char *a,*b;
id=shmget(111,50,IPC_CREAT|00666);
pid=fork();
if(pid>0)
{
a=shmat(id,NULL,0);
printf("\nEnter the string...\t");
gets(a);
wait(NULL);
shmdt(a);
}
else
{
sleep(5);
b=shmat(id,NULL,0);
n=strlen(b);
printf(“\n\n”);
for(i=0;i<n;i++)
printf("%c",toupper(b[i]));
shmdt(b);
}
return 0;
}

[ananthi@ananthi ~]$ gcc -Wall 7a.c -o a
[ananthi@ananthi ~]$ ./a out
Enter the string... krishna ananthi
KRISHNA ANANTHI[ananthi@ananthi ~]$

2.Develop an echo client / server application using shared memory.

//SERVER SIDE

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdio_ext.h>
int main()
{
int id;
char *a;
id=shmget(111,50,IPC_CREAT|00666);
a=shmat(id,NULL,0);
printf("\nEnter the string in server side..\t");
scanf("%s",a);
printf("\n\nServer waits until client responds...\n\n");
while(a[0]!='$');
printf("\n\nCLIENT::%s",a);
printf("\n\nServer task completed....\n");
return 0;
}

SAMPLE INPUT AND OUTPUT:

[ananthi@ananthi ~]$ gcc -Wall 7bs.c -o a
[ananthi@ananthi ~]$ ./a out

Enter the string in server side.. Serverbusy.....
Server waits until client responds...
CLIENT::$client goes to offline..
Server task completed....
[ananthi@ananthi ~]$

//CLIENT SIDE

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdio_ext.h>
int main()
{
int id,n;
char *a;
id=shmget(111,50,00666);
a=shmat(id,NULL,0);
n=strlen(a);
printf("\nDisplaying the string in client side wrote by server..\n\nSERVER :: ");
printf("%s",a);
strcpy(a,"$client goes to offline..\n\n");
return 0;
}

SAMPLE INPUT AND OUTPUT:

[ananthi@ananthi ~]$ gcc -Wall 7bc.c -o a
[ananthi@ananthi ~]$ ./a out

Displaying the string in client side wrote by server..

SERVER :: Serverbusy.....[ananthi@ananthi ~]$

3.Develop an application to get a number in child and find the reverse of that number in
parent using pipe.

#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdio_ext.h>
int main()
{
int pid,n,rev=0,fd[2],b;
pipe(fd);
pid=fork();
if(pid==0)
{
printf("\n\nEnter the number..\t");
scanf("%d",&n);
close(fd[0]);
write(fd[1],&n,1);
}
else
{
close(fd[1]);
read(fd[0],&n,1);
while(n>0)
{
rev=(rev*10)+n%10;
n=n/10;
}
printf("\n\nReversed number is %d\n",rev);
}
return 0;
}



SAMPLE INPUT AND OUTPUT:

[ananthi@ananthi ~]$ gcc -Wall 7c.c -o a
[ananthi@ananthi ~]$ ./a out
Enter the number of bits.. 5
Enter the number.. 12345
Reversed number is 54321
[ananthi@ananthi ~]$

4.Develop a client / server chat application using pipes

//SERVER
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/wait.h>
#include<fcntl.h>
int main()
{
char n[50];
int r,w;
mkfifo("ananthi",00666);
mkfifo("priya",00666);
close(r);
w=open("ananthi",O_WRONLY);
printf("\n\t\tSERVER ANANTHI:\n\n");
gets(n);
write(w,&n,sizeof(n));
while(1)
{
wait(0);
close(w);
r=open("priya",O_RDONLY);
read(r,&n,sizeof(n));
printf("\nCLIENT PRIYA:%s\n",n);
close(r);
w=open("ananthi",O_WRONLY);
printf("\nSERVER ANANTHI:");
gets(n);
write(w,&n,sizeof(n));
}
return 0;
}
SAMPLE INPUT AND OUTPUT:

[ananthi@ananthi ~]$ gcc -Wall 7ds.c -o s
[ananthi@ananthi ~]$ ./s

SERVER ANANTHI:

SERVER ANANTHI: hi mohana..
CLIENT PRIYA: hi krish..
SERVER ANANTHI: how r u ?
CLIENT PRIYA: fine.
SERVER ANANTHI: then wat doing?
CLIENT PRIYA: doing program
SERVER ANANTHI: wat pro??
CLIENT PRIYA: ipc..
SERVER ANANTHI: it is easy??
CLIENT PRIYA: easy only.. do it
SERVER ANANTHI: ok..i will do it..:)
CLIENT PRIYA: that's good..bye tc
SERVER ANANTHI: bye.. gud nite..:) sweet dreams..
[1]+ Stopped ./s
[ananthi@ananthi ~]$

//CLIENT
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/wait.h>
int main()
{
char n[50];
int r,w;
printf("\t\tCLIENT PRIYA:\n\n");
while(1)
{
wait(0);
close(w);
r=open("ananthi",O_RDONLY);
read(r,&n,sizeof(n));
printf("\nSERVER ANANTHI:\n");
puts(n);
close(r);
w=open("priya",O_WRONLY);
printf("\nCLIENT PRIYA:");
gets(n);
write(w,&n,sizeof(n));
}
return 0;
}

SAMPLE INPUT AND OUTPUT:
[ananthi@ananthi ~]$ gcc -Wall 7dc.c -o c
[ananthi@ananthi ~]$ ./c

CLIENT PRIYA:

SERVER ANANTHI: hi mohana..
CLIENT PRIYA: hi krish..
SERVER ANANTHI: how r u ?
CLIENT PRIYA: fine.
SERVER ANANTHI: then wat doing??
CLIENT PRIYA: doing program
SERVER ANANTHI: wat pro??
CLIENT PRIYA: ipc..
SERVER ANANTHI: it is easy??
CLIENT PRIYA: easy only.. do it
SERVER ANANTHI: ok..i will do it..:)
CLIENT PRIYA: that's good..bye tc
SERVER ANANTHI: bye.. gud nite..:) sweet dreams..
CLIENT PRIYA:
[1]+ Stopped ./c
[ananthi@ananthi ~]$

No comments:

Post a Comment