全文预览

文件的读写与上锁

上传者:蓝天 |  格式:doc  |  页数:9 |  大小:155KB

文档介绍
need);Рif ((fd = open(fifo_file, O_RDWR)) < 0)Р{Рprintf("Function myfilecopy error in source_file:");Рreturn -1;Р}Р/* 为了模拟FIFO结构,对整个文件内容进行平行移动*/Р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(int argc ,char *argv[])Р{Рint customer_capacity = 10;Рif (argc > 1) /* 第一个参数指定需要消费的资源数目,默认值为10 */Р{Рsscanf(argv[1], "%d", &customer_capacity);Р}Рif (customer_capacity > 0)Р{Рcustom(customer_capacity);Р}Рexit(ESS);Р}Р4、实验结果Р此实验的运行结果如下所示。实验结果会和这两个进程运行的具体过程相关。Р终端一:Р# ./producer 1 15 /*生产周期为1S,需要生产的资源数为20个*/РWrite lock set by 11867РRelease lock by 11867Р……Р终端二:Р# ./customer 5 /*需要消费的资源数为5个*/РEnjoy:abcdeРWrite lock set by 11879РRelease lock by 11879

收藏

分享

举报
下载此文档