#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <getopt.h>
#define MES "hogehoge"
static int _write_thread_flag = 0;
static void *
_write_thread(void *arg)
{
mqd_t r;
mqd_t t = *((mqd_t *)arg);
int write_count = 0, err_count = 0;
while(1) {
r = mq_send(t, MES , sizeof(MES) , 0);
if (r == (mqd_t)-1) {
++err_count;
}
if (_write_thread_flag) break;
++write_count;
}
printf("%d %d %lu\n", write_count, err_count, pthread_self());
return NULL;
}
static int
_calc_value(const char *num, int default_val)
{
char *endptr = 0;
long val = strtol(num, &endptr, 10);
if (*endptr != 0) {
return default_val;
}
return val;
}
static void
_parse_option(int argc, char **argv, int *thread, int *time)
{
int c;
int index;
struct option opt[] = {
{"thread", 1, 0, 0},
{"time", 1, 0, 1},
{0,0,0,0}
};
while(1) {
c = getopt_long(argc, argv, "", opt, &index);
if (c == -1) break;
switch(c) {
case 0:
if (thread) *thread = _calc_value(optarg,1);
break;
case 1:
if (time) *time = _calc_value(optarg, 60);
break;
default:
break;
}
}
}
int
main(int argc, char **argv)
{
mqd_t q;
int i;
pthread_t thread;
int time = 60, num_thread = 1;
_parse_option(argc, argv, &num_thread, &time);
q = mq_open("/hoge", O_WRONLY | O_CREAT , 0600, 0);
if (q == (mqd_t)-1) {
printf("hoge %s\n", strerror(errno));
return -1;
}
printf("%d %d\n", num_thread, time);
for (i = 0; i < num_thread; ++i) {
pthread_create(&thread, NULL, &_write_thread, &q);
}
sleep(time);
_write_thread_flag = 1;
sleep(5);
return 0;
}