diff options
| author | chenzizhan <[email protected]> | 2023-11-10 17:12:13 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2023-11-10 17:12:13 +0800 |
| commit | 50bb3722cf846cb06e39c2b131112123bcbbbc55 (patch) | |
| tree | 789f13a65a11b23178c11b91fe10855b96dad47b /src/fieldstat_easy.c | |
| parent | 4d86da7c364d5d17b07045984573e380dc5c9a8a (diff) | |
fix: flock does not work with python
Diffstat (limited to 'src/fieldstat_easy.c')
| -rw-r--r-- | src/fieldstat_easy.c | 17 |
1 files changed, 6 insertions, 11 deletions
diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c index c4c0974..fdc643c 100644 --- a/src/fieldstat_easy.c +++ b/src/fieldstat_easy.c @@ -93,13 +93,9 @@ void *fs_easy_output_thread(void *arg) // return void * for pthread_create check if (ret == NULL) { ret = strdup("[]"); } - struct flock lock; - lock.l_type = F_WRLCK; - lock.l_start = 0; - lock.l_whence = SEEK_SET; - lock.l_len = 0; - if (fcntl(fileno(fs->output_fp), F_SETLKW, &lock) < 0) { - fprintf(stderr, "fs_easy_output_thread: fcntl failed to lock\n"); + + if (flock(fileno(fs->output_fp), LOCK_EX) == -1) { + perror("fs_easy_output_thread: Error locking file"); free(ret); continue; } @@ -108,10 +104,9 @@ void *fs_easy_output_thread(void *arg) // return void * for pthread_create check ftruncate(fileno(fs->output_fp), 0); rewind(fs->output_fp); fprintf(fs->output_fp, "%s\n", ret); - - lock.l_type = F_UNLCK; - if (fcntl(fileno(fs->output_fp), F_SETLK, &lock) < 0) { - fprintf(stderr, "fs_easy_output_thread: fcntl failed to release\n"); + fflush(fs->output_fp); + if (flock(fileno(fs->output_fp), LOCK_UN) == -1) { + perror("fs_easy_output_thread: Error unlocking file"); } free(ret); |
