Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <err.h>
- #include <sys/sem.h>
- #include <stdio.h>
- #include <sys/msg.h>
- #include <errno.h>
- key_t key1, key2, key3; // klucz dla semaforow
- int semid1, semid2, semid3; // ID semaforow
- union semun ctl; // unia do kontroli semafora
- int pid1, pid2, pid3, ppid;
- union semun
- {
- int val;
- struct semid_ds *buf;
- unsigned short int *array;
- struct seminfo *__buf;
- };
- int semlock(int semid)
- {
- struct sembuf opr;
- opr.sem_num = 0;
- opr.sem_op = -1;
- opr.sem_flg = 0;
- if (semop(semid, &opr, 1) == -1){
- warn("Blad blokowania semafora!");
- return 0;
- }else{
- return 1;
- }
- }
- int semunlock(int semid)
- {
- struct sembuf opr;
- opr.sem_num = 0;
- opr.sem_op = 1;
- opr.sem_flg = 0;
- if (semop(semid, &opr, 1) == -1)
- {
- warn("Blad odblokowania semafora!");
- return 0;
- }
- else
- {
- return 1;
- }
- }
- struct mymsgbuf {
- long mtype; /* typ wiadomości */
- int request; /* numer żądania danego działania */
- int i;
- } msg;
- struct mymsgbuf buf;
- int open_queue( key_t keyval ) {
- int qid;
- if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
- return(-1);
- return(qid);
- }
- int send_message( int qid, struct mymsgbuf *qbuf ){
- int result, length;
- /* lenght jest rozmiarem struktury minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgsnd( qid, qbuf, length, 0)) == -1)
- return(-1);
- return(result);
- }
- int remove_queue( int qid ){
- if( msgctl( qid, IPC_RMID, 0) == -1)
- return(-1);
- return(0);
- }
- int read_message( int qid, long type, struct mymsgbuf *qbuf ){
- int result, length;
- /* lenght jest rozmiarem struktury minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
- return(-1);
- return(result);
- }
- int main()
- {
- if ((key1 = ftok(".", 'A')) == -1)
- errx(1, "Blad tworzenia klucza!");
- if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1)
- errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid1, 0, SETVAL, ctl) == -1)
- errx(3, "Blad ustawiania semafora!");
- if ((key2 = ftok(".", 'B')) == -1)
- errx(1, "Blad tworzenia klucza!");
- if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1)
- errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid2, 0, SETVAL, ctl) == -1)
- errx(3, "Blad ustawiania semafora!");
- if ((key3 = ftok(".", 'C')) == -1)
- errx(1, "Blad tworzenia klucza!");
- if ((semid3 = semget(key3, 1, IPC_CREAT | 0600)) == -1)
- errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid3, 0, SETVAL, ctl) == -1)
- errx(3, "Blad ustawiania semafora!");
- semlock(semid2);
- int qid;
- key_t msgkey;
- /* tworzymy wartość klucza IPC */
- msgkey = ftok(".", 'm');
- /* otwieramy/tworzymy kolejkę */
- if(( qid = open_queue( msgkey)) == -1) {
- perror("Otwieranie_kolejki");
- exit(1);
- }
- msg.mtype = 1; /* typ wiadomości musi być dodatni */
- msg.request = 1;
- msg.i = 220; /* wysyana liczba */
- if(fork() == 0) {
- char c;
- while(1){ // (c = fgetc(stdin)) != EOF
- semlock(semid1);
- printf("proces1\n"); fflush(stdout);
- //printf("%c", c); fflush(stdout);
- semunlock(semid2);
- }
- }
- if(fork() == 0) {
- while(1) {
- semlock(semid2);
- printf("proces2\n"); fflush(stdout);
- semunlock(semid3);
- }
- }
- if(fork() == 0) {
- while(1) {
- semlock(semid3);
- printf("proces2\n"); fflush(stdout);
- semunlock(semid31);
- }
- }
- sleep(2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement