Тексты программ Текст программы - semdemo.c 1 #include <unistd.h> 2 #include <stdlib.h> 3 #include <sys/types.h> 4 #include <sys/ipc.h> 5 #include <sys/sem.h> 6 #include <stdio.h> 7 #define DUMMY 0 8 #define COUNT 4 9 10 main(int argc, char *argv[]) 11 { 12 key_t ipckey; 13 int semid, pid, creator, i; 14 static struct sembuf lock = 15 { 0, -1, SEM_UNDO }; 16 static struct sembuf unlock = 17 { 0, 1, SEM_UNDO }; 18 19 setbuf(stdout, (char *) NULL); 20 ipckey = ftok(argv[0], 's'); 21 if ((semid = semget(ipckey, 1, 22 IPC_CREAT | IPC_EXCL | 0666)) != -1) { 23 creator = 1; 24 } 25 else { 26 if ((semid = semget(ipckey, 1, 0)) == -1) { 27 perror(argv[0]); 28 exit(1); 29 } 30 creator = 0; 31 } 32 if (creator) {/* initialize semaphore */ 33 if (semctl(semid, 0, SETVAL, 1) == -1) { 34 perror(argv[0]); 35 exit(2); 36 } 37 } 38 39 pid = getpid(); 40 for (i = 0; i < COUNT; i++ ) { 41 if (semop(semid, &lock, 1) == -1) { 42 perror(argv[0]); 43 exit(3); 44 } 45 printf("[%d] locking\t", pid); 46 sleep(1);/* terminal output being used */ 47 printf("[%d] unlocking\n", pid); 48 if (semop(semid, &unlock, 1) == -1) { 49 perror(argv[0]); 50 exit(4); 51 } 52 } 53 54 if (creator) { 55 sleep(5); 56 if(semctl(semid, DUMMY, IPC_RMID, DUMMY) == -1) { 57 perror(argv[0]); 58 exit(5); 59 } 60 } 61 return(0); 62 } Текст программы - printer.c 1 #include <stdlib.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <fcntl.h> 5 #include <sys/types.h> 6 #include <sys/ipc.h> 7 #include <sys/sem.h> 8 #include <stdio.h> 9 #include "printer.h" 10 11 main(int argc, char *argv[]) 12 { 13 char *printer[NUMPR + 1],buf[BUFSIZ],errmsg[30]; 14 key_t ipckey; 15 ushort initial[NUMPR + 1], prntnum; 16 int n, creator, fdin, fdout, semid; 17 struct sembuf operation[2]; 18 19 if ((printer[1] = getenv("PRINTER1")) == NULL || 20 (printer[2] = getenv("PRINTER2")) == NULL) { 21 printf("missing printer assignment\n"); 22 exit(1); 23 } 24 if (strncmp(argv[0], "line", 4) == 0) 25 prntnum = 1; 26 else if (strncmp(argv[0], "lase", 4) == 0) 27 prntnum = 2; 28 else /* random choice */ 29 prntnum = getpid() % NUMPR + 1; 30 31 ipckey = ftok(argv[0], 's'); 32 if ((semid = semget(ipckey, NUMPR + 1, 33 IPC_CREAT | IPC_EXCL | 0666)) != -1) { 34 creator = 1; 35 } 36 else { 37 if ((semid = semget(ipckey, NUMPR + 1, 38 0666)) == -1) { 39 sprintf(errmsg,"%s - semget", argv[0]); 40 perror(errmsg); 41 exit(2); 42 } 43 creator = 0; 44 } 45 if (creator) { /* initialize semaphore set */ 46 initial[0] = NUMPR; 47 for (n = 1; n <= NUMPR; n++) 48 initial[n] = 1; 49 if(semctl(semid,DUMMY,SETALL,initial) == -1) { 50 sprintf(errmsg,"%s - SETALL", argv[0]); 51 perror(errmsg); 52 exit(3); 53 } 54 } 55 56 operation[1].sem_num = prntnum; 57 operation[1].sem_op = ACQUIRE; 58 operation[1].sem_flg = SEM_UNDO; 59 operation[0].sem_num = 0; 60 operation[0].sem_op = ACQUIRE; 61 operation[0].sem_flg = SEM_UNDO; 62 if (semop(semid, operation, 2) == -1) { 63 sprintf(errmsg,"%s - ACQUIRE", argv[0]); 64 perror(errmsg); 65 exit(4); 66 } 67 if ((fdin = open(argv[1], O_RDONLY)) == -1) { 68 sprintf(errmsg,"%s - %s", argv[0], argv[1]); 69 perror(errmsg); 70 exit(5); 71 } 72 if((fdout=open(printer[prntnum],O_CREAT | O_WRONLY,0644)) == - 1) { 73 sprintf(errmsg,"%s - %s",argv[0],printer[prntnum]); 74 perror(errmsg); 75 exit(6); 76 } 77 while ((n = read(fdin, buf, BUFSIZ)) > 0) 78 write(fdout, buf, n); 79 operation[1].sem_op = RELEASE; 80 operation[0].sem_op = RELEASE; 81 if (semop(semid, operation, 2) == -1) { 82 sprintf(errmsg,"%s - RELEASE", argv[0]); 83 perror(errmsg); 84 exit(7); 85 } 86 87 }