Google Ads

Monday, January 4, 2010

PIPES

INTERPROCESS COMMUNICATION USING
PIPES



#include “ sys/types.h “
#include “ sys/ipc.h “
#include “ errno.h “
#include “ fcntl.h “
#include “ stdio.h “
#include “ sys/stat.h “
#include “ unistd.h “
#include “ string.h “
#define MAXLINE 50
void client(int,int);
void server(int,int);

int main(void)
{

int pipe1[2],pipe2[2];
pid_t childpid;
pipe(pipe1);
pipe(pipe2);

if((childpid=fork())==0)
{
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0],pipe2[1]);
exit(0);
}

close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0],pipe1[1]);
waitpid(childpid,NULL,0);
exit(0);

}




void client(int readfd,int writefd)
{
size_t len;
size_t n;
char buf[MAXLINE];
printf("\nEnter Filename:");
fgets(buf,MAXLINE,stdin);
len=strlen(buf);
if(buf[len-1]=='\n')
len--;
write(writefd,buf,len);
while((n=read(readfd,buf,MAXLINE)) “ 0)
write(STDOUT_FILENO,buf,n);
}

void server(int readfd,int writefd)
{
int fd;
size_t n;
char buf[MAXLINE+1];
if((n=read(readfd,buf,MAXLINE))==0)
buf[n]='\0';

if((fd=open(buf,O_RDONLY)) “ 0)
{
snprintf(buf+n,sizeof(buf)-n,"Can't open %s :",strerror(errno));
n=strlen(buf);
write(writefd,buf,n);
}
else
{
while((n=read(fd,buf,MAXLINE)) “ 0)
write(writefd,buf,n);
printf("%s",buf);
close(fd);
}
}








OUTPUT

[user1@localhost user1]$ cc pipe.c
[user1@localhost user1]$ ./a.out

Enter Filename:pipes.c

#include “ stdio.h “
main()
{
printf("\nWELCOME\n");
}

[user1@localhost user1]$

Sunday, January 3, 2010

MESSAGE QUEUES

INTERPROCESS COMMUNICATION USING
MESSAGE QUEUES


#include “ stdlib.h “
#include “ sys/types.h ”
#include “ sys/ipc.h ”
#include “ stdio.h “
#include " unistd.h “
#define msgtxtlen 100
#define sizeofmsg sizeof(msg)
#define msglen strlen(msg.mtext)
struct msgbuf
{
long mtype;
char mtext[msgtxtlen];
}msg;

main()
{
int ch;
int msgqid,cont=1,ren;
printf("\n\tMessage Queue Manipulation");
while(cont)
{
printf("\n\t1.Create message queue");
printf("\n\t2.Add a message into the queue");
printf("\n\t3.Retrieve a message from the queue");
printf("\n\t4.Remove a message queue");
printf("\n\t5.Exit");
printf("\n\tEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
msgqid=msgget(0x25,IPC_CREAT0644);
printf("\n\tMessage queue is created");
break;



case 2:
printf("\n\tMessage sending:");
printf("\n\tEnter the message type:");
scanf("%d",&msg.mtype);
printf("\n\tEnter the message:");
scanf("%s",&msg.mtext);
ren=msgsnd(msgqid,&msg,sizeof(msg),0);
if(ren!=1)
printf("\n\tMessage posted successfully");
else
printf("\n\tSorry u made mistake");
break;

case 3:
printf("\n\tMessage receiving and removing from the queue");
printf("\n\tEnter the message type");
scanf("%d",&msg.mtype);
ren=msgrcv(msgqid,&msg,sizeof(msg),msg.mtype,0);
printf("\n\tMessage type :%d",msg.mtype);
printf("\n\tMessage length :%d",msglen);
printf("\n\tMessage content :%s",msg.mtext);
break;

case 4:
ren=msgctl(msgqid,IPC_RMID,NULL);
if(ren!=1)
printf("\n\tMessage queue was removed");
else
printf("\n\tSorry");
break;

case 5:
exit(0);
default:
printf("\n\tEnter the value choice\n");
break;
}
printf("\n\tDo u wish to continue. yes=1,no=0 :");
scanf("%d",&cont);
}
}

OUTPUT

[user1@localhost user1]$ cc msgq.c
[user1@localhost user1]$ ./a.out

Message Queue Manipulation

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:1

Message queue is created
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:2

Message sending:
Enter the message type:0

Enter the message:hello
Message posted successfully
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit


Enter the choice:3

Message receiving and removing from the queue
Enter the message type0

Message type :0
Message length :5
Message content :hello
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:4

Message queue was removed
Do u wish to continue. yes=1,no=0 :1

1.Create message queue
2.Add a message into the queue
3.Retrieve a message from the queue
4.Remove a message queue
5.Exit

Enter the choice:5

[user1@localhost user1]$