There is a program (Ubuntu 12.04 LTS, single core processor):
#include <stdio.h> #include <unistd.h> #include <signal.h> #include <fcntl.h> #include <sys/types.h> int main(){ mode_t mode = S_IRUSR | S_IWUSR; int i = 0, fd, pid; unsigned char pi1 = 0x33, pi2 = 0x34; if((fd = open("res", O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0){ perror("open error"); exit(1); } if((pid = fork()) < 0){ perror("fork error"); exit(1); } if(pid == 0) { if(write(fd, &pi2, 1) != 1){ perror("write error"); exit(1); } }else{ if(write(fd, &pi1, 1) != 1){ perror("write error"); exit(1); } } close(fd); return 0; }
The idea is to open the file for writing, and then go to fork. The position in which a record number will be reached for both processes. The strange thing is that if you run the program, then its output to the "res" file is not constant: I got angry then 34, then 4, then 3. The question is, why is such a conclusion? (In the end, if the position is divided, then the output should be either 34 or 43.).
In my suspicion, the process is interrupted in the write function when it finds a position for writing.
source share