]>
cygwin.com Git - cygwin-apps/cygutils.git/blob - src/ipc/shmtool.c
1 /*****************************************************************************
2 Excerpt from "Linux Programmer's Guide - Chapter 6"
3 (C)opyright 1994-1995, Scott Burkett
4 *****************************************************************************
6 *****************************************************************************
7 A command line tool for tinkering with shared memory
9 *****************************************************************************/
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * See the COPYING file for license information.
43 main (int argc
, char *argv
[])
52 /* Create unique key via call to ftok() */
53 key
= ftok (".", 'S');
55 /* Open the shared memory segment - create if necessary */
56 if ((shmid
= shmget (key
, SEGSIZE
, IPC_CREAT
| IPC_EXCL
| 0666)) == -1)
58 printf ("Shared memory segment exists - opening as client\n");
60 /* Segment probably already exists - try as a client */
61 if ((shmid
= shmget (key
, SEGSIZE
, 0)) == -1)
69 printf ("Creating new shared memory segment\n");
72 /* Attach (map) the shared memory segment into the current process */
73 if ((segptr
= shmat (shmid
, 0, 0)) == ((char *) -1))
79 switch (tolower (argv
[1][0]))
82 writeshm (shmid
, segptr
, argv
[2]);
85 readshm (shmid
, segptr
);
91 changemode (shmid
, argv
[2]);
99 writeshm (int shmid
, char *segptr
, char *text
)
101 strcpy (segptr
, text
);
102 printf ("Done...\n");
105 readshm (int shmid
, char *segptr
)
107 printf ("segptr: %s\n", segptr
);
110 removeshm (int shmid
)
112 shmctl (shmid
, IPC_RMID
, 0);
113 printf ("Shared memory segment marked for deletion\n");
116 changemode (int shmid
, char *mode
)
118 struct shmid_ds myshmds
;
120 /* Get current values for internal data structure */
121 shmctl (shmid
, IPC_STAT
, &myshmds
);
123 /* Display old permissions */
124 printf ("Old permissions were: %o\n", myshmds
.shm_perm
.mode
);
126 /* Convert and load the mode */
127 sscanf (mode
, "%o", &myshmds
.shm_perm
.mode
);
129 /* Update the mode */
130 shmctl (shmid
, IPC_SET
, &myshmds
);
132 printf ("New permissions are : %o\n", myshmds
.shm_perm
.mode
);
137 fprintf (stderr
, "shmtool - A utility for tinkering with shared memory\n");
138 fprintf (stderr
, "\nUSAGE: shmtool (w)rite <text>\n");
139 fprintf (stderr
, " (r)ead\n");
140 fprintf (stderr
, " (d)elete\n");
141 fprintf (stderr
, " (m)ode change <octal mode>\n");
This page took 0.040794 seconds and 5 git commands to generate.