"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/test/test-mq-posix.c" (15 Mar 2019, 2753 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test-mq-posix.c": 0.09.52_vs_0.09.54.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 
   26 #include <unistd.h>
   27 #include <stdio.h>
   28 #include <string.h>
   29 #include <sys/types.h>
   30 #include <mqueue.h>
   31 #include <signal.h>
   32 
   33 #if defined(__gnu_hurd__)
   34 #error posix message queues not implemented on GNU/HURD
   35 #endif
   36 #if defined(__FreeBSD_kernel__)
   37 #error posix message queues not implemented with FreeBSD kernel
   38 #endif
   39 
   40 typedef struct {
   41         unsigned int    value;
   42 } msg_t;
   43 
   44 static void notify_func(union sigval s)
   45 {
   46         (void)s;
   47 }
   48 
   49 int main(int argc, char **argv)
   50 {
   51     mqd_t mq;
   52     msg_t msg;
   53     struct mq_attr attr;
   54     int ret;
   55     struct timespec abs_timeout;
   56     struct sigevent sigev;
   57     char mq_name[64];
   58 
   59     attr.mq_flags = 0;
   60     attr.mq_maxmsg = 32;
   61     attr.mq_msgsize = sizeof(msg_t);
   62     attr.mq_curmsgs = 0;
   63 
   64     (void)snprintf(mq_name, sizeof(mq_name), "/%s-%i",
   65         argv[0], getpid());
   66     /*
   67      * This is not meant to be functionally
   68      * correct, it is just used to check we
   69      * can build minimal POSIX message queue
   70      * based code
   71      */
   72     mq = mq_open(mq_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
   73     if (mq < 0)
   74         return -1;
   75 
   76     (void)memset(&sigev, 0, sizeof sigev);
   77     sigev.sigev_notify = SIGEV_THREAD;
   78     sigev.sigev_notify_function = notify_func;
   79     sigev.sigev_notify_attributes = NULL;
   80 
   81     ret = mq_notify(mq, &sigev);
   82     (void)ret;
   83     (void)memset((void *)&abs_timeout, 0, sizeof(abs_timeout));
   84     ret = mq_timedreceive(mq, (char *)&msg, sizeof(msg), NULL, &abs_timeout);
   85     (void)ret;
   86     ret = mq_receive(mq, (char *)&msg, sizeof(msg), NULL);
   87     (void)ret;
   88     ret = mq_getattr(mq, &attr);
   89     (void)ret;
   90     ret = mq_timedsend(mq, (char *)&msg, sizeof(msg), 1, &abs_timeout);
   91     (void)ret;
   92     ret = mq_send(mq, (char *)&msg, sizeof(msg), 1);
   93     (void)ret;
   94     ret = mq_close(mq);
   95     (void)ret;
   96     ret =  mq_unlink(mq_name);
   97     (void)ret;
   98 
   99     return 0;
  100 }