Ejemplo n.º 1
0
int
main(int argc, char *argv[])
{
	int fd;
	time_t now;
	char *msg;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
		exit(1);
	}

	if ((fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1)
		oops(argv[1], 2);

	while (1) {
		time(&now);
		msg = ctime(&now);

		lock_operation(fd, F_WRLCK);	/* lock for writing */

		if (lseek(fd, 0, SEEK_SET) == -1)
			oops("lseek", 3);
		if (write(fd, msg, strlen(msg)) == -1)
			oops("write", 4);

		lock_operation(fd, F_UNLCK);	/* unlock file */
		sleep(1);
	}

	close(fd);
	return 0;
}
Ejemplo n.º 2
0
int main(int argc,char* argv[])
{
    int fd,nread;
    char buf[BUFLEN];
    if(argc!=2){
        fprintf(stderr,"usage:./file_tc filename\n");
        exit(1);
    }
    fd=open(argv[1],O_RDONLY);
    if(fd==-1) oops(argv[1],O_RDONLY);
    lock_operation(fd,F_RDLCK);
    while((nread=read(fd,buf,BUFLEN))>0){
        write(STDOUT_FILENO,buf,nread);
    }
    lock_operation(fd,F_UNLCK);
    close(fd);
}
Ejemplo n.º 3
0
/**
 * Releases advisory, exclusive lock on given file (FILE *).
 */
void File_UnLock(FILE *fp)
{
	lock_operation(fp, DO_UNLOCK);
}
Ejemplo n.º 4
0
/**
 * Takes advisory, exclusive lock on given file (FILE *).
 * Returns false if locking fails (e.g. another Hatari
 * instance has already file open for writing).
 */
bool File_Lock(FILE *fp)
{
	return lock_operation(fp, DO_LOCK);
}