xref: /freebsd/tools/test/stress2/misc/kcmp.sh (revision ef777be98543f7daae90bd123d4fc1ec4a54efc2)
1#!/bin/sh
2
3#
4# Copyright (c) 2025 Peter Holm <pho@FreeBSD.org>
5#
6# SPDX-License-Identifier: BSD-2-Clause
7#
8
9# Seen:
10# UID  PID PPID  C PRI NI   VSZ  RSS MWCHAN   STAT TT     TIME COMMAND
11#   0 3730 3668 11  20  0 13596 2904 exithold DE+   0  1:59.68 ./kcmp
12
13# Fixed by: 5b3e5c6ce3e5
14
15. ../default.cfg
16
17set -u
18prog=$(basename "$0" .sh)
19cat > /tmp/$prog.c <<EOF
20#include <sys/types.h>
21
22#include <err.h>
23#include <fcntl.h>
24#include <pthread.h>
25#include <signal.h>
26#include <stdlib.h>
27#include <time.h>
28#include <unistd.h>
29
30static void *
31t1(void *data __unused)
32{
33	for (;;)
34		pause();
35
36	return (NULL);
37}
38
39int
40main(void)
41{
42	pid_t p1, p2;
43	pthread_t tid[2];
44	time_t start;
45	uintptr_t idx1, idx2;
46	int r;
47
48	if ((r = pthread_create(&tid[0], NULL, t1, NULL)) != 0)
49		errc(1, r, "pthread_create");
50	if ((r = pthread_create(&tid[1], NULL, t1, NULL)) != 0)
51		errc(1, r, "pthread_create");
52
53	start = time(NULL);
54	while (time(NULL) - start < 60) {
55		idx1 = idx2 = 0;
56		p1 = arc4random() % 1000000;
57		p2 = arc4random() % 1000000;
58		kcmp(p1, p2, KCMP_VM, idx1, idx2);
59	}
60}
61EOF
62mycc -o /tmp/$prog -Wall -Wextra -O0 /tmp/$prog.c -lpthread || exit 1
63
64/tmp/$prog
65
66rm /tmp/$prog.c /tmp/$prog
67exit 0
68