Created
May 13, 2019 14:53
-
-
Save do-berry/ab9600cb8b0a3220b0a96cb866688123 to your computer and use it in GitHub Desktop.
PW, kolejki posix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define SIZE 80 | |
| #define MQ_NAME "/Kolejka" | |
| typedef struct { | |
| int pnr ; /* numer procesu */ | |
| char text[SIZE]; /* tekst komunikatu */ | |
| } ms_type; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Kompilacja: cc wyslij.c -o wyslij -lrt | |
| #include <stdio.h> | |
| #include <mqueue.h> | |
| #include <unistd.h> | |
| #include "common.h" | |
| int main(int argc, char * argv[]) { | |
| int res; | |
| mqd_t mq; | |
| ms_type kom; | |
| struct mq_attr attr; | |
| /* Utworzenie kolejki komunikatow -----------------*/ | |
| attr.mq_msgsize = sizeof(kom); | |
| attr.mq_maxmsg = 4; | |
| attr.mq_flags = 0; | |
| mq_unlink("/Kolejka"); | |
| mq = mq_open("/Kolejka", O_RDWR | O_CREAT, 0666, & attr); | |
| if(mq < 0) { | |
| perror("prod.c /Kolejka"); | |
| } | |
| mq_close(mq); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //--------------------------------------------------------------------------------- | |
| // Producent / konsument proces wysylajacy komunikaty do kolejki | |
| // Odbiera program mq_rcv | |
| // Kompilacja: gcc mq_snd.c -o mq_snd -lrt | |
| // Uruchomienie: ./mq_snd numer | |
| // Gdy zamontujemy koleki komunikatow w kat /dev/mqueue to mozna nimi manipulowaÄ | |
| // uzywajÄ c poleceĹ ls,... | |
| // mkdir /dev/mqueue | |
| // mount -t mqueue none /dev/mqueue | |
| // ---------------------------------------------------------------------------------- | |
| #include <stdio.h> | |
| #include <mqueue.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include "common.h" | |
| int main(int argc, char *argv[]) { | |
| int i; | |
| int kroki; | |
| int res; | |
| mqd_t mq; | |
| ms_type msg; | |
| struct mq_attr attr; | |
| mq=mq_open("/Kolejka",O_RDWR,0660,NULL); | |
| if(mq < 0) { | |
| perror("prod.c /Kolejka"); | |
| exit(-1); | |
| } | |
| msg.pnr = atoi(argv[1]); | |
| kroki = atoi(argv[2]); | |
| for(i=0; i < kroki ;i++) { | |
| sprintf(msg.text,"Proces %d komunikat %d ",msg.pnr, i); | |
| res = mq_send(mq,(char *)&msg,sizeof(msg),10); | |
| if(res <0) perror("send"); | |
| printf("%s\n",msg.text); | |
| sleep(1); | |
| } | |
| mq_close(mq); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <mqueue.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include "common.h" | |
| int main(int argc, char *argv[]) | |
| { | |
| int i, res, steps = 0; | |
| unsigned int prior; | |
| mqd_t mq; | |
| ms_type msg; | |
| prior = 10; | |
| struct mq_attr attr; | |
| steps = atoi(argv[1]); | |
| // Utworzenie kolejki komunikatow ---------------- | |
| mq = mq_open("/Kolejka", O_RDWR, 0660, NULL); | |
| if (mq < 0) | |
| { | |
| perror("kons.c /Kolejka"); | |
| exit(0); | |
| } | |
| for (i = 0; i < steps; i++) | |
| { | |
| res = mq_receive(mq,(char *) &msg, sizeof(msg), &prior); | |
| if (res == -1) | |
| perror("Blad odczytu z mq"); | |
| else | |
| printf("Konsument: %d odebral: %s\n", steps, msg.text); | |
| printf("%s\n",msg.text); | |
| sleep(1); | |
| } | |
| mq_close(mq); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment