"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/test/test-shm-sysv.c" (15 Mar 2019, 2114 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-shm-sysv.c": 0.09.49_vs_0.09.50.

    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 <unistd.h>
   28 #include <sys/ipc.h>
   29 #include <sys/shm.h>
   30 #include <sys/stat.h>
   31 #include <fcntl.h>
   32 
   33 int main(void)
   34 {
   35     int shm_id, ret;
   36     key_t key;
   37     size_t sz = 64 * 1024;
   38     char *addr;
   39 
   40     /*
   41      * This is not meant to be functionally
   42      * correct, it is just used to check we
   43      * can build minimal POSIX semaphore
   44      * based code
   45      */
   46     key = (key_t)getpid();
   47 
   48     shm_id = shmget(key, sz, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
   49     if (shm_id < 0)
   50         return -1;
   51     addr = shmat(shm_id, NULL, 0);
   52     if (addr == (char *)-1)
   53         goto reap;
   54 
   55 #if defined(IPC_STAT)
   56     {
   57         struct shmid_ds s;
   58 
   59         ret = shmctl(shm_id, IPC_INFO, (struct shmid_ds *)&s);
   60         (void)ret;
   61     }
   62 #endif
   63 #if defined(__linux__) && defined(IPC_INFO)
   64     {
   65         struct shminfo s;
   66 
   67         ret = shmctl(shm_id, IPC_INFO, (struct shmid_ds *)&s);
   68         (void)ret;
   69     }
   70 #endif
   71 #if defined(__linux__) && defined(SHM_INFO)
   72     {
   73         struct shm_info s;
   74 
   75         ret = shmctl(shm_id, SHM_INFO, (struct shmid_ds *)&s);
   76         (void)ret;
   77     }
   78 #endif
   79 
   80     (void)shmdt(addr);
   81 reap:
   82     ret = shmctl(shm_id, IPC_RMID, NULL);
   83     (void)ret;
   84     return 0;
   85 }