Тексты программ create.c 1 #include <sys/types.h> 2 #include <time.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include <stdlib.h> 6 #include <fcntl.h> 7 #include <stdio.h> 8 #include <sys/uio.h> 9 #include "employee.h" 10 #include "empheader.h" 11 12 main(int argc, char *argv[]) 13 { 14 int fd; 15 struct employee record; 16 struct recheader header; 17 struct iovec iov[2]; 18 19 if (argc < 2) { 20 fprintf(stderr, "Usage: %s file\n", argv[0]); 21 exit(1); 22 } 23 if ((fd = open(argv[1], O_WRONLY | O_CREAT | 24 O_SYNC | O_EXCL, 0640)) == -1) { 25 perror(argv[1]); 26 exit(2); 27 } 28 29 iov[0].iov_base = (caddr_t)&header; 30 iov[1].iov_base = (caddr_t)&record; 31 iov[0].iov_len = sizeof(header); 32 iov[1].iov_len = sizeof(record); 33 34 init_header(&header); 35 36 for (;;) { 37 printf("Enter employee name <SPACE> salary: "); 38 scanf("%s", record.name); 39 if (record.name[0] == '.') 40 break; 41 scanf("%d", &record.salary); 42 writev(fd, iov, 2); 43 } 44 close(fd); 45 exit(0); 46 } 47 48 static void init_header(struct recheader *head) 49 { 50 long tloc; 51 52 tloc = time(0); 53 strncpy(head->date,ctime(&tloc),24); 54 head->uid = getuid(); 55 } inquire.c 1 #include <sys/types.h> 2 #include <time.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <fcntl.h> 6 #include <stdio.h> 7 #include <unistd.h> 8 #include <sys/uio.h> 9 #include "employee.h" 10 #include "empheader.h" 11 12 main(int argc, char *argv[]) 13 { 14 int fd, recnum; 15 struct employee record; 16 struct recheader header; 17 struct iovec iov[2]; 18 19 if ((fd = open(argv[1], O_RDONLY)) == -1) { 20 perror(argv[1]); 21 exit(2); 22 } 23 24 iov[0].iov_base = (caddr_t)&header; 25 iov[1].iov_base = (caddr_t)&record; 26 iov[0].iov_len = sizeof(header); 27 iov[1].iov_len = sizeof(record); 28 29 for (;;) { 30 printf("\nEnter record number: "); 31 scanf("%d", &recnum); 32 if (recnum == 0) 33 break; 34 lseek(fd, (recnum-1)*(sizeof(record)+sizeof(header)), 35 SEEK_SET); 36 if (readv(fd, iov, 2) > 0) 37 printf("Employee: %s\tSalary: %d\n", 38 record.name, record.salary); 39 else 40 printf("Record %d not found\n", recnum); 41 } 42 close(fd); 43 exit(0); 44 } update1.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 off_t size; 13 struct employee *p; 14 int fd, recnum; 15 16 if ((fd = open(argv[1], O_RDWR)) == -1) { 17 perror(argv[1]); 18 exit(1); 19 } 20 21 size = lseek(fd, 0, SEEK_END); 22 p = (struct employee *)mmap(0, size, 23 PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 24 25 for(;;) { 26 printf("\nEnter record number: "); 27 scanf("%d", &recnum); 28 recnum--; 29 if (recnum < 0) 30 break; 31 if (recnum * sizeof(struct employee) >= size) { 32 printf("record %d not found\n", recnum+1); 33 continue; 34 } 35 printf("Employee: %s, salary: %d\n", 36 p[recnum].name, p[recnum].salary); 37 38 printf("Enter new salary: "); 39 scanf("%d", &p[recnum].salary); 40 msync(p, size, MS_SYNC); 41 } 42 munmap(p, size); 43 close(fd); 44 }