The scrap-is-not-scrap (sins) experiment began out of a public school education in biology and dissatisfaction with genetic algorithms with their ability to do work.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

123 lines
2.3 KiB

/*
@file generation.c
*/
#include <arpa/inet.h>
#include <errno.h>
#include <malloc.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>
#define MAX_LEN 1024
#define PORT 5000
int reproduce(void *pic_address, int pic_size)
{
int status = 0;
printf("{\"address\":\"%p\",\"size\":\"%d\"}\n", pic_address, pic_size);
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
int sock = socket(AF_INET, SOCK_STREAM, 0);
status = connect(sock, (struct sockaddr *)&server, sizeof(server));
status = send(sock, pic_address, pic_size, 0);
status = 1;
CLONE_CLEANUP:
if (sock)
{
close(sock);
}
return status;
}
int generation(void)
{
int status = 0;
char reply[MAX_LEN];
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
int sock = socket(AF_INET, SOCK_STREAM, 0);
printf("connecting\n");
status = connect(sock, (struct sockaddr *)&server, sizeof(server));
status = recv(sock, reply, MAX_LEN, 0);
int child_len = (int)reply;
int prot = (PROT_READ | PROT_WRITE | PROT_EXEC);
int flags = (MAP_ANON | MAP_PRIVATE);
void *pic_buffer = mmap(NULL, child_len, prot, flags, -1, 0);
if (MAP_FAILED == pic_buffer)
{
status = errno;
goto GEN_CLEANUP;
}
int iter = child_len;
while (1)
{
status = recv(sock, reply, MAX_LEN, 0);
if (!status)
{
break;
}
if ((status + iter) > child_len)
{
break;
}
memcpy(&pic_buffer[iter - child_len], reply, status);
iter -= status;
}
int (*reproduce_function)(void *, int) = reproduce;
void (*pic_function)(void *, int, void *) = pic_buffer;
pic_function(pic_buffer, child_len, reproduce_function);
status = 1;
GEN_CLEANUP:
if (sock)
{
close(sock);
}
return status;
}
int main(int argc, const char **argv)
{
pid_t process_id;
int status = 0;
process_id = fork();
if (0 == process_id)
{
status = generation();
if (status)
{
return 1;
}
}
return 0;
}