1、有名管道间通信
read进程代码:
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FIFO "/tmp/myfifo"
int main(int argc , char** argv)
{
char buf_r[100];
int fd;
int nread;
if((mkfifo(FIFO,O_CREAT | O_EXCL)<0)&& (errno!=EEXIST))
printf("cannot create fifo\n");
printf("prapare for reading bytes \n");
memset(buf_r,0,sizeof(buf_r));
fd = open(FIFO,O_RDONLY | O_NONBLOCK,0);
if(fd == -1)
{
perror("open");
exit(1);
}
while(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread = read(fd,buf_r,100)==-1))
{
if(errno== EAGAIN)
printf("no data \n");
}
else
printf("read from FIFO %s \n",buf_r);
sleep(1);
}
pause();
}
write 代码:
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FIFO_SERVER "/tmp/myfifo"
int main(int argc ,char** argv)
{
int fd;
char w_buf[100];
int nwrite;
fd = open(FIFO_SERVER,O_WRONLY | O_NONBLOCK,0);
if(argc == 1)
{
exit(-1);
}
strcpy(w_buf,argv[1]);
if((nwrite = write(fd,w_buf,100))==-1)
{
if(errno == EAGAIN)
printf("the fifo is not read\n");
}
else
printf("write %s to the fifo\n",w_buf);
return 0;
}
2、信号量通信
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
void my_func(int sign_no)
{
if(sign_no == SIGINT)
printf("I have get SIGINT\n");
else if(sign_no == SIGQUIT)
printf("I have get SIGQUIT\n");
}
void main()
{
printf("Waiting for signal SIGINT or SIGQUIT\n");
signal(SIGINT,my_func);
signal(SIGQUIT,my_func);
pause();
exit(0);
}
mysignal.c
运行该文件,在另一个终端中执行
kill -s SIGQUIT 'pid'
这里的pid为mysignal的进程号
通过kill向进程mysignal发送信号。mysignal得到信号后,打印相应的信息。
3、共享内存通信:
共享内存是被多个进程共享的一部分物理内存。共享内存是进程间共享数据的一种最快的方法,一个进程向共享内存区域写入了数据,共享这个区域的所有进程就可以立刻看到其中的内容
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int argc , char**argv)
{
int shmid;
char *p_addr,*c_addr;
if(argc != 2)
{
fprintf(stderr,"Usage:%s \n \a",argv[0]);
exit(1);
}
//创建共享内存对象
if((shmid = shmget(IPC_PRIVATE,1024,IPC_CREAT)) == -1)
{
fprintf("create Share Memory Error: %s\n",strerror(errno));
exit(1);
}
//create Process
if(0 !=fork()) //father Process
{ //把共享内存区对象映射到调用进程的地址空间
p_addr = shmat(shmid,0,0);
memset(p_addr,'\0',1024);
strncpy(p_addr,argv[1],1024);
wait(NULL);
shmdt(p_addr);//断开共享内存连接
shmctl(shmid,IPC_RMID,NULL);//IPC_RMID:删除这片共享内存
exit(0);
}
else //child Process
{
<span style="white-space:pre"> </span>sleep(1);
c_addr = shmat(shmid,0,0);
printf("Client get %s\n",c_addr);
<span style="white-space:pre"> </span>shmdt(c_addr);//断开共享内存连接
exit(0);
}
}
4、消息队列
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
struct msg_buf
{
int mtype;
char data[255];
};
int main()
{
key_t key;
int msgid;
int ret;
struct msg_buf msgbuf;
key = ftok("/tmp/1",'a');//创建key
printf("key= [%x]\n",key);
msgid = msgget(key,IPC_CREAT|0666);//创建一个新队列或打开一个存在的队列
if(msgid==-1)
{
printf("create error \n");
}
msgbuf.mtype = 10;
strcpy(msgbuf.data,"test haha");
ret=msgsnd(msgid,&msgbuf,sizeof(msgbuf.data),IPC_NOWAIT);//数据放到消息队列中
if(ret==-1)
{
printf("send error\n");
return -1;
}
memset(&msgbuf,0,sizeof(msgbuf));
ret=msgrcv(msgid,&msgbuf,sizeof(msgbuf.data),10,IPC_NOWAIT);//从消息队列中接收数据
if(ret==-1)
{
printf("receive error \n");
return -1;
}
printf("receive : %s\n",msgbuf.data);
return 0;
}
5、信号量