include include stdio h stdlib h errno h
#include #include <stdio. h> <stdlib. h> <errno. h> <fcntl. h> <unistd. h> int main(int argc, char *argv[]) { /* l_type l_whence l_start l_len l_pid struct flock fl = { F_WRLCK, SEEK_SET, 0, int fd; FILE LOCK */ 0, fl. l_pid = getpid(); if (argc > 1) fl. l_type = F_RDLCK; if ((fd = open("lockdemo. 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 lockn"); printf("Press <RETURN> to release lock: "); getchar(); fl. l_type = F_UNLCK; /* set to unlock same region */ if (fcntl(fd, F_SETLK, &fl) == -1) { perror("fcntl"); exit(1); } printf("Unlocked. n"); close(fd); } 0 }; 1
FIFO : speak. c #include #include <stdio. h> <stdlib. h> <errno. h> <string. h> <fcntl. h> <sys/types. h> <sys/stat. h> <unistd. h> #define FIFO_NAME "american_maid" main() { char s[300]; int num, fd; /* don't forget to error check this stuff!! */ mknod(FIFO_NAME, S_IFIFO | 0666, 0); printf("waiting for readers. . . n"); fd = open(FIFO_NAME, O_WRONLY); printf("got a reader--type some stuffn"); while (gets(s), !feof(stdin)) { if ((num = write(fd, s, strlen(s))) == -1) perror("write"); else printf("speak: wrote %d bytesn", num); } } 2
FIFO : tick. c #include #include <stdio. h> <stdlib. h> <errno. h> <string. h> <fcntl. h> <sys/types. h> <sys/stat. h> <unistd. h> #define FIFO_NAME "american_maid" main() { char s[300]; int num, fd; /* don't forget to error check this stuff!! */ mknod(FIFO_NAME, S_IFIFO | 0666, 0); printf("waiting for writers. . . n"); fd = open(FIFO_NAME, O_RDONLY); printf("got a writer: n"); do { if ((num = read(fd, s, 300)) == -1) perror("read"); else { s[num] = '