sort: add -x hex sort feature back
[minix.git] / test / ipc / shmt / shmt10.c
blobfd1d7bab0dcaaba66700a8fba7a6f9fb8eb48a60
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 * shmt10.c - test simultaneous shmat/shmdt
27 * CALLS
28 * shmget, shmat, shmdt, shmctl
30 * ALGORITHM
31 * Create a shared memory segment and fork a child. Both
32 * parent and child spin in a loop attaching and detaching
33 * the segment. After completing the specified number of
34 * iterations, the child exits and the parent deletes the
35 * segment.
37 * USAGE
38 * shmt10 [-i 500]
39 * -i # of iterations, default 500
44 #include <stdio.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <sys/ipc.h>
48 #include <sys/shm.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <signal.h>
52 #include <errno.h>
54 #define SIZE 0x32768
56 /** LTP Port **/
57 #include "test.h"
58 #include "usctest.h"
60 char *TCID="shmt10"; /* Test program identifier. */
61 int TST_TOTAL=2; /* Total number of test cases. */
62 extern int Tst_count; /* Test Case counter for tst_* routines */
63 /**************/
65 int shmid;
66 key_t key;
68 int child(int);
69 int rm_shm(int);
70 void fini(int);
72 int main(int argc, char *argv[])
74 char *c1=NULL;
75 int pid, st;
76 register int i;
77 int iter = 500;
78 int c;
79 extern char *optarg;
81 key = (key_t)getpid();
82 signal(SIGTERM, fini);
84 /*--------------------------------------------------------*/
86 while ((c = getopt(argc, argv, "i:")) != EOF) {
87 switch (c) {
88 case 'i':
89 iter = atoi(optarg);
90 break;
91 default:
92 tst_resm(TCONF, "usage: %s [-i <# iterations>]", argv[0]);
93 tst_exit();
98 /*------------------------------------------------------------------------*/
100 if ((shmid = shmget(key, SIZE, IPC_CREAT|0666)) < 0) {
101 tst_resm(TFAIL,"shmget") ;
102 tst_resm(TFAIL, "Error: shmid = %d\n", shmid) ;
103 tst_exit() ;
106 pid = fork();
107 switch(pid) {
108 case -1:
109 tst_resm(TBROK,"fork failed");
110 tst_exit() ;
111 case 0:
112 child(iter);
113 tst_exit();
116 for (i = 0; i < iter; i++) {
117 if ((c1 = (char *) shmat(shmid, (void *)0, 0)) == (char *)-1) {
118 tst_resm(TFAIL,
119 "Error shmat: iter %d, shmid = %d\n", i, shmid);
120 break;
122 if (shmdt(c1) < 0) {
123 tst_resm(TFAIL,
124 "Error: shmdt: iter %d ", i) ;
125 break;
128 while ( wait(&st) < 0 && errno == EINTR )
130 tst_resm(TPASS,"shmat,shmdt");
131 /*------------------------------------------------------------------------*/
133 rm_shm(shmid);
134 tst_exit();
136 /*------------------------------------------------------------------------*/
137 return(0);
140 int rm_shm(shmid)
141 int shmid ;
143 if (shmctl(shmid, IPC_RMID, NULL) == -1) {
144 perror("shmctl");
145 tst_resm(TFAIL,
146 "shmctl Failed to remove: shmid = %d, errno = %d\n",
147 shmid, errno) ;
148 tst_exit();
150 return(0);
153 int child(iter)
154 int iter;
156 register int i;
157 char *c1;
159 for (i = 0; i < iter; i++) {
160 if ((c1 = (char *) shmat(shmid, (void *)0, 0)) == (char *)-1) {
161 tst_resm(TFAIL,
162 "Error:child proc: shmat: iter %d, shmid = %d\n",
163 i, shmid);
164 tst_exit();
166 if (shmdt(c1) < 0) {
167 tst_resm(TFAIL,
168 "Error: child proc: shmdt: iter %d ", i);
169 tst_exit();
172 return(0);
175 void
176 fini(int sig)
178 rm_shm(shmid);