Write () performs differently in two situations

So, I had to solve this exercise:

This exercise is intended to demonstrate why atomicity guaranteed by opening a flag file is O_APPENDnecessary. Write a program that takes up to three command line arguments:

$ atomic_append filename num-bytes [x] 

This file should open the specified file name (if necessary, create it) and add num-bytesbytes to the file, using write()to write the byte at a time. By default, the program should open the file with the flag O_APPEND, but if the third argument of the command line (x) is specified, the flag O_APPENDshould be omitted, and instead, the program must make a lseek(fd, 0, SEEK_END)call before each write(). Run two instances of this program at the same time with no argument xto write 1 million bytes to the same file:

$ atomic_append f1 1000000 & atomic_append f1 1000000 

Repeat the same steps, writing to another file, but this time specifying an argument x:

$ atomic_append f2 1000000 x & atomic_append f2 1000000 x 

List the file sizes f1 and f2 with ls –land explain the difference.

So here is what I wrote:

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

int main(int argc, char *argv[]) {
        int fd, flags, num_bytes;
        if (argc < 3 || strcmp(argv[1], "--help") == 0) {
                printf("Usage: %s filename num-bytes [x]\n", argv[0]);
                return 1;
        }
        num_bytes = atoi(argv[2]);
        if (argc == 4 && strcmp(argv[3], "x") == 0) {
                fd = open(argv[1], O_CREAT | O_WRONLY, 0666);
                if (fd == -1)
                        perror("open");
                while (num_bytes-- > 0) {
                        lseek(fd, 0, SEEK_END);
                        write(fd, "a", 1);
                }
                if (close(fd) == -1)
                        perror("close");
        }
        else {
                fd = open(argv[1], O_CREAT | O_APPEND | O_WRONLY, 0666);
                if (fd == -1)
                        perror("open");
                while(num_bytes-- > 0)
                        write(fd, "a", 1);
                if (close(fd) == -1)
                        perror("close");
        }
        return 0;
}

Now, after I launched it, as required:

abhinav@cr33p:~/System/5$ ./a.out f1 1000000 & ./a.out f1 1000000
[1] 4335
[1]+  Done                    ./a.out f1 1000000
abhinav@cr33p:~/System/5$ ./a.out f2 1000000 x & ./a.out f2 1000000 x
[1] 4352
[1]+  Done                    ./a.out f2 1000000 x
abhinav@cr33p:~/System/5$ ls f1 f2
f1  f2
abhinav@cr33p:~/System/5$ ls -l f*
-rw-rw-r-- 1 abhinav abhinav 2000000 Dec 10 16:23 f1
-rw-rw-r-- 1 abhinav abhinav 1000593 Dec 10 16:24 f2

, , , ? :

:

-rw------- 1 posborne posborne 1272426 2012-01-15 21:31 test2.txt
-rw------- 1 posborne posborne 2000000 2012-01-15 21:29 test.txt

test2.txt O_APPEND. test2.txt ( ), ( ).

, , . , ?

+4
1

, O_APPEND:

            while (num_bytes-- > 0) {
                    lseek(fd, 0, SEEK_END);
                    write(fd, "a", 1);

, lseek(). lseek() write().

, O_APPEND:

            while(num_bytes-- > 0)
                    write(fd, "a", 1);

write() ' , O_APPEND, , .

, O_APPEND - lseek(), write() .

+1

Source: https://habr.com/ru/post/1663395/


All Articles