VM: simplify slab allocator
[minix.git] / test / ipc / shmt / shmt03.c
blobd3e5564613d1c4da89fe09f8946a65f5081c19d9
1 /*
3 * Copyright (c) International Business Machines Corp., 2002
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* 12/20/2002 Port to LTP robbiew@us.ibm.com */
21 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
24 * NAME
25 * shmt3
27 * CALLS
28 * shmctl(2) shmget(2) shmat(2)
30 * ALGORITHM
31 * Create one shared memory segment and attach it twice to the same process,
32 * at an address that is chosen by the system. After the first attach has
33 * completed, write to it and then do the second attach.
34 * Verify that the doubly attached segment contains the same data.
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43 #include <errno.h>
45 /** LTP Port **/
46 #include "test.h"
47 #include "usctest.h"
49 char *TCID="shmt03"; /* Test program identifier. */
50 int TST_TOTAL=4; /* Total number of test cases. */
51 extern int Tst_count; /* Test Case counter for tst_* routines */
52 /**************/
54 #define K_1 1024
55 #define SUCCESSFUL 1
57 int first_attach,
58 second_attach;
59 int rm_shm(int);
61 int main(void)
63 char *cp1, *cp2;
64 int shmid;
65 key_t key ;
68 key = (key_t) getpid() ;
69 errno = 0 ;
71 /*------------------------------------------------------------*/
74 if ((shmid = shmget(key, 16*K_1, IPC_CREAT|0666)) < 0) {
75 perror("shmget");
76 tst_resm(TFAIL, "shmget Failed: shmid = %d, errno = %d\n",
77 shmid, errno) ;
78 tst_exit() ;
81 tst_resm(TPASS,"shmget");
83 /*------------------------------------------------------------*/
86 if ((cp1 = (char *)shmat(shmid, (void *)0, 0)) == (char *)-1) {
87 perror("shmat");
88 tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d\n",
89 shmid, errno) ;
90 } else {
91 *cp1 = '1' ;
92 *(cp1+5*K_1) = '2' ;
93 first_attach = SUCCESSFUL ;
96 tst_resm(TPASS,"1st shmat");
98 /*------------------------------------------------------------*/
101 if ((cp2 = (char *)shmat(shmid, (void *)0, 0)) == (char *)-1) {
102 perror("shmat");
103 tst_resm(TFAIL, "shmat Failed: shmid = %d, errno = %d\n",
104 shmid, errno) ;
106 else {
107 second_attach = SUCCESSFUL ;
108 if ( (*cp2 != '1' || *(cp2+5*K_1) != '2') &&
109 first_attach == SUCCESSFUL ) {
110 tst_resm(TFAIL,
111 "Error: Shared memory contents\n") ;
115 tst_resm(TPASS,"2nd shmat");
117 /*---------------------------------------------------------------*/
120 rm_shm(shmid) ;
122 if ( first_attach && second_attach ) {
123 if ( *cp2 != '1' || *(cp2+5*K_1) != '2' ||
124 *cp1 != '1' || *(cp1+5*K_1) != '2' ) {
125 tst_resm(TFAIL, "Error: Shared memory contents\n") ;
129 tst_resm(TPASS, "Correct shared memory contents");
130 /*-----------------------------------------------------------------*/
131 tst_exit() ;
133 /*----------------------------------------------------------------*/
134 return(0);
137 int rm_shm(shmid)
138 int shmid ;
140 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
141 perror("shmctl");
142 tst_resm(TFAIL,
143 "shmctl Failed to remove: shmid = %d, errno = %d\n",
144 shmid, errno) ;
145 tst_exit();
147 return(0);