[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 Тексты программ
report1.c
  1 #include <sys/types.h>
  2 #include <unistd.h>
  3 #include <stdlib.h>
  4 #include <stdio.h>
  5 #include <fcntl.h>
  6 #include <errno.h>
  7 #include "employee.h"
  8 #define   MAXTRIES   5
  9 main(int argc, char *argv[])
 10 {
 11     struct flock lock;
 12     struct employee record;
 13     int fd, sum = 0, try = 0;
 14     if ((fd = open(argv[1], O_RDONLY)) == -1) {
 15         perror(argv[1]);
 16         exit(1);
 17     }
 18     lock.l_type = F_RDLCK;
 19     lock.l_whence = SEEK_SET;
 20     lock.l_start = 0;
 21     lock.l_len = 0; /* whole file address space */
 22     while (fcntl(fd, F_SETLK, &lock) == -1) {
 23         if ((errno == EACCES) || (errno == EAGAIN)) {
 24             if (try++ < MAXTRIES) {
 25                 sleep(1);
 26                 continue;
 27             }
 28             printf("%s busy -- try later\n",
 29                 argv[1]);
 30             exit(2);
 31         }
 32         perror(argv[1]); exit(3);
 33     }
 34     sum = 0;
 35     while (read(fd, &record, sizeof(record)) > 0) {
 36         printf("Employee: %s, Salary: %d\n",
 37             record.name, record.salary);
 38         sum += record.salary;
 39     }
 40     printf("\nTotal salary: %d\n", sum);
 41
 42     lock.l_type = F_UNLCK;    /* unlock file */
 43     fcntl(fd, F_SETLK, &lock);
 44     close(fd);
 45 }

update1.c
  1 #include <sys/types.h>
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <fcntl.h>
  5 #include <stdlib.h>
  6 #include <errno.h>
  7 #include "employee.h"
  8
  9 main(int argc, char *argv[])
 10 {
 11      struct flock lock;
 12      struct employee record;
 13      int fd, recnum;
 14      unsigned short uid;
 15      off_t position;
 16
 17      if ((fd = open(argv[1], O_RDWR)) == -1) {
 18       perror(argv[1]);
 19       exit(1);
 20      }
 21
 22      uid = getuid();
 23      for(;;) {
 24       printf("\nEnter record number: ");
 25       scanf("%d", &recnum);
 26       if (recnum <= 0)
 27            break;
 28       position = (recnum-1) * sizeof(record);
 29       lock.l_type = F_WRLCK;        /* lock record */
 30       lock.l_whence = SEEK_SET;
 31       lock.l_start = position;
 32       lock.l_len = sizeof(record);
 33       if (fcntl(fd, F_SETLKW, &lock) == -1) {
 34            perror(argv[1]);
 35            exit(2);
 36       }
 37
 38       lseek(fd, position, SEEK_SET);      /* read record */
 39       if (read(fd, &record, sizeof(record)) == 0) {
 40            printf("record %d not found\n", recnum);
 41            lock.l_type = F_UNLCK;
 42            fcntl(fd, F_SETLK, &lock);
 43            continue;
 44       }
 45       printf("Employee: %s, salary: %d\n",
 46            record.name, record.salary);
 47
 48       record.uid = uid;            /* update record */
 49       printf("Enter new salary: ");
 50       scanf("%d", &record.salary);
 51       lseek(fd, position, SEEK_SET);
 52       write(fd, &record, sizeof(record));
 53
 54       lock.l_type = F_UNLCK;        /* release record */
 55       fcntl(fd, F_SETLK, &lock);
 56      }
 57      close(fd);
 58 }
updatem.c
  1 #include <sys/mman.h>
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #include <fcntl.h>
  5 #include <sys/types.h>
  6 #include <unistd.h>
  7 #include <errno.h>
  8 #include "employee.h"
  9
 10 main(int argc, char *argv[])
 11 {
 12    struct flock lock;
 13    off_t size;
 14    struct employee *p;
 15    int position, fd, recnum;
 16    unsigned short uid;
 17
 18    if ((fd = open(argv[1], O_RDWR)) == -1) {
 19     perror(argv[1]);
 20     exit(1);
 21    }
 22
 23    size = lseek(fd, 0, SEEK_END);
 24    p = (struct employee *)mmap(0, size,
 25        PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
 26
 27    uid = getuid();
 28    for(;;) {
 29        printf("\nEnter record number: ");
 30        scanf("%d", &recnum);
 31        if (recnum <= 0)
 32        break;
 33
 34        position = recnum * sizeof(struct employee);
 35        if (position >= size) {
 36        printf("record %d not found\n", recnum);
 37        continue;
 38        }
 39
 40        lock.l_type = F_WRLCK;        /* lock record */
 41        lock.l_whence = SEEK_SET;
 42        lock.l_start = position;
 43        lock.l_len = sizeof(struct employee);
 44        if (fcntl(fd, F_SETLKW, &lock) == -1) {
 45         perror(argv[1]);
 46         exit(2);
 47        }
 48
 49        printf("Employee: %s, salary: %d\n",
 50         p[recnum].name, p[recnum].salary);
 51
 52        p[recnum].uid = uid;            /* update */
 53        printf("Enter new salary: ");
 54        scanf("%d", &p[recnum].salary);
 55        msync(p, size, MS_ASYNC);
 56
 57        lock.l_type = F_UNLCK;        /* release record */
 58        fcntl(fd, F_SETLK, &lock);
 59   }
 60   munmap(p, size);
 61   close(fd);
 62 }
update2.c
  1 #include <sys/types.h>
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <errno.h>
  7 #include "employee.h"
  8
  9 main(int argc, char *argv[])
 10 {
 11      struct flock lock;
 12      struct employee record;
 13      int fd, recnum;
 14      unsigned short uid;
 15      char ans[5];
 16      off_t position;
 17
 18      if ((fd = open(argv[1], O_RDWR)) == -1) {
 19       perror(argv[1]);
 20       exit(1);
 21      }
 22      uid = getuid();
 23      for(;;) {
 24       printf("\nEnter record number: ");
 25       scanf("%d", &recnum);
 26       if (recnum <= 0)
 27            break;
 28       position = (recnum-1) * sizeof(record);
 29       lock.l_type = F_RDLCK;          /* read lock */
 30       lock.l_whence = SEEK_SET;
 31       lock.l_start = position;
 32       lock.l_len = sizeof(record);
 33       if (fcntl(fd, F_SETLKW, &lock) == -1) {
 34            perror(argv[1]);
 35            exit(2);
 36       }
 37       lseek(fd, position, SEEK_SET);          /* read record */
 38       if (read(fd, &record, sizeof(record))
 39            == 0) {
 40            printf("record %d not found\n", recnum);
 41            lock.l_type = F_UNLCK;
 42            fcntl(fd, F_SETLK, &lock);
 43            continue;
 44       }
 45       printf("Employee: %s, salary: %d\n",
 46            record.name, record.salary);
 47
 48       printf("Do you want to update salary (y or n)? ");
 49       scanf("%s", ans);
 50       if (ans[0] != 'y') {
 51            lock.l_type = F_UNLCK;
 52            fcntl(fd, F_SETLK, &lock);
 53            continue;
 54       }
 55       lock.l_type = F_WRLCK;          /* write lock */
 56       if (fcntl(fd, F_SETLKW, &lock) == -1) {
 57            perror(argv[1]);
 58            exit(3);
 59       }
 60       record.uid = uid;          /* update record */
 61       printf("Enter new salary: ");
 62       scanf("%d", &record.salary);
 63       lseek(fd, position, SEEK_SET);
 64       write(fd, &record, sizeof(record));
 65
 66       lock.l_type = F_UNLCK; /* release record */
 67       fcntl(fd, F_SETLK, &lock);
 68      }
 69      close(fd);
 70 }
getlocks.c
  1 #include <sys/types.h>
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <errno.h>
  7 #include "employee.h"
  8
  9 main(int argc, char *argv[])
 10 {
 11     struct employee record;
 12     struct flock lock;
 13     off_t size;
 14     int fd;
 15
 16     if ((fd = open(argv[1], O_WRONLY)) == -1) {
 17         perror(argv[1]);
 18         exit(1);
 19     }
 20     size = lseek(fd, 0, SEEK_END);  /* size of file */
 21
 22     lock.l_whence = SEEK_SET;
 23     lock.l_len = sizeof(record);
 24     printf("File: %s\n", argv[1]);
 25     for (lock.l_start = 0; lock.l_start < size;
 26         lock.l_start += lock.l_len) {
 27         lock.l_type = F_WRLCK;
 28         fcntl(fd, F_GETLK, &lock);
 29         if (lock.l_type == F_UNLCK)     /* not locked */
 30             continue;
 31         printf("Pid: %ld", lock.l_pid);
 32         printf("\t%s lock",
 33             (lock.l_type == F_WRLCK)? "Write" : "Read");
 34         printf("\tRecord number: %ld",
 35             lock.l_start/sizeof(record));
 36         if (lock.l_len == 0) {     /* rest of file */
 37             lock.l_len = size - lock.l_start;
 38             lock.l_start = size;  /* terminate for loop */
 39             }
 40         printf("\tNumber of records: %ld\n",
 41             lock.l_len/sizeof(record));
 42     }
 43 }
report2.c
  1 #include <sys/types.h>
  2 #include <stdio.h>
  3 #include <fcntl.h>
  4 #include <unistd.h>
  5 #include <stdlib.h>
  6 #include <errno.h>
  7 #include "employee.h"
  8 #define   MAXTRIES   5
  9
 10 main(int argc, char *argv[])
 11 {
 12     struct employee record;
 13     int fd, sum = 0, try = 0;
 14
 15     if ((fd = open(argv[1], O_RDWR)) == -1) {
 16         perror(argv[1]);
 17         exit(1);
 18     }
 19     while (lockf(fd, F_TLOCK, 0) == -1) {
 20         if ((errno == EACCES) || (errno == EAGAIN)) {
 21             if (try++ < MAXTRIES) {
 22                 sleep(1);
 23                 continue;
 24             }
 25             printf("%s busy -- try later\n",
 26                 argv[1]);
 27             exit(2);
 28         }
 29         perror(argv[1]);
 30         exit(3);
 31     }
 32
 33     sum = 0;
 34     while (read(fd, &record, sizeof(record)) > 0) {
 35         printf("Employee: %s, Salary: %d\n",
 36             record.name, record.salary);
 37         sum += record.salary;
 38     }
 39     printf("\nTotal salary: %d\n", sum);
 40
 41     lseek(fd, 0, SEEK_SET);
 42     lockf(fd, F_ULOCK, 0);
 43     close(fd);
 44 }
update3.c
  1 #include <sys/types.h>
  2 #include <stdlib.h>
  3 #include <stdio.h>
  4 #include <unistd.h>
  5 #include <fcntl.h>
  6 #include "employee.h"
  7
  8 main(int argc, char *argv[])
  9 {
 10      struct employee record;
 11      unsigned short uid;
 12      int fd, recnum;
 13      off_t position;
 14
 15      if ((fd = open(argv[1], O_RDWR)) == -1) {
 16       perror(argv[1]);
 17       exit(1);
 18      }
 19
 20      uid = getuid();
 21      for(;;) {
 22       printf("\nEnter record number: ");
 23       scanf("%d", &recnum);
 24       if (recnum <= 0)
 25            break;
 26       position = (recnum-1) * sizeof(record);
 27
 28       lseek(fd, position, SEEK_SET);
 29       if (lockf(fd, F_LOCK, sizeof(record)) == -1) {
 30            perror(argv[1]);
 31            exit(2);
 32       }
 33       if (read(fd, &record, sizeof(record)) == 0) {
 34            printf("record %d not found\n", recnum);
 35            lockf(fd, F_ULOCK, sizeof(record));
 36            continue;
 37       }
 38       printf("Employee: %s, salary: %d\n",
 39            record.name, record.salary);
 40
 41       record.uid = uid;            /* update record */
 42       printf("Enter new salary: ");
 43       scanf("%d", &record.salary);
 44       lseek(fd, position, SEEK_SET);
 45       write(fd, &record, sizeof(record));
 46
 47       lseek(fd, position, SEEK_SET);
 48       lockf(fd, F_ULOCK, sizeof(record));
 49      } 50      close(fd);
 51 }