简体   繁体   English

实施文件锁以复制文件

[英]Implementing file locks to make a copy a file

Develop a C program for file-copy where two processes work together to complete the task : Parent process receives source filename and destination filename from command line. 开发一个用于文件复制的C程序, 其中两个进程一起完成任务 :父进程从命令行接收源文件名和目标文件名。 It opens the source file in read mode. 它以读取模式打开源文件。

Use shared lock on the source file in both the processes. 在两个过程中都对源文件使用共享锁。 Use exclusive lock on the destination file. 在目标文件上使用排他锁。 Do read/write operations in 256 byte blocks. 进行256字节块的读/写操作。 You should lock as small portion of the file as possible at one time. 您应该一次锁定文件的尽可能小的一部分。

How do i got about when to check locks and when to put locks? 我如何获得何时检查锁和何时放置锁? I am not able to find a good resource which contains example. 我找不到包含示例的好的资源。 I tried reading it from "Beej's guide to IPC", but it doesnt have a complete example. 我尝试从“ Beej的IPC指南”中阅读它,但是它没有完整的示例。

I know that I have to use fcntl() but how and when? 我知道我必须使用fcntl(),但是如何以及何时使用? Pls someone give a pseudocode of the program.. 请有人给出该程序的伪代码。

See my answer How can I copy a file on unix using C on StackOverflow. 查看我的答案如何在StackOverflow 上使用CUNIX复制文件 It uses a rudimentary locking and read the comments that caf has mentioned by using lockf , there is a more robust way to do this using fcntl . 它使用基本锁定,并使用lockf阅读caf提到的注释,还有一种更强大的方法是使用fcntl进行此操作。 There is a detailed document about this on GNU's website here . 有关于这一个详细的文件,在GNU的网站在这里 Here is the code on the opengroup that demonstrates the usage of fcntl to do the locking. 这是opengroup上的代码 ,演示了如何使用fcntl进行锁定。

Hope this helps, Best regards, Tom. 希望这对您有所帮助,汤姆,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM