/************************************************* * Grab some shared memory. * This program was written to test how much can * shared memory we can get out of an HP-UX 10.20. * * Whatch out, you need to release the memory using * ipcrm ! * * Everytime you'll run this prg, it will grab a new * piece of memory. THIS PROGRAM DOES NOT RELEASE * SHARED MEMORY !!! * * * * Copyright Yves Dorfsman 1999. *************************************************/ #include #include #include #include #include #include #include void usage(char*); main(int argc, char** argv) { int howlong, shm_id; char num[255], *unit; long size; if (argc != 2) { usage(argv[0]); return -1; } /*************** * If the arg finishes with m, M, k, or K, then the size is in MegaBytes, * or KiloBytes. * We need to split the arg in two pieces: the last character (unit) * and the rest (num). * eg: argv[1]=22M ==> num=22, unit=M ****************/ howlong = strlen(argv[1]) - 1; strncpy(num, argv[1], howlong); unit = argv[1] + howlong; printf("%s %s\n", num, unit); /*********** * Ok, now that we have num and unit, we have to find out if unit * is a number (in which case the arg was given in bytes and is equal * to argv[1]), a k (then, arg is in kbytes), etc... ***********/ /* We assume that unit = k */ size = atol(num) * 1024; if ( strcasecmp(unit, "m") == 0) size *= 1024; else if ( strcasecmp(unit, "g") == 0) size *= 1048576; else if ( strcasecmp(unit, "k") != 0) /* if neither gig nor meg nor kilo, then was all number */ size = atol(argv[1]); printf("\nGetting a shared memory segment of %d\n", size); shm_id = shmget(0, size, 0666); if (shm_id == -1) { perror(argv[0]); return errno; } else { printf("Shared memory ID = %d\n", shm_id); printf("Please remember to release it with ipcrm.\n\n"); } } void usage(char* progname) { fprintf(stderr, "\n\t%s:\tSyntax error.\n", progname); fprintf(stderr, "\tUsage:\t%s size\n\n", progname); fprintf(stderr, "\tsize can be express in bytes, KB, MB, or GB."); fprintf(stderr, "\tSimply add the suffix k,m, or g imediately after the number"); fprintf(stderr, "\tUsage:\t%s 100m\n\n", progname); }