Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207080
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 824
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-12 21:40
个人简介

只有今天的埋头,才有明天的出头。

文章分类

全部博文(80)

文章存档

2014年(80)

我的朋友

分类: LINUX

2014-12-21 21:06:48

#include
#include
#include
#include
#include
#include
#include


#define FIFO "/tmp/myfifo"


int main(int argc,char **argv)
{
char buf_r[100];
int fd,nread;
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&(errno!=EEXIST))
{
printf("Can not creat fifo\n");
}
else
{
printf("Creat fifo success\nPreparing 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 yet\n");
}
}
else
{
printf("Read %s from FIFO\n",buf_r);
sleep(1);
}
}
pause();
ulink(FIFO);
}
++++++++++++++++++++++++++++++++++
#include
#include
#include
#include
#include
#include
#include


#define FIFO "/tmp/myfifo"


int main(int argc,char **argv)
{
int fd;
char buf_w[100];
int nwrite;
fd=open(FIFO,O_WRONLY|O_NONBLOCK,0);
if(fd==-1)
{
if(errno==ENXIO)
printf("open error:no reading process");
}
if(argc==1)
{
printf("Plese sent something\n");
}
strcpy(buf_w,argv[1]);
if((nwrite=write(fd,buf_w,100))==-1)
{
if(errno==EAGAIN)
printf("The FIFO has not been read yet.Please try latter.\n");
}
else
{
printf("write %s to the FIFO\n",buf_w);
}


}
++++++++++++++++++++++++++++++++++++++++++++
#include
#include
#include




int main()
{
int pipe_fd[2];
if(pipe(pipe_fd)<0)
{
printf("Pipe creat error\n");
exit(1);
}
else
{
printf("Pipe creat success\n");
}
close(pipe_fd[0]);
close(pipe_fd[1]);
}
+++++++++++++++++++++++++++++++++++
#include
#include
#include
#include
#include
#include


int main()
{
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char *p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));

if(pipe(pipe_fd)<0)
{
printf("Creat pipe error\n");
return 1;
}
if((pid=fork())==0)
{
close(pipe_fd[1]);
sleep(2);
if((r_num=read(pipe_fd[0],buf_r,100))>0)
{
printf("%d numbers read from pipe is %s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else
{
close(pipe_fd[0]);
if(write(pipe_fd[1],"Hello",5)!=-1)
{
printf("Pipe write1 success.\n");
}
if(write(pipe_fd[1]," pipe",5)!=-1)
{
printf("Pipe write2 success.\n");
}
close(pipe_fd[1]);
sleep(3);
exit(0);
}
return 0;
}
++++++++++++++++++++++++++++++++++++++
#include
#include
#include
#include


#define BUFSIZE 2048
int main()
{
int shmid;
char *shmadd;
if((shmid=shmget(IPC_PRIVATE,BUFSIZE,0666))<0)
{
perror("shmget:");
exit(1);
}
else
{
printf("Create share memory success,the shmid is:%d\n",shmid);
}
system("ipcs -m");
if((shmadd=shmat(shmid,0,0))<(char *)0)
{
peror("shmat:");
exit(1);
}
else
{
printf("Mat memory success,the share address is matted to %x\n",shmadd);
}
system("ipcs -m");
if((shmdt(shmadd))<0)
{
perror("shmdt:");
exit(1);
}
else
{
printf("Delete memory success\n");
}
system("ipcs -m");
exit(0);
}
+++++++++++++++++++++++++++++++
#include
#include
#include
#include
#include
#include
#include


#define BUFSIZE 512
struct message{
long msg_type;
char msg_text[BUFSIZE];
};
int main()
{
int qid;
key_t key;
int len;
struct message msg;
if((key=ftok(".",'a'))==-1)
{
perror("ftok:");
exit(1);
}
if((qid=msgget(key,IPC_CREAT|066))==-1)
{
perror("msgget:");
exit(1);
}
printf("Open queue %d\n",qid);
puts("Please enter the message to the queue:");
if((fgets((&msg)->msg_text,BUFSIZE,stdin))==NULL)
{
puts("no message.");
exit(1);
}
msg.msg_type=getpid();
len=strlen(msg.msg_text);
if((msgsnd(qid,&msg,len,0))<0)
{
perror("msgsnd:");
exit(1);
}
if(msgrcv(qid,&msg,BUFSIZE,0,0)<0)
{
perror("msgrcv");
exit(1);
}
printf("Message is %s\n",(&msg)->msg_text);
if((msgctl(qid,IPC_RMID,NULL))<0)
{
perror("msgctl:");
exit(1);
}
exit(0);
}


阅读(2443) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~