プロセス グループに応じて書き込み用にファイルをロックする必要があることを正しく理解していれば、この場合は fcntl と gpgrp を組み合わせることができます。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#define ALLOWED_GROUP 500
int main(int argc, char *argv[])
{
struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
int fd;
fl.l_pid = getpid();
if (argc > 1 || getpgrp() != ALLOWED_GROUP)
fl.l_type = F_RDLCK;
if ((fd = open("demo.c", O_RDWR)) == -1) {
perror("open");
exit(1);
}
printf("Press <RETURN> to try to get lock: ");
getchar();
printf("Trying to get lock...");
if (fcntl(fd, F_SETLKW, &fl) == -1) {
perror("fcntl");
exit(1);
}
printf("got lock\n");
printf("Press <RETURN> to release lock: ");
getchar();
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
exit(1);
}
printf("Unlocked.\n");
close(fd);
return 0;
}