Using File Locking void update_file(char* file_path, char* new_data) { FILE* fin_out = fopen(file_path, "w+"); if (fin_out) { /* lock the file for exclusive access. */ /* return immediatly if cannot obtain lock. */ if (flock(fileno(fin_out), LOCK_EX | LOCK_NB) == 0) { if (ftruncate(fileno(fin_out), 0) == 0) { rewind(fin_out); fprintf(fin_out, "%s", new_data); } } fclose(fin_out); } }