Skip to content

Instantly share code, notes, and snippets.

@do-berry
Created May 13, 2019 14:53
Show Gist options
  • Select an option

  • Save do-berry/ab9600cb8b0a3220b0a96cb866688123 to your computer and use it in GitHub Desktop.

Select an option

Save do-berry/ab9600cb8b0a3220b0a96cb866688123 to your computer and use it in GitHub Desktop.
PW, kolejki posix
#define SIZE 80
#define MQ_NAME "/Kolejka"
typedef struct {
int pnr ; /* numer procesu */
char text[SIZE]; /* tekst komunikatu */
} ms_type;
// 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;
}
//---------------------------------------------------------------------------------
// 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);
}
#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