Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/shm.h>
- #include <unistd.h>
- #include <stdlib.h>
- struct bufor {
- int mtype;
- int mvalue;
- };
- int *pam;
- #define MAX2 12
- #define MAX 10
- #define SP 1
- #define W 2
- #define zapis pam[MAX+1]
- #define odczyt pam[MAX]
- int main() {
- key_t klucz, kluczm;
- int msgID, shmID;
- int i;
- int pamm[12];
- struct bufor komunikat;
- if ((klucz = ftok(".", 'A')) == -1 ) {
- printf("Blad ftok (main)\n");
- exit(1);
- }
- msgID=msgget(klucz,IPC_CREAT|0666); //uzyskanie dosepu do kolejki komunikatow
- if (msgID==-1) {
- printf("blad kolejki komunikatow\n");
- exit(1);
- }
- kluczm=ftok(".",'B'); //uzyskanie dosepu do pamieci dzielonej
- shmID=shmget(kluczm, MAX2*sizeof(int),IPC_CREAT|0666);
- if((pam = shmat(shmID, NULL, 0)) == (int *)-1) { //przylaczenie pam. dzielonej
- printf("Blad pamieci wspoldzielonej\n");
- exit(EXIT_FAILURE);
- }
- msgrcv(msgID, &komunikat, sizeof(int), W, 0);
- pam[13] = pam[13] + 1;
- if(pam[13]==1)
- msgrcv(msgID, &komunikat, sizeof(int), SP, 0);
- komunikat.mtype = W;
- msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0);
- for(i=0; i<MAX; i++)
- fprintf(stderr,"czytam %d \n",pam[i]);
- msgrcv(msgID, &komunikat, sizeof(int), W, 0);
- pam[13] = pam[13] - 1;
- if(pam[13]==0) {
- komunikat.mtype = SP;
- msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0);
- }
- komunikat.mtype = W;
- msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0);
- }
Add Comment
Please, Sign In to add comment