* [LTP] [PATCH] posix/conformance/interfaces/sem_timedwait/2-1: Fix test
@ 2024-01-08 13:50 chenguanxi11234
2024-01-09 12:00 ` Cyril Hrubis
0 siblings, 1 reply; 2+ messages in thread
From: chenguanxi11234 @ 2024-01-08 13:50 UTC (permalink / raw)
To: ltp; +Cc: yang.guang5, Chen Haonan
From: Chen Haonan <chen.haonan2@zte.com.cn>
Since the parent and child processes are not operating on the
same semaphore, this code wasn't doing its job correctly before,
so we mapped the semaphore to a piece of shared memory and changed
some implementation details in the original code to make it work.
Signed-off-by: Chen Haonan <chen.haonan2@zte.com.cn>
---
.../interfaces/sem_timedwait/2-1.c | 41 ++++++++++++++-----
1 file changed, 30 insertions(+), 11 deletions(-)
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c
index 655e35108..3082140e3 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c
@@ -10,13 +10,24 @@
* will unlock the semaphore from another process.
*/
+/* Modifications by: Chen Haonan <chen.haonan2@zte.com.cn>
+ * Date: 2024-01-08
+ *
+ * Since the parent and child processes are not operating on
+ * the same semaphore, this code wasn't doing its job correctly
+ * before, so we mapped the semaphore to a piece of shared
+ * memory and changed some implementation details in the
+ * original code to make it work.
+ */
+#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/wait.h>
+#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
@@ -28,12 +39,16 @@
int main(void)
{
- sem_t mysemp;
+ sem_t *mysemp;
struct timespec ts;
int pid;
- /* Semaphore started out locked */
- if (sem_init(&mysemp, 0, 0) == -1) {
+ /* Map semaphore to shared memory */
+ int shm_fd = shm_open("/myshm", O_CREAT | O_RDWR, 0666);
+
+ ftruncate(shm_fd, sizeof(sem_t));
+ mysemp = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
+
+ if (sem_init(mysemp, 1, 0) == -1) {
perror(ERROR_PREFIX "sem_init");
return PTS_UNRESOLVED;
}
@@ -43,20 +58,22 @@ int main(void)
{
ts.tv_sec = time(NULL) + 2;
ts.tv_nsec = 0;
-
- if (sem_timedwait(&mysemp, &ts) == -1) {
+ if (sem_timedwait(mysemp, &ts) == -1) {
puts("TEST FAILED");
+ sem_destroy(mysemp);
+ munmap(mysemp, sizeof(sem_t));
+ close(shm_fd);
+ shm_unlink("/myshm");
return PTS_FAIL;
} else {
- puts("TEST PASSED");
- sem_destroy(&mysemp);
+ puts("TEST PASSED");
return PTS_PASS;
}
} else if (pid > 0) // parent to unlock semaphore
{
int i;
sleep(1);
- if (sem_post(&mysemp) == -1) {
+ if (sem_post(mysemp) == -1) {
perror(ERROR_PREFIX "sem_post");
return PTS_FAIL;
}
@@ -64,12 +81,14 @@ int main(void)
perror("Error waiting for child to exit");
return PTS_UNRESOLVED;
}
-
- if (!WEXITSTATUS(i)) {
+ if (WEXITSTATUS(i)) {
return PTS_FAIL;
}
puts("TEST PASSED");
- sem_destroy(&mysemp);
+ sem_destroy(mysemp);
+ munmap(mysemp, sizeof(sem_t));
+ close(shm_fd);
+ shm_unlink("/myshm");
return PTS_PASS;
}
return PTS_UNRESOLVED;
--
2.25.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [LTP] [PATCH] posix/conformance/interfaces/sem_timedwait/2-1: Fix test
2024-01-08 13:50 [LTP] [PATCH] posix/conformance/interfaces/sem_timedwait/2-1: Fix test chenguanxi11234
@ 2024-01-09 12:00 ` Cyril Hrubis
0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2024-01-09 12:00 UTC (permalink / raw)
To: chenguanxi11234; +Cc: yang.guang5, ltp, Chen Haonan
Hi!
> +/* Modifications by: Chen Haonan <chen.haonan2@zte.com.cn>
> + * Date: 2024-01-08
> + *
> + * Since the parent and child processes are not operating on
> + * the same semaphore, this code wasn't doing its job correctly
> + * before, so we mapped the semaphore to a piece of shared
> + * memory and changed some implementation details in the
> + * original code to make it work.
> + */
Please do not duplicate the commit message in the actual code.
> +#include <stdlib.h>
> #include <stdio.h>
> #include <errno.h>
> #include <unistd.h>
> #include <semaphore.h>
> #include <sys/stat.h>
> #include <sys/wait.h>
> +#include <sys/mman.h>
> #include <fcntl.h>
> #include <signal.h>
> #include <time.h>
> @@ -28,12 +39,16 @@
>
> int main(void)
> {
> - sem_t mysemp;
> + sem_t *mysemp;
> struct timespec ts;
> int pid;
>
> - /* Semaphore started out locked */
> - if (sem_init(&mysemp, 0, 0) == -1) {
> + /* Map semaphore to shared memory */
> + int shm_fd = shm_open("/myshm", O_CREAT | O_RDWR, 0666);
> _
> + ftruncate(shm_fd, sizeof(sem_t));
> + mysemp = (sem_t *)mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
We do not have to have a shm object for a shared memory between a parent
and a child process, for that we can just map anonymous shared memory.
> + if (sem_init(mysemp, 1, 0) == -1) {
> perror(ERROR_PREFIX "sem_init");
> return PTS_UNRESOLVED;
> }
> @@ -43,20 +58,22 @@ int main(void)
> {
> ts.tv_sec = time(NULL) + 2;
> ts.tv_nsec = 0;
> -
> - if (sem_timedwait(&mysemp, &ts) == -1) {
> + if (sem_timedwait(mysemp, &ts) == -1) {
> puts("TEST FAILED");
> + sem_destroy(mysemp);
> + munmap(mysemp, sizeof(sem_t));
> + close(shm_fd);
> + shm_unlink("/myshm");
> return PTS_FAIL;
> } else {
> - puts("TEST PASSED");
> - sem_destroy(&mysemp);
> + puts("TEST PASSED");
> return PTS_PASS;
> }
> } else if (pid > 0) // parent to unlock semaphore
> {
> int i;
> sleep(1);
> - if (sem_post(&mysemp) == -1) {
> + if (sem_post(mysemp) == -1) {
> perror(ERROR_PREFIX "sem_post");
> return PTS_FAIL;
> }
> @@ -64,12 +81,14 @@ int main(void)
> perror("Error waiting for child to exit");
> return PTS_UNRESOLVED;
> }
> -
> - if (!WEXITSTATUS(i)) {
> + if (WEXITSTATUS(i)) {
> return PTS_FAIL;
> }
> puts("TEST PASSED");
> - sem_destroy(&mysemp);
> + sem_destroy(mysemp);
> + munmap(mysemp, sizeof(sem_t));
> + close(shm_fd);
> + shm_unlink("/myshm");
> return PTS_PASS;
> }
> return PTS_UNRESOLVED;
> --
> 2.25.1
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-01-09 11:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-08 13:50 [LTP] [PATCH] posix/conformance/interfaces/sem_timedwait/2-1: Fix test chenguanxi11234
2024-01-09 12:00 ` Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox