"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/test/test-sem-sysv.c" (15 Mar 2019, 3287 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-sem-sysv.c": 0.09.54_vs_0.09.55.

    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 #define  _GNU_SOURCE
   26 
   27 #include <time.h>
   28 #include <sys/types.h>
   29 #include <sys/ipc.h>
   30 #include <sys/sem.h>
   31 #include <sys/stat.h>
   32 #include <fcntl.h>
   33 #include <unistd.h>
   34 
   35 #if defined(__gnu_hurd__)
   36 #error semop, semget and semctl are not implemented
   37 #endif
   38 
   39 typedef union _semun {
   40     int              val;   /* Value for SETVAL */
   41     struct semid_ds *buf;   /* Buffer for IPC_STAT, IPC_SET */
   42     unsigned short  *array; /* Array for GETALL, SETALL */
   43     struct seminfo  *__buf; /* Buffer for IPC_INFO (Linux-specific) */
   44 } semun_t;
   45 
   46 int main(void)
   47 {
   48     key_t key;
   49     int sem;
   50     semun_t arg;
   51     int ret;
   52     struct sembuf semwait, semsignal;
   53     struct timespec timeout;
   54 
   55     /*
   56      * This is not meant to be functionally
   57      * correct, it is just used to check we
   58      * can build minimal POSIX semaphore
   59      * based code
   60      */
   61     key = (key_t)getpid();
   62     sem = semget(key, 1, IPC_CREAT | S_IRUSR | S_IWUSR);
   63     arg.val = 1;
   64     ret = semctl(sem, 0, SETVAL, arg);
   65     (void)ret;
   66     semwait.sem_num = 0;
   67     semwait.sem_op = -1;
   68     semwait.sem_flg = SEM_UNDO;
   69     (void)semwait;
   70     (void)clock_gettime(CLOCK_REALTIME, &timeout);
   71     timeout.tv_sec++;
   72 #if defined(__linux__)
   73     ret = semtimedop(sem, &semwait, 1, &timeout);
   74     (void)ret;
   75 #endif
   76 
   77     semsignal.sem_num = 0;
   78     semsignal.sem_op = 1;
   79     semsignal.sem_flg = SEM_UNDO;
   80 
   81     ret = semop(sem, &semsignal, 1);
   82     (void)ret;
   83 
   84 #if defined(IPC_STAT)
   85     {
   86         struct semid_ds ds;
   87         semun_t s;
   88 
   89         s.buf = &ds;
   90         ret = semctl(sem, 0, IPC_STAT, &s);
   91         (void)ret;
   92     }
   93 #endif
   94 #if defined(SEM_STAT)
   95     {
   96         struct semid_ds ds;
   97         semun_t s;
   98 
   99         s.buf = &ds;
  100         ret = semctl(sem, 0, SEM_STAT, &s);
  101         (void)ret;
  102     }
  103 #endif
  104 #if defined(IPC_INFO) && defined(__linux__)
  105     {
  106         struct seminfo si;
  107         semun_t s;
  108 
  109         s.__buf = &si;
  110         ret = semctl(sem, 0, IPC_INFO, &s);
  111         (void)ret;
  112     }
  113 #endif
  114 #if defined(SEM_INFO) && defined(__linux__)
  115     {
  116         struct seminfo si;
  117         semun_t s;
  118 
  119         s.__buf = &si;
  120         ret = semctl(sem, 0, SEM_INFO, &s);
  121         (void)ret;
  122     }
  123 #endif
  124 #if defined(GETVAL)
  125     ret = semctl(sem, 0, GETVAL);
  126     (void)ret;
  127 #endif
  128 #if defined(GETPID)
  129     ret = semctl(sem, 0, GETPID);
  130     (void)ret;
  131 #endif
  132 #if defined(GETNCNT)
  133     ret = semctl(sem, 0, GETNCNT);
  134     (void)ret;
  135 #endif
  136 #if defined(GEZCNT)
  137     ret = semctl(sem, 0, GETZCNT);
  138     (void)ret;
  139 #endif
  140     ret = semctl(sem, 0, IPC_RMID);
  141     (void)ret;
  142 
  143     return 0;
  144 }