robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * FILE : sem02.c |
robbiew | e52c6dc | 2002-03-06 18:35:27 +0000 | [diff] [blame] | 22 | * |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 23 | * DESCRIPTION : The application creates several threads using pthread_create(). |
| 24 | * One thread performs a semop() with the SEM_UNDO flag set. The change in |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * sempaphore value performed by that semop should be "undone" only when the |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 26 | * last pthread exits. |
robbiew | e52c6dc | 2002-03-06 18:35:27 +0000 | [diff] [blame] | 27 | * |
| 28 | * EXPECTED OUTPUT: |
| 29 | * Waiter, pid = <pid#> |
| 30 | * Poster, pid = <pid#>, posting |
| 31 | * Poster posted |
| 32 | * Poster exiting |
| 33 | * Waiter waiting, pid = <pid#> |
| 34 | * Waiter done waiting |
| 35 | * |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 36 | * HISTORY: |
robbiew | ded99ee | 2002-03-20 20:02:56 +0000 | [diff] [blame] | 37 | * written by Dave Olien (oliend@us.ibm.com) |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 38 | * 03/06/2002 Robbie Williamson (robbiew@us.ibm.com) |
| 39 | * -ported |
plars | 5c34fa3 | 2003-07-05 03:43:44 +0000 | [diff] [blame] | 40 | * 07/04/2003 Paul Larson (plars@linuxtestproject.org) |
| 41 | * -ported to LTP |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 42 | * |
| 43 | */ |
plars | 5c34fa3 | 2003-07-05 03:43:44 +0000 | [diff] [blame] | 44 | #include "test.h" |
plars | 5c34fa3 | 2003-07-05 03:43:44 +0000 | [diff] [blame] | 45 | |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 46 | #include <stdio.h> |
robbiew | b59499d | 2002-03-28 19:21:32 +0000 | [diff] [blame] | 47 | #include <stdlib.h> |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 48 | #include <unistd.h> |
| 49 | #include <sys/sem.h> |
| 50 | #include <errno.h> |
| 51 | #include <pthread.h> |
robbiew | b59499d | 2002-03-28 19:21:32 +0000 | [diff] [blame] | 52 | #include <sys/types.h> |
| 53 | #include <sys/ipc.h> |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 54 | |
| 55 | #define KEY IPC_PRIVATE |
| 56 | |
| 57 | #define NUMTHREADS 2 |
| 58 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 59 | void *retval[NUMTHREADS]; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | void *waiter(void *); |
| 61 | void *poster(void *); |
plars | 5c34fa3 | 2003-07-05 03:43:44 +0000 | [diff] [blame] | 62 | void cleanup(void); |
| 63 | |
| 64 | char *TCID = "sem02"; |
| 65 | int TST_TOTAL = 1; |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 66 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 67 | struct sembuf Psembuf = { 0, -1, SEM_UNDO }; |
| 68 | struct sembuf Vsembuf = { 0, 1, SEM_UNDO }; |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 69 | |
robbiew | b59499d | 2002-03-28 19:21:32 +0000 | [diff] [blame] | 70 | union semun { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | int val; /* value for SETVAL */ |
| 72 | struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */ |
| 73 | unsigned short *array; /* array for GETALL & SETALL */ |
| 74 | struct seminfo *ipc_buf; /* buffer for IPC_INFO */ |
robbiew | b59499d | 2002-03-28 19:21:32 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 77 | int sem_id; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | int err_ret; /* This is used to determine PASS/FAIL status */ |
plars | 5c34fa3 | 2003-07-05 03:43:44 +0000 | [diff] [blame] | 79 | int main(int argc, char **argv) |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 80 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 81 | int i, rc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 82 | const char *msg; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | union semun semunion; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 84 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | pthread_t pt[NUMTHREADS]; |
| 86 | pthread_attr_t attr; |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 87 | |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 88 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | /* Create the semaphore set */ |
| 91 | sem_id = semget(KEY, 1, 0666 | IPC_CREAT); |
| 92 | if (sem_id < 0) { |
| 93 | printf("semget failed, errno = %d\n", errno); |
| 94 | exit(1); |
| 95 | } |
| 96 | /* initialize data structure associated to the semaphore */ |
| 97 | semunion.val = 1; |
| 98 | semctl(sem_id, 0, SETVAL, semunion); |
robbiew | 2245234 | 2003-02-27 17:03:52 +0000 | [diff] [blame] | 99 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | /* setup the attributes of the thread */ |
| 101 | /* set the scope to be system to make sure the threads compete on a */ |
| 102 | /* global scale for cpu */ |
| 103 | pthread_attr_init(&attr); |
| 104 | pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 105 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | err_ret = 1; /* Set initial error value to 1 */ |
| 107 | /* Create the threads */ |
| 108 | for (i = 0; i < NUMTHREADS; i++) { |
| 109 | if (i == 0) |
| 110 | rc = pthread_create(&pt[i], &attr, waiter, retval[i]); |
| 111 | else |
| 112 | rc = pthread_create(&pt[i], &attr, poster, retval[i]); |
| 113 | } |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 114 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 115 | /* Sleep long enough to see that the other threads do what they are supposed to do */ |
| 116 | sleep(20); |
| 117 | semunion.val = 1; |
| 118 | semctl(sem_id, 0, IPC_RMID, semunion); |
| 119 | if (err_ret == 1) |
| 120 | tst_resm(TFAIL, "failed"); |
| 121 | else |
| 122 | tst_resm(TPASS, "passed"); |
| 123 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 124 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | tst_exit(); |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 126 | } |
| 127 | |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 128 | /* This thread sleeps 10 seconds then waits on the semaphore. As long |
| 129 | as someone has posted on the semaphore, and no undo has taken |
| 130 | place, the semop should complete and we'll print "Waiter done |
| 131 | waiting." */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | void *waiter(void *foo) |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 133 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | int pid; |
| 135 | pid = getpid(); |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 136 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | tst_resm(TINFO, "Waiter, pid = %d", pid); |
| 138 | sleep(10); |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 139 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | tst_resm(TINFO, "Waiter waiting, pid = %d", pid); |
| 141 | semop(sem_id, &Psembuf, 1); |
| 142 | tst_resm(TINFO, "Waiter done waiting"); |
| 143 | err_ret = 0; /* If the message above is displayed, the test is a PASS */ |
| 144 | pthread_exit(0); |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* This thread immediately posts on the semaphore and then immediately |
| 148 | exits. If the *thread* exits, the undo should not happen, and the |
| 149 | waiter thread which will start waiting on it in 10 seconds, should |
| 150 | still get it. */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | void *poster(void *foo) |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 152 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | int pid; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | pid = getpid(); |
| 156 | tst_resm(TINFO, "Poster, pid = %d, posting", pid); |
| 157 | semop(sem_id, &Vsembuf, 1); |
| 158 | tst_resm(TINFO, "Poster posted"); |
| 159 | tst_resm(TINFO, "Poster exiting"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 160 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | pthread_exit(0); |
robbiew | 95ae8f6 | 2002-03-06 18:24:32 +0000 | [diff] [blame] | 162 | } |
plars | 5c34fa3 | 2003-07-05 03:43:44 +0000 | [diff] [blame] | 163 | |
| 164 | void cleanup(void) |
| 165 | { |
Garrett Cooper | b0a973a | 2011-01-21 01:15:32 -0800 | [diff] [blame] | 166 | } |