handle multi-part files
[tridge/junkcode.git] / shm_size.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/ipc.h>
6 #include <sys/shm.h>
7
8 void *shm_setup(int size)
9 {
10         int shmid;
11         void *ret;
12
13         shmid = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
14         if (shmid == -1) {
15                 printf("can't get shared memory\n");
16                 exit(1);
17         }
18         ret = (void *)shmat(shmid, 0, 0);
19         if (!ret || ret == (void *)-1) {
20                 printf("can't attach to shared memory\n");
21                 return NULL;
22         }
23         /* the following releases the ipc, but note that this process
24            and all its children will still have access to the memory, its
25
26            means we don't leave behind lots of shm segments after we exit 
27
28            See Stevens "advanced programming in unix env" for details
29            */
30         shmctl(shmid, IPC_RMID, 0);
31
32
33         
34         return ret;
35 }
36
37 int main(int argc, char *argv[])
38 {
39         volatile char *buf;
40         int size;
41
42         if (argc < 2) {
43                 printf("shm_size <size>\n");
44                 exit(1);
45         }
46
47         size = atoi(argv[1]);
48
49         buf = shm_setup(size);
50
51         if (!buf) {
52                 printf("shm_setup(%d) failed\n", size);
53                 exit(1);
54         }
55         return 0;
56 }
57