msg1.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
struct msgbuf
{
long type;
char buf[128];
};
int id1;
void* run(void* arg)
{
struct msgbuf msg2;
while(1)
{
memset(&msg2,0,sizeof(msg2));
msgrcv(id1,&msg2,128,2,020000);
printf("the message is:%s\n",msg2.buf);
}
}
int main(int argc, const char *argv[])
{
int msgtype=atoi(argv[1]);
pthread_t id;
pthread_create(&id,0,run,0);
pthread_detach(id);
if(argc!=2){printf("wrong argument\n");}
key_t key=ftok("./ipc1",1);
if(key==-1){perror("ftok");return 1;}
id1=msgget(key,IPC_CREAT|0666);
if(id1==-1){perror("msgget");return 1;}
struct msgbuf msg1;
int size=0;
while(1)
{
memset(&msg1,0,sizeof(msg1));
msg1.type=msgtype;
printf("enter ur message:");
scanf("%128s",msg1.buf);
while(getchar()!=10);
size=strlen(msg1.buf);
msgsnd(id1,&msg1,size,0);
}
return 0;
}
msg2.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
struct msgbuf
{
long type;
char buf[128];
};
int id2;
void* run(void* arg)
{
struct msgbuf msg2;
int size=0;
while(1)
{
memset(&msg2,0,sizeof(msg2));
msg2.type=2;
printf("enter ur message:");
scanf("%128s",msg2.buf);
while(getchar()!=10);
size=strlen(msg2.buf);
msgsnd(id2,&msg2,size,0);
}
}
int main(int argc, const char *argv[])
{
pthread_t id;
pthread_create(&id,0,run,0);
pthread_detach(id);
int msgtype=atoi(argv[1]);
if(argc!=2){printf("wrong argument\n");}
key_t key=ftok("./ipc1",1);
if(key==-1){perror("ftok");return 1;}
id2=msgget(key,IPC_CREAT|0666);
if(id2==-1){perror("msgget");return 1;}
struct msgbuf msg1;
int size=0;
while(1)
{
memset(&msg1,0,sizeof(msg1));
msgrcv(id2,&msg1,128,msgtype,020000);
printf("the message is:%s\n",msg1.buf);
}
return 0;
}