// Demonstrate writing to a file with repeated writes. Often run in // simultaneous processes as in // // $> a.out file.txt 10 'AAA' & a.out file.txt 10 'BBB' & // // which will lead to either all AAA or all BBB messages in // file.txt. Alternatively run with a fifo/named pipe as in // // $> mkfifo some.fifo // $> a.out some.fifo 10 'AAA' & a.out some.fifo 10 'BBB' & // // Doing a cat some.fifo will complete both processes and show output // intermingled from both. #include #include #include #include #include #include #include #define MAXSIZE 1024 int main(int argc, char *argv[]){ if(argc < 4){ printf("usage: %s file iters message\n",argv[0]); exit(1); } char *fname = argv[1]; // name of file to write int iters = atoi(argv[2]); // number of iterations to write char *cmd_msg = argv[3]; // message to write each time char msg[MAXSIZE]; // buffer for message int fd = open(fname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); // create if needed for(int i=0; i