LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1740|回复: 6

[求助]操作系统课的实验代码……看不懂。。。。

[复制链接]
发表于 2006-12-17 22:18:31 | 显示全部楼层 |阅读模式
学了一学期的操作系统,从来没讲过具体代码的实现,现在开使做实验了,书上用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的。。。
发表于 2006-12-18 00:36:10 | 显示全部楼层
呃。都是很基础的东西!先好好看下书再说!
随便一本有关linux的书都会告诉你pipe,fork,write,read,等等的用法和函数原型!
基础都没学好!
回复 支持 反对

使用道具 举报

发表于 2006-12-18 10:31:36 | 显示全部楼层
int pipe(int filedes[2]);

pipe() creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by filedes. filedes[0] is for reading, filedes[1] is for writing.

int pipe_fd[2]; //我想应该是管道吧   你是对的!
pid_t是进程ID,实际上是个int型的!

pid1=fork()==0 创建一个子进程!fork函数的作用就是创建一子进程!对父进程fork返回父进程ID,对子进程fork返回0

write(pipe_fd[1],w_buf,4)==-1) //write()函数的作用是 ……?   --往pipe写
回复 支持 反对

使用道具 举报

发表于 2006-12-18 18:21:11 | 显示全部楼层
推荐楼主看一下Advanced Programming in the Unix Environment
回复 支持 反对

使用道具 举报

发表于 2006-12-19 17:25:44 | 显示全部楼层
函数都可以用man查到的
http://www.die.net/doc/linux/man/
这个网站也可以查
回复 支持 反对

使用道具 举报

发表于 2006-12-31 20:07:38 | 显示全部楼层
生产者消费者问题,怀念中...
你把中文注示都去掉吧,中文有时会有问题。
回复 支持 反对

使用道具 举报

发表于 2007-1-4 17:06:40 | 显示全部楼层
LZ估计是没去上过这门课。。。。
现在好想回去上课听听啊,可惜没机会了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表