<commit_before>/*
 * Copyright (c) 2015 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/wait.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int	nserver = 1;
int	nclient = 2;

struct child {
	pid_t	c_pid;
	void	(*c_func)(void);
	char	*c_name;
} *children;

void	server(void);
void	client(void);

int
main(int argc, char *argv[])
{
	struct child *c;
	pid_t pid;
	int i, status;

	children = calloc(nserver + nclient + 1, sizeof(struct child));
	if (children == NULL)
		err(1, "calloc");

	for (c = children, i = 0; i < nserver; c++, i++) {
		c->c_func = server;
		asprintf(&c->c_name, "server %d", i);
	}
	for (i = 0; i < nclient; c++, i++) {
		c->c_func = client;
		asprintf(&c->c_name, "client %d", i);
	}

	for (c = children; c->c_func; c++) {
		pid = fork();
		switch (pid) {
		case -1:
			err(1, "fork");
		case 0:
			setproctitle("%s", c->c_name);
			c->c_func();
			_exit(0);
		default:
			c->c_pid = pid;
		}
	}

	while (1) {
		pid = wait(&status);
		if (pid == -1)
			err(1, "wait");
		for (c = children; c->c_func; c++) {
			if (c->c_pid == pid)
				break;
		}
		if (c->c_pid != pid)
			err(1, "pid %d", pid);
		if (status != 0)
			warnx("pid %d, status %d", pid, status);
		pid = fork();
		switch (pid) {
		case -1:
			err(1, "fork");
		case 0:
			setproctitle("%s", c->c_name);
			c->c_func();
			_exit(0);
		default:
			c->c_pid = pid;
		}
	}

	return (0);
}

void
server(void)
{
	sleep(2);
}

void
client(void)
{
	sleep(1);
}<commit_msg>Bind server socket.<commit_after>/*
 * Copyright (c) 2015 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int	nserver = 1;
int	nclient = 2;
const	char *sockname = "sock";

struct child {
	pid_t	c_pid;
	void	(*c_func)(const char *);
	char	*c_name;
} *children;

void	server(const char *);
void	client(const char *);

int
main(int argc, char *argv[])
{
	struct child *c;
	pid_t pid;
	int i, status;

	children = calloc(nserver + nclient + 1, sizeof(struct child));
	if (children == NULL)
		err(1, "calloc");

	for (c = children, i = 0; i < nserver; c++, i++) {
		c->c_func = server;
		asprintf(&c->c_name, "server-%d", i);
	}
	for (i = 0; i < nclient; c++, i++) {
		c->c_func = client;
		asprintf(&c->c_name, "client-%d", i);
	}

	for (c = children; c->c_func; c++) {
		pid = fork();
		switch (pid) {
		case -1:
			err(1, "fork");
		case 0:
			setproctitle("%s", c->c_name);
			c->c_func(c->c_name);
			_exit(0);
		default:
			c->c_pid = pid;
		}
	}

	while (1) {
		pid = wait(&status);
		if (pid == -1)
			err(1, "wait");
		for (c = children; c->c_func; c++) {
			if (c->c_pid == pid)
				break;
		}
		if (c->c_pid != pid)
			err(1, "pid %d", pid);
		if (status != 0)
			warnx("pid %d, status %d", pid, status);
		pid = fork();
		switch (pid) {
		case -1:
			err(1, "fork");
		case 0:
			setproctitle("%s", c->c_name);
			c->c_func(c->c_name);
			_exit(0);
		default:
			c->c_pid = pid;
		}
	}

	return (0);
}

void
server(const char *name)
{
	char buf[1024];
	struct sockaddr_un sun;
	socklen_t sunlen;
	ssize_t n;
	int s;

 redo:
	if ((s = socket(PF_UNIX, SOCK_DGRAM, 0)) == -1)
		err(1, "server socket");
	sun.sun_len = sizeof(sun);
	sun.sun_family = AF_UNIX;
	snprintf(sun.sun_path, sizeof(sun.sun_path), "%s-%s", sockname, name);
	sunlen = sizeof(sun);
	unlink(sun.sun_path);
	if (bind(s, (struct sockaddr *)&sun, sunlen) == -1)
		err(1, "server bind");
	while (1) {
		switch (arc4random_uniform(100)) {
			case 0:
				if (close(s) == -1)
					err(1, "server close");
				goto redo;
			case 1:
				_exit(0);
			default:
				if ((n = recv(s, buf, sizeof(buf), 0)) == -1)
					err(1, "server recv");
				warnx("server recv %zd", n);
		}
	}
}

void
client(const char *name)
{
	sleep(1);
}
{'fraction_non_alphanumeric': 0.1644250805076719, 'fraction_numerical': 0.01382837658647471, 'mean_word_length': 3.3890274314214466, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7157317', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2027, 'n_words': 651}