|
学了一学期的操作系统,从来没讲过具体代码的实现,现在开使做实验了,书上用C写的那些代码发现基本上完全看不懂。。。自己码下来编译还是有问题,特来求救……
学习的时候确实比较偷懒,各位大虾就看在我舍得这么辛苦得把书上的代码打出来的份上,教教我吧。
顺便:那些奇怪的pipe(),fork(),sleep()...在那些书上有介绍啊?
/*用pipe()创建一个 管道文件,然后用fork()创建 2个 生产 进程 和 两个消费进程,它们之间 通过 pipe()传递信息*/
#include"sys/types.h"
#include"sys/file.h"
#include"unistd.h" //以上 三个 头文件都没见过呢。。。
#include"stdio.h"
#include"stdlib.h"
char r_buf[4]; //原注释为 :读缓冲
char w_buf[4]; //原注释为 :写缓冲
int pipe_fd[2]; //我想应该是管道吧
pid_t pid1,pid2,pid3,pid4 //这里看不懂了,pid_t是 什么东西啊?
int producer(int id);
int consumer(int id);
int main(int argc,char *argv)
{
if(pipe(pipe_fd)<0) //pipe函数是做什么用的啊?学C 的时候没听过,查了上面几个头文件也都没看到。。
{
printf("pipe create error.\n");
exit(-1);
}
else
{
printf("pipe is created successfully. \n");
if((pid1=fork())==0) //fork()函数的作用也不大清楚。。。
producer(1);
if((pid2=fork())==0)
producer(2);
if((pid3=fork())==0)
consumer(1);
if((pid4=fork())==0)
consumer(1);
}
close(pipe_fd[0]); //原注释:需要加上这两句,否则会有读者或者写者永远等待。。不了解。。
close(pipe_fd[1]);
int i,pid,status;
for(i=0;i<4;i++)
pid=wait(&status); //不懂,刚定义的status没有赋初值吧,怎么就用上了呢?wait()函数的作用又在哪里能找到啊?
exit(0);
}
int producer(int id)
{
printf("producer %d is running! \n",id);
close(pipe_fd[0]); //消费者 使用管道0,生产者使用管道1?
int i=0;
for(i=1;i<10;i++) //从1到10是什么啊?
{
sleep(3); //等待3秒吗?
if(id==1)
strcpy(w_buf,"aaa\0");
else
strcpy(w_buf,"bbb\0");
if(write(pipe_fd[1],w_buf,4)==-1) //write()函数的作用是 ……?
printf("write to pipe error. \n");
}
close(pipe_fd[1]);
printf("producer %d is over! \n");
exit(id);
}
int consumer(int id)
{
close(pipe_fd[1]); //关闭生产者管道?
printf("consumer %d is running. \n",id);
if(id==1)
strcpy(w_buf,"ccc\0");
else
strcpy(w_buf,"ddd\0");
while(1)
{
sleep(1);
strcpy(r_buf,"eee\0");
if(read(pipe_fd[0],r_buf,4)==0)
break;
printf("consumer %d get %s,while the w_buf is %s \n",id,r_buf,w_buf);
}
close(pipe_fd[0]);
printf("consumer %d is over! \n",id);
exit(id);
}
总结起来有以下问题:
1。pipe(),fork(),close(),wait(),write(),sleep(),read()这些函数的作用,它们都是包含在那些头文件里的?
2。gcc fork.c -o fork之后的出现一堆错误,不懂哪里错了。去年C没怎么学好.....
fork.c:13: 错误: syntax error before ‘int’
fork.c: 在函数 ‘main’ 中:
fork.c:19: 错误: stray ‘\239’ in program
fork.c:19: 错误: stray ‘\189’ in program
fork.c:19: 错误: stray ‘\155’ in program
fork.c:23: 错误: stray ‘\239’ in program
fork.c:23: 错误: stray ‘\189’ in program
fork.c:23: 错误: stray ‘\157’ in program
fork.c:24: 错误: syntax error before ‘else’
fork.c: 在顶层:
fork.c:37: 错误: syntax error before ‘[’ token
fork.c:37: 警告: 数据定义时没有类型或存储类
fork.c:38: 错误: syntax error before ‘[’ token
fork.c:38: 警告: 数据定义时没有类型或存储类
fork.c:41: 错误: syntax error before ‘for’
fork.c:43: 错误: syntax error before numeric constant
fork.c:43: 错误: ‘exit’ 类型冲突
fork.c:43: 警告: 数据定义时没有类型或存储类
fork.c: 在函数 ‘producer’ 中:
fork.c:49: 错误: stray ‘\239’ in program
fork.c:49: 错误: stray ‘\188’ in program
fork.c:49: 错误: stray ‘\155’ in program
fork.c:50: 错误: syntax error before ‘int’
fork.c:55: 警告: 内建函数 ‘strcpy’ 不兼容的隐式声明
fork.c: 在函数 ‘consumer’ 中:
fork.c:71: 警告: 内建函数 ‘strcpy’ 不兼容的隐式声明
3.书后的实验结果:
pipe is created successfully!
producer 1 is running!
producer 2 is running!
consumer 1 is running!
consumer 2 is running!
consumer 2 get aaa,while the w_buf is ddd
consumer 1 get bbb,while the w_buf is ccc
consumer 1 get aaa,while the w_buf is ccc
consumer 2 get bbb,while the w_buf is ddd
consumer 2 get aaa,while the w_buf is ddd
consumer 1 get bbb,while the w_buf is ccc
consumer 1 get aaa,while the w_buf is ccc
consumer 2 get bbb,while the w_buf is ddd
consumer 2 get aaa,while the w_buf is ddd
consumer 1 get bbb,while the w_buf is ccc
consumer 1 get aaa,while the w_buf is ccc
consumer 2 get bbb,while the w_buf is ddd
consumer 2 get aaa,while the w_buf is ddd
consumer 1 get bbb,while the w_buf is ccc
consumer 1 get aaa,while the w_buf is ccc
consumer 2 get bbb,while the w_buf is ddd
producer 1 is over!
producer 2 is over!
consumer 2 get aaa,while the w_buf is ddd
consumer 1 get bbb,while the w_buf is ccc
consumer 2 is over!
consumer 1 is over!
消费者的输出是printf("consumer %d get %s,while the w_buf is %s \n",id,r_buf,w_buf);
里面的r_buf在程序里就只有一个赋值语句
strcpy(r_buf,"eee\0");
将其赋值为eee吧,怎么eee从来没有在结果里出现过?反而都是些aaa,bbb的。。。 |
|