#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#include <limits.h>
#include <time.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define MAX 1000
#define PRIO MQ_PRIO_MAX
#define MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
#define FALSE 0
#define TRUE (!FALSE)
void sigh(int);
void berechne(FILE *datei_ptr, uint64_t SECS, uint64_t rate, char *name,
uint64_t BLOCKSIZE, volatile uint64_t counter);
volatile sig_atomic_t flag = TRUE;
int main(int argc, const char *argv[])
{
int i = 0;
size_t anz;
mqd_t tmq = 0;
int durchl = 0;
pid_t npid = 0;
FILE *datei_ptr;
uint64_t SECS = 5;
uint64_t rate = 0;
unsigned int prio;
char *senden = NULL;
char *empfang = NULL;
uint64_t BLOCKSIZE = 0;
struct mq_attr mq_attr;
char tmq_name[] = “tmq”;
char *name = “NACHQU6B.TXT”;
volatile uint64_t counter = 0;
printf(“BLOCKSIZE: %d \n”, atoi(argv[1]));
BLOCKSIZE = atoi(argv[1]);
senden = malloc(BLOCKSIZE sizeof(char)); / Heap */
empfang = malloc(BLOCKSIZE sizeof(char)); / Heap */
for (i = 0; i < BLOCKSIZE - 1; i++)
{
senden[i] = ‘x’;
empfang[i] = ‘y’;
}
senden[BLOCKSIZE - 1] = ‘\0’;
empfang[BLOCKSIZE - 1] = ‘\0’;
mq_attr.mq_maxmsg = MAX;
mq_attr.mq_msgsize = BLOCKSIZE;
mq_attr.mq_flags = 0;
if ((tmq = mq_open(tmq_name, O_CREAT | O_RDWR, MODE, &mq_attr)) == - 1)
{
printf(“Error is: %s\n”, strerror(errno));
return (EXIT_FAILURE);
}
npid = fork();
if (npid)
{
signal(SIGALRM, sigh);
alarm(SECS);
if ((tmq = mq_open(tmq_name, O_WRONLY)) == - 1)
{
printf("Error is: %s\n", strerror(errno));
return (EXIT_FAILURE);
}
while (flag)
{
if (mq_send(tmq, senden, strlen(senden) + 1, PRIO) != - 1)
{
counter++;
}
}
berechne(datei_ptr, SECS, rate, name, BLOCKSIZE, counter);
if (mq_close(tmq) == - 1)
{
printf("Error is: %s\n", strerror(errno));
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
else
{
signal(SIGALRM, sigh);
alarm(SECS);
while (flag)
{
if ((anz = mq_receive(tmq, empfang, strlen(empfang) + 1, &prio)) == - 1)
{
if (errno == EMSGSIZE)
{
printf("Message to big mq_receive \n");
return (EXIT_FAILURE);
}
}
else
{
//receive
}
}
if (mq_unlink(tmq_name) != 0)
{
printf("Error is: %s\n", strerror(errno));
return (EXIT_FAILURE);
}
free(senden);
free(empfang);
return (EXIT_SUCCESS);
}
return (EXIT_SUCCESS);
}
void sigh(int sig_num)
{
flag = FALSE;
}
void berechne(FILE *datei_ptr, uint64_t SECS, uint64_t rate, char *name,
uint64_t BLOCKSIZE, volatile uint64_t counter)
{
rate = (counter *BLOCKSIZE) / (SECS *1024);
printf(“BLOCKSIZE: %8lld COUNTER: %8lld RATE : %8lld KByte\n”, BLOCKSIZE,
counter, rate);
datei_ptr = fopen(name, “a+”);
fprintf(datei_ptr, “BLOCKSIZE: %8lld COUNTER: %8lld RATE: %8lld KByte\n”,
BLOCKSIZE, counter, rate);
if (datei_ptr == NULL)
/* Error */
{
printf(“Error is: %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
fclose(datei_ptr);
}