Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author Dawid Mocek
- SHM exmaple code from https://beej.us/guide/bgipc/output/html/multipage/shm.html
- Testing Hugepages:
- [oracle@we-pr-002-2 ~]$ ulimit -l
- 1048576
- [oracle@we-pr-002-2 ~]$ cat /proc/meminfo
- MemTotal: 8176208 kB
- MemFree: 5985184 kB
- Buffers: 13556 kB
- Cached: 952644 kB
- SwapCached: 0 kB
- Active: 806600 kB
- Inactive: 212156 kB
- Active(anon): 52588 kB
- Inactive(anon): 324 kB
- Active(file): 754012 kB
- Inactive(file): 211832 kB
- Unevictable: 0 kB
- Mlocked: 0 kB
- SwapTotal: 2097148 kB
- SwapFree: 2097148 kB
- Dirty: 244 kB
- Writeback: 0 kB
- AnonPages: 52584 kB
- Mapped: 9144 kB
- Shmem: 336 kB
- Slab: 62416 kB
- SReclaimable: 17324 kB
- SUnreclaim: 45092 kB
- KernelStack: 840 kB
- PageTables: 3728 kB
- NFS_Unstable: 0 kB
- Bounce: 0 kB
- WritebackTmp: 0 kB
- CommitLimit: 5660964 kB
- Committed_AS: 112860 kB
- VmallocTotal: 34359738367 kB
- VmallocUsed: 15700 kB
- VmallocChunk: 34359711467 kB
- HardwareCorrupted: 0 kB
- HugePages_Total: 512
- HugePages_Free: 507
- HugePages_Rsvd: 251
- HugePages_Surp: 0
- Hugepagesize: 2048 kB
- DirectMap4k: 8192 kB
- DirectMap2M: 8380416 kB
- [oracle@we-pr-002-2 ~]$ ipcs
- ------ Shared Memory Segments --------
- key shmid owner perms bytes nattch status
- 0x52030b0f 131073 oracle 644 536870912 0
- ------ Semaphore Arrays --------
- key semid owner perms nsems
- ------ Message Queues --------
- key msqid owner perms used-bytes messages
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #define SHM_SIZE 536870912 /* make it a 512M shared memory segment */
- int main(int argc, char *argv[])
- {
- key_t key;
- int shmid;
- char *data;
- int mode;
- char c;
- FILE *fh = NULL;
- long size;
- if (argc > 2) {
- fprintf(stderr, "usage: shmdemo [big_text_file]\n");
- exit(1);
- }
- /* make the key: */
- if ((key = ftok("shm.c", 'R')) == -1) {
- perror("ftok");
- exit(1);
- }
- printf("Geting shared memory for key....\n");
- /* connect to (and possibly create) the segment: */
- if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT | SHM_HUGETLB)) == -1) {
- perror("shmget");
- exit(1);
- }
- printf("... got. Waiting to attach to shared memory\n");
- c = getc(stdin);
- /* attach to the segment to get a pointer to it: */
- data = shmat(shmid, (void *)0, 0);
- if (data == (char *)(-1)) {
- perror("shmat");
- exit(1);
- }
- printf("Attached to shared memory\n");
- /* read or modify the segment, based on the command line: */
- if (argc == 2) {
- printf("Modifing shared memory...\n");
- printf("Writing to segment file: \"%s\"\n", argv[1]);
- fh = fopen(argv[1], "r+");
- fseek(fh, 0L, SEEK_END);
- size = ftell(fh);
- fseek(fh, 0L, SEEK_SET);
- fread(data, size, sizeof(char), fh);
- fclose(fh);
- // strncpy(data, argv[1], SHM_SIZE);
- printf("Shared memory modified\n");
- } else {
- printf("Reading shared memory\n");
- printf("Segment contains: \"%s\"\n", data);
- printf("Done reading shared memory\n");
- }
- printf("Detaching shared memory\n");
- /* detach from the segment: */
- if (shmdt(data) == -1) {
- perror("shmdt");
- exit(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement