1

This is an unresolved exam question of mine. Can two Unix processes simultaneous write to different positions in a single file?

  1. Yes, the two processes will have their own file table entries
  2. no, the shared i-node contains a single offset pointer
  3. only one process will have write privilege
  4. yes, but only if we operate using NFS
disasterkid
  • 6,948
  • 25
  • 94
  • 179
  • This is a homework/test question, isn't it? – PaulProgrammer Oct 29 '13 at 19:50
  • @PaulProgrammer yes tbh. – disasterkid Oct 29 '13 at 19:57
  • 2
    Here is a hint, three of the replies are incorrect. – jlliagre Oct 29 '13 at 20:33
  • @jlliagre why don't you go do something else? – disasterkid Oct 29 '13 at 20:54
  • That's quite an ungrateful comment. http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions Make a good faith attempt to solve the problem yourself first. If we can't see enough work on your part your question will likely be booed off the stage; it will be voted down and closed. – jlliagre Oct 29 '13 at 21:16
  • I need the answer to this question. Yes it is from a test I have failed twice because no one gives me the correct answer to some questions. If stack overflow is here to solve our problems, this is one problem I'm having about Unix file systems. Now if you want to vote down or boo off-stage or whatever geeky actions you guys do in these situations, go ahead and do it but don't ask for apologies for a question somebody needs to find answer to. Thanks. And please take a look at your answer above to see you were the one who started this and not me. Have a nice day! – disasterkid Oct 29 '13 at 21:25
  • 1
    If you already failed twice on that question, you should only have two remaining choices, don't you ? – jlliagre Oct 29 '13 at 21:59

3 Answers3

4
  • There is no file offset recorded in an inode so answer 2. is incorrect.
  • There is no documented reason for a process to have its access rights modified so 3. is incorrect.
  • NFS allows simultaneous access by processes on different hosts, the question here is for processes on the same host so NFS shouldn't make a difference.

Here is a shell script demonstrating the remaining answer 1. is correct:

# create a 10m file
dd if=/dev/zero of=/var/tmp/file bs=1024k count=10

# create two 1 MB files
cd /tmp
printf "aaaaaaaa" > aa
printf "bbbbbbbb" > bb
i=0
while [ $i -lt 17 ]; do
  cat aa aa > aa.new && mv aa.new aa
  cat bb bb > bb.new && mv bb.new bb
  i=$((i+1))
done

ls -lG /var/tmp/file /tmp/aa /tmp/bb

# launch 10 processes that will write at different locations in the same file.
# Uses dd notrunc option for the file not to be truncated
# Uses GNU dd fdatasync option for unbuffered writes

i=0
while [ $i -lt 5 ]; do
  (
  dd if=/tmp/aa of=/var/tmp/file conv=notrunc,fdatasync bs=1024k count=1 seek=$((i*2)) 2>/dev/null &
  dd if=/tmp/bb of=/var/tmp/file conv=notrunc,fdatasync bs=1024k count=1 seek=$((i*2+1)) 2>/dev/null &
  ) &
  i=$((i+1))
done

# Check concurrency
printf "\n%d processes are currently writing to /var/tmp/file\n" "$(fuser /var/tmp/file 2>/dev/null | wc -w)"

# Wait for write completion and check file contents
wait
printf "/var/tmp/file contains:\n"
od -c /var/tmp/file

Its output shows ten processes successfully and simultaneously write to the very same file:

-rw-r--r-- 1 jlliagre  1048576 oct.  30 08:25 /tmp/aa
-rw-r--r-- 1 jlliagre  1048576 oct.  30 08:25 /tmp/bb
-rw-r--r-- 1 jlliagre 10485760 oct.  30 08:25 /var/tmp/file

10 processes are currently writing to /var/tmp/file

/var/tmp/file contains:
0000000   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a
*
4000000   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b
*
10000000   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a
*
14000000   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b
*
20000000   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a
*
24000000   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b
*
30000000   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a
*
34000000   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b
*
40000000   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a   a
*
44000000   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b   b
*
50000000
jlliagre
  • 29,783
  • 6
  • 61
  • 72
2

Definition:

Yes, the two processes will have their own file table entries.

If the file is opened twice with the open function the two file descriptor is created.

Each file descriptor have seperate file status flags.

So the two file descriptor have a write permission file descriptor1 and file descriptor2 have initial position of point to first character to file.

If we specify some position to both descriptor and write in file it can be tested easily.

The content of file.txt

My name is Chandru. This is an empty file.

Coding for testing:

#include<stdio.h>                                                                           
#include<fcntl.h>
#include<stdlib.h>


 main()
 {
    int fd1, fd2;

 if((fd1=open("file.txt", O_WRONLY)) <0){
            perror("Error");
            exit(0);
    }
    if((fd2=open("file.txt", O_WRONLY)) < 0) {
            perror("Error");
            exit(0);
    }
    if(lseek(fd1,20,SEEK_SET) != 20)
    {
            printf("Cannot seek\n");
            exit(0);
    }
    if(write(fd1,"testing",7) != 7)
    {
            printf("Error write\n");
            exit(0);
    }
    if(lseek(fd2,10,SEEK_SET) != 10)
    {
            printf("Cannot seek\n");
            exit(0);
    }
    if(write(fd2,"condition",9) != 9)
    {
            printf("Error write\n");
            exit(0);
    }
  }

Output: After that my output is

My name isconditionitesting empty file.

Chandru
  • 1,306
  • 13
  • 21
  • 1
    While you show several file descriptors for the same file can simultaneously exist which is indeed the main point, you do not really answer the question which is about two different processes writing to the same file, not a single process. – jlliagre Oct 31 '13 at 16:00
1

Yes, they can of course, with the following caveats:

  • Depending on open() mode, one process can easily wipe the file contents
  • Depending on scheduling, the order of write operations is not deterministic
  • There is no mandatory locking (in general) - careful design calls for advisory locking.
  • If they write in the same area using buffered I/O results can be non-deterministic.
Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31