linux进程间的通信实验,linux实验进程之间的通信.doc

院 系: 计算机学院

实验课程:linux内核设计与实现

实验项目:进程之间的通信

指导老师:冯刚

开课时间: 2010 ~ 2011 年度第 2 学期

专 业:计算机科学与技术(师范)

班 级:2班

华南师范大学教务处

一、实验名称

进程之间的通信

二、实验目的

为了理解和掌握UNIX和Linux进程通信系统调用的功能,这里给出了进程通信实现机制中使用的系统调用命令的格式和如何利用系统调用命令进行进程通信编程,以便通过学习,提高学生对进程通信系统调用的编程能力。

三、实验内容

编写一个有名管道程序。一个(客户)进程从键盘循环读一系列字符,将这些字符和发送者的pid发给服务器进程,让其统计输入的是字符还是数字,分别为多少个,完成后再向客户进程发回服务的结果,由客户进程输出。

源程序:

//client.c

#include

#include

#include

#include

#include

#include

#include

#include

#define m S_IRUSR|S_IWUSR

int main()

{

char buf[BUFSIZ];

int f1,f2,len,ret;

ret=mkfifo("fifo_c",m);//创建有名管道fifo_c

if(ret==-1)//若创建失败,输出提示

{

printf("can't creat\n");

return 1;

}

f1=open("fifo_c",O_WRONLY);//以只写方式打开fifo_c

if(f1==-1)//若打开失败,输出提示

{

printf("can't open\n");

return 1;

}

while((f2=open("fifo_s",O_RDONLY))==-1)//以只读方式打开fifo_s

sleep(1);

while(1)

{

printf("input a string:");

scanf("%s",buf);//键盘输入字符串

if(strcmp("quit",buf)==0)//若输入“quit",则退出客户端程序

{

close(f1);

unlink("fifo_c");

close(f2);

exit(0);

}

write(f1,buf,strlen(buf));//向管道fifo_c写数据

len=read(f2,buf,BUFSIZ);//从管道fifo_s读数据

if(len>0)

{

buf[len]='\0';

printf("server answer:\n num:%s\n",buf);

}

len=read(f2,buf,BUFSIZ);//从管道fifo_s读数据

if(len>0)

{

buf[len]='\0';

printf(" letter:%s\n",buf);

}

}

return 0;

}

//server.c

#include

#include

#include

#include

#include

#include

#include

#include

#define m S_IRUSR|S_IWUSR

//函数itos将整数v转化为字符数组s

void itos(int v,char *s)

{

int i=0,len;

char t;

if(v==0)

s[i++]='0';

while(v)

{

s[i++]=(v%10+'0');

v/=10;

}

s[i]='\0';

len=strlen(s);

for(i=0;i

{

t=s[i];

s[i]=s[len-i-1];

s[len-i-1]=t;

}

return;

}

int main()

{

char buf[BUFSIZ];

char buf1[BUFSIZ];

int f1,f2,len,ret,i,letter,num,c;

ret=mkfifo("fifo_s",m);;//创建有名管道fifo_s

if(ret==-1)//若创建失败,输出提示

{

printf("can't creat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值