views:

94

answers:

1

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. 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. 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.

I know that I have to use fcntl() but how and when? Pls someone give a pseudocode of the program..

+5  A: 

See my answer How can I copy a file on unix using C on StackOverflow. 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. There is a detailed document about this on GNU's website here. Here is the code on the opengroup that demonstrates the usage of fcntl to do the locking.

Hope this helps, Best regards, Tom.

tommieb75