实验1.2

Mylock.h

 

#include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

#include<fcntl.h>

#include<sys/stat.h>

 

/*structmyflock

{

   short l_type;

   off_t l_start;

   short l_whence;

   off_t l_len;

   pid_t l_pid;

};*/

 

intlock_set(int fd,int type)

{

   struct flock old_lock,lock;

   lock.l_whence = SEEK_SET;

   lock.l_start = 0;

   lock.l_len = 0;

   lock.l_type = type;

   lock.l_pid = -1;

 

   fcntl(fd,F_GETLK,&lock);

   if(lock.l_type != F_UNLCK)

   {

      if(lock.l_type == F_RDLCK)

      {

          printf("Read lock already set by%d\n",lock.l_pid);

      }

      else if(lock.l_type == F_WRLCK)

      {

          printf("Write lock already setby %d\n",lock.l_pid);

      }

   }

 

lock.l_type =type;

 

if((fcntl(fd,F_SETLKW,&lock))< 0)

{

   printf("Lock failed:type =%d\n",lock.l_type);

   return 1;

}

 

switch(lock.l_type)

{

   case F_RDLCK:

   {

      printf("Read lock set by%d\n",getpid());

   }

   break;

   case F_WRLCK:

   {

      printf("Write lock set by%d\n",getpid());

   }

   break;

   case F_UNLCK:

   {

      printf("Release lock by%d\n",getpid());

      return 1;

   }

   break;

   default:

   break;

}

   return 0;

}

 

Producer.c

#include<stdio.h>

#include<unistd.h>

#include<stdlib.h>

#include<string.h>

#include<fcntl.h>

#include"mylock.h"

 

#defineMAXLEN  10

 

#defineALPHABET      1

#defineALPHABET_START        'a'

#defineCOUNT_OF_ALPHABET       26

 

#define DIGIT       2

#defineDIGIT_START         '0'

#defineCOUNT_OF_DIGIT 10

 

#defineSIGN_TYPE ALPHABET

const char*fifo_file="./myfifo";

charbuff[MAXLEN];

 

intproduct(void)

{

       int fd;

       unsigned int sign_type,sign_start,sign_count,size;

       static unsigned int counter = 0;

 

       if((fd=open(fifo_file,O_CREAT|O_RDWR|O_APPEND,0644))< 0)

       {

              printf("Open fifo fileerror\n");

              exit(1);

       }

       sign_type=SIGN_TYPE;

       switch(sign_type)

       {

              case ALPHABET:

              {

                     sign_start=ALPHABET_START;

                     sign_count=COUNT_OF_ALPHABET;

              }

              break;

              case DIGIT:

              {

                     sign_start=DIGIT_START;

                     sign_count=COUNT_OF_DIGIT;

              }

              break;

              default:

              {

                     return -1;

              }

       }

       sprintf(buff,"%c",(sign_start+counter));

       counter=(counter+1)%sign_count;

 

       lock_set(fd,F_WRLCK);

       if((size=write(fd,buff,strlen(buff)))<0)

       {

              printf("Producer:writeerror\n");

              return -1;

       }

       lock_set(fd,F_UNLCK);

       close(fd);

       return 0;

}

 

int main(intargc,char *argv[])

{

       int time_step=1;

       int time_life=10;

       if(argc>1)

       {

              sscanf(argv[1],"%d",&time_step);

       }

       if(argc>2)

       {

              sscanf(argv[2],"%d",&time_life);

       }

       while(time_life--)

       {

              if(product()<0)

              { break;}

              sleep(time_step);

       }

       exit(EXIT_SUCCESS);

}

 

 

Customer.c

#include<stdio.h>

#include<unistd.h>

#include<stdlib.h>

#include<fcntl.h>

#include"mylock.h"

 

#defineMAX_FILE_SIZE 100*1024*1024

 

const char*fifo_file = "./myfifo";

const char*tmp_file = "./tmp";

 

intcustoming(const char *myfifo,int need)

{

       int fd;

       char buff;

       int counter = 0;

 

       if((fd=open(myfifo,O_RDONLY)) < 0)

       {

              printf("Function customingerror\n");

              return -1;

       }

      

       printf("Enjoy:");

       lseek(fd,SEEK_SET,0);

       while(counter < need)

       {

              while((read(fd,&buff,1)==1)&& (counter < need))

              {

                     fputc(buff,stdout);

                     counter++;

              }

       }

       fputs("\n",stdout);

       close(fd);

       return 0;

}

 

intmyfilecopy(const char *sour_file,const char *dest_file,int offset,int count,intcopy_mode)

{

       int in_file,out_file;

       int counter = 0;

       char buff_unit;

       if((in_file =open(sour_file,O_RDONLY|O_NONBLOCK)) < 0)

       {

              printf("Function myfilecopyerror in source file\n");

              return -1;

       }

 

       if((out_file =open(dest_file,O_CREAT|O_RDWR|O_TRUNC| \

       O_NONBLOCK,0664)) < 0)

       {

              printf("Function myfilecopyerror in destination file:");

              return -1;

       }

 

       lseek(in_file,offset,SEEK_SET);

       while((read(in_file,&buff_unit,1) ==1) && (counter < count))

       {

              write(out_file,&buff_unit,1);

              counter++;

       }

 

       close(in_file);

       close(out_file);

       return 0;

}

 

intcustom(int need)

{

       int fd;

       customing(fifo_file,need);

      

       if((fd=open(fifo_file,O_RDWR)) < 0)

       {

              printf("Function myfilecopyerror in source_file");

              return -1;

       }

 

       lock_set(fd,F_WRLCK);

       myfilecopy(fifo_file,tmp_file,need,MAX_FILE_SIZE,0);

       myfilecopy(tmp_file,fifo_file,0,MAX_FILE_SIZE,0);

       lock_set(fd,F_UNLCK);

       unlink(tmp_file);

       close(fd);

       return 0;

}

 

int main(intargc,char *argv[])

{

       int customer_capacity = 10;

 

       if(argc > 1)

       {

              sscanf(argv[1],"%d",&customer_capacity);

       }

       if(customer_capacity > 0)

       {

              custom(customer_capacity);

       }

       exit(EXIT_SUCCESS);

}

(1)创建生产者和消费者线程 在Windows2000环境下,创建一个控制台进程,在此进程中创建n个线程来模拟生产者或者消费者。这些线程的信息由本程序定义的“测试用例文件”中予以指定。 该文件的格式和含义如下: 3 1 P 3 2 P 4 3 C 4 1 4 P 2 5 C 3 1 2 4 第一行说明程序中设置几个临界区,其余每行分别描述了一个生产者或者消费者线程的信息。每一行的各字段间用Tab键隔开。不管是消费者还是生产者,都有一个对应的线程号,即每一行开始字段那个整数。第二个字段用字母P或者C区分是生产者还是消费者。第三个字段表示在进入相应线程后,在进行生产和消费动作前的休眠时间,以秒计时;这样做的目的是可以通过调整这一列参数,控制开始进行生产和消费动作的时间。如果是代表生产者,则该行只有三个字段。如果代表消费者,则该行后边还有若干字段,代表要求消费的产品所对应的生产者的线程号。所以务必确认这些对应的线程号存在并且该线程代表一个生产者。 (2)生产和消费的规则 在按照上述要求创建线程进行相应的读写操作时,还需要符合以下要求: ①共享缓冲区存在空闲空间时,生产者即可使用共享缓冲区。 ②从上边的测试数据文件例子可以看出,某一生产者生产一个产品后,可能不止一个消费者,或者一个消费者多次地请求消费该产品。此时,只有当所有的消费需求都被满足以后,该产品所在的共享缓冲区才可以被释放,并作为空闲空间允许新的生产者使用。 ③每个消费者线程的各个消费需求之间存在先后顺序。例如上述测试用例文件包含一行信息“5 C 3 l 2 4”,可知这代表一个消费者线程,该线程请求消费1,2,4号生产者线程生产的产品。而这种消费是有严格顺序的,消费1号线程产品的请求得到满足后才能继续往下请求2号生产者线程的产品。 ④要求在每个线程发出读写操作申请、开始读写操作和结束读写操作时分别显示提示信息。 (3)相关基础知识 本实验所使用的生产者和消费者模型具有如下特点: 本实验的多个缓冲区不是环形循环的,也不要求按顺序访问。生产者可以把产品放到目前某一个空缓冲区中。 消费者只消费指定生产者的产品。 在测试用例文件中指定了所有的生产和消费的需求,只有当共享缓冲区的数据满足了所有关于它的消费需求后,此共享缓冲区才可以作为空闲空间允许新的生产者使用。 本实验在为生产者分配缓冲区时各生产者间必须互斥,此后各个生产者的具体生产活动可以并发。而消费者之间只有在对同一产品进行消费时才需要互斥,同时它们在消费过程结束时需要判断该消费对象是否已经消费完毕并清除该产品。 Windows用来实现同步和互斥的实体。在Windows中,常见的同步对象有:信号量(Semaphore)、互斥量(Mutex)、临界段(CriticalSection)等。使用这些对象都分为三个步骤,一是创建或者初始化:接着请求该同步对象,随即进入临界区,这一步对应于互斥量的上锁;最后释放该同步对象,这对应于互斥量的解锁。这些同步对象在一个线程中创建,在其他线程中都可以使用,从而实现同步互斥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值