Path: blob/main/tools/test/pthread_vfork/pthread_vfork_test.c
39534 views
/*-1* Copyright (c) 2008 Ganbold Tsagaankhuu2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*26*/2728#include <sys/types.h>29#include <sys/wait.h>30#include <err.h>31#include <pthread.h>32#include <signal.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>36#include <unistd.h>3738#define NUM_THREADS 1003940static void *41vfork_test(void *threadid __unused)42{43pid_t pid, wpid;44int status;4546for (;;) {47pid = vfork();48if (pid == 0)49_exit(0);50else if (pid == -1)51err(1, "Failed to vfork");52else {53wpid = waitpid(pid, &status, 0);54if (wpid == -1)55err(1, "waitpid");56}57}58return (NULL);59}6061static void62sighandler(int signo __unused)63{64}6566/*67* This program invokes multiple threads and each thread calls68* vfork() system call.69*/70int71main(void)72{73pthread_t threads[NUM_THREADS];74struct sigaction reapchildren;75sigset_t sigchld_mask;76int rc, t;7778memset(&reapchildren, 0, sizeof(reapchildren));79reapchildren.sa_handler = sighandler;80if (sigaction(SIGCHLD, &reapchildren, NULL) == -1)81err(1, "Could not sigaction(SIGCHLD)");8283sigemptyset(&sigchld_mask);84sigaddset(&sigchld_mask, SIGCHLD);85if (sigprocmask(SIG_BLOCK, &sigchld_mask, NULL) == -1)86err(1, "sigprocmask");8788for (t = 0; t < NUM_THREADS; t++) {89rc = pthread_create(&threads[t], NULL, vfork_test, &t);90if (rc)91errc(1, rc, "pthread_create");92}93pause();94return (0);95}969798