[LTP] [v5,2/2] semop04: Refactor with new API
Petr Vorel
pvorel@suse.cz
Wed Jun 21 12:25:06 CEST 2023
Hi Wei,
> -void delayloop()
> +static void delayloop(void)
> {
> int delay;
> +
> delay = 1 + ((100.0 * rand()) / RAND_MAX);
> - if (verbose)
> - printf("in delay function for %d microseconds\n", delay);
> usleep(delay);
> }
...
> + for (i = 0; i < LOOPS; i++) {
> + semdown(semid);
> delayloop();
Why not just:
usleep(1 + ((100.0 * rand()) / RAND_MAX));
and delete whole delayloop() function?
> - if (semup(semid)) {
> - printf("semup failed\n");
> - }
> - if (verbose)
> - printf("sem is up\n");
> + semup(semid);
> }
...
> /* set up the semaphore */
> - if ((semid = semget((key_t) 9142, 1, 0666 | IPC_CREAT)) < 0) {
> - printf("error in semget()\n");
> - exit(-1);
> - }
> + semid = SAFE_SEMGET((key_t) 9142, 1, 0666 | IPC_CREAT);
> +
> semunion.val = 1;
> - if (semctl(semid, 0, SETVAL, semunion) == -1) {
> - printf("error in semctl\n");
> - }
> - if ((pid = fork()) < 0) {
> - printf("fork error\n");
> - exit(-1);
> - }
> + SAFE_SEMCTL(semid, 0, SETVAL, semunion);
> +
> + pid = SAFE_FORK();
> +
> if (pid) {
> - /* parent */
> srand(pid);
> mainloop(semid);
> - waitpid(pid, &chstat, 0);
> - if (!WIFEXITED(chstat)) {
> - printf("child exited with status\n");
> - exit(-1);
> - }
> - if (semctl(semid, 0, IPC_RMID, semunion) == -1) {
> - printf("error in semctl\n");
> - }
> - if (errors) {
> - printf("FAIL: there were %d errors\n", errors);
> - } else {
> - printf("PASS: error count is 0\n");
> - }
> - exit(errors);
> + tst_reap_children();
> + SAFE_SEMCTL(semid, 0, IPC_RMID, semunion);
> + tst_res(TPASS, "Semaphore up/down check success");
Maybe here use plain semctl() verified with TST_EXP_POSITIVE() from
tst_test_macros.h? This way we'd get TFAIL on failure instead of TPASS.
Kind regards,
Petr
More information about the ltp
mailing list