1、两个进程通过消息队列,来半双工通信
a.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>struct msgbuf{
long mytype;
char mtext[128];
};
int msgid;void* msg_w(void *arg)
{
struct msgbuf sendmsg;
sendmsg.mytype = 10;
while(1)
{
memset(sendmsg.mtext, 0, sizeof(sendmsg.mtext));
scanf("%s", sendmsg.mtext);
if(msgsnd(msgid, &sendmsg, sizeof(sendmsg.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
if(0 == strcmp(sendmsg.mtext, "quit"))
{
exit(0);
}
}
pthread_exit(NULL);
}void* msg_r(void *arg)
{
struct msgbuf recmsg;
ssize_t res = 0;
recmsg.mytype = 100;
while(1)
{
// memset(recmsg.mtext, 0, sizeof(recmsg.mtext));
if( res = msgrcv(msgid , &recmsg , sizeof(recmsg.mtext) , 100 , 0) < 0)
{
perror("msgrcv");
return NULL;
}
if(0 == strcmp(recmsg.mtext, "quit"))
{
exit(0);
}
printf("%s\n", recmsg.mtext);
bzero(recmsg.mtext,sizeof(recmsg.mtext));
}
pthread_exit(NULL);
}int main(int argc, const char *argv[])
{
key_t key = ftok("/home/ubuntu/", 2);
if(key < 0)
{
perror("ftok");
return -1;
}
msgid = msgget(key, IPC_CREAT|0664);
if(msgid < 0)
{
perror("msgget");
return -1;
}
printf("key = %#x, msgid = %d.\n", key, msgid);pthread_t pid1, pid2;
if(pthread_create(&pid1, NULL, msg_w, NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pid2, NULL, msg_r, NULL) != 0)
{
perror("pthread_create");
return -1;
}pthread_join(pid1, NULL);
pthread_join(pid2, NULL);msgctl(msgid, IPC_RMID, NULL);
return 0;
}
b.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>struct msgbuf{
long mytype;
char mtext[128];
};
int msgid;void* msg_w(void *arg)
{
struct msgbuf sendmsg;
sendmsg.mytype = 100;
while(1)
{
memset(sendmsg.mtext, 0, sizeof(sendmsg.mtext));
scanf("%s", sendmsg.mtext);
if(msgsnd(msgid, &sendmsg, sizeof(sendmsg.mtext), 0) < 0)
{
perror("msgsnd");
return NULL;
}
if(0 == strcmp(sendmsg.mtext, "quit"))
{
exit(0);
}
}
pthread_exit(NULL);
}void* msg_r(void *arg)
{
struct msgbuf recmsg;
ssize_t res = 0;
recmsg.mytype = 10;
while(1)
{
//memset(recmsg.mtext, 0, sizeof(recmsg.mtext));
if(msgrcv(msgid , &recmsg , sizeof(recmsg.mtext) , 10 , 0) < 0)
{
perror("msgrcv");
return NULL;
}
if(0 == strcmp(recmsg.mtext, "quit"))
{
exit(0);
}
printf("%s\n", recmsg.mtext);
bzero(recmsg.mtext,sizeof(recmsg.mtext));
}
pthread_exit(NULL);
}int main(int argc, const char *argv[])
{
key_t key = ftok("/home/ubuntu/", 2);
if(key < 0)
{
perror("ftok");
return -1;
}
msgid = msgget(key, IPC_CREAT|0664);
if(msgid < 0)
{
perror("msgget");
return -1;
}pthread_t pid1, pid2;
if(pthread_create(&pid1, NULL, msg_w, NULL) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&pid2, NULL, msg_r, NULL) != 0)
{
perror("pthread_create");
return -1;
}pthread_join(pid1, NULL);
pthread_join(pid2, NULL);msgctl(msgid, IPC_RMID, NULL);
return 0;
}
2、共享内存实现数组打印和倒置
a.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>int main(int argc, const char *argv[])
{
char str[] = "123456";key_t key = ftok("./", 2);
if(key < 0)
{
perror("ftok");
return -1;
}
int shmid = shmget(key, 32, IPC_CREAT|0777);
if(shmid < 0)
{
perror("shmget");
return -1;
}void *shmaddr = shmat(shmid, NULL, 0);
if((void *)1 == shmaddr)
{
perror("shmaddr");
return -1;
}int flag = 0;
char *pa = (char*)shmaddr+4;
*(int*)shmaddr = flag;
strcpy(pa,str);while(1)
{
if(0 == flag)
{
printf("%s\n", pa);
flag = 1;
*(int*)shmaddr = flag;
}
flag = *(int*)shmaddr;
sleep(1);
}shmdt(shmaddr);
return 0;
}
b.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>int main(int argc, const char *argv[])
{
char str[] = "123456";key_t key = ftok("./", 2);
if(key < 0)
{
perror("ftok");
return -1;
}
int shmid = shmget(key, 32, IPC_CREAT|0777);
if(shmid < 0)
{
perror("shmget");
return -1;
}void *shmaddr = shmat(shmid, NULL, 0);
if((void *)1 == shmaddr)
{
perror("shmaddr");
return -1;
}int flag = 0;
char *pa = (char*)shmaddr+4;
*(int*)shmaddr = flag;
int len = strlen(pa);printf("len = %d.\n", len);
while(1)
{
if(1 == flag)
{
int i=0, j=len-1;
while(i<j)
{
if(*(pa+i) != *(pa+j))
{
*(pa+i) ^= *(pa+j);
*(pa+j) ^= *(pa+i);
*(pa+i) ^= *(pa+j);
}
i++;
j--;
}
flag = 0;
*(int*)shmaddr = flag;
}
flag = *(int*)shmaddr;
}shmdt(shmaddr);
return 0;
}