Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

weekly challenge

Name: Anonymous 2010-09-06 9:09


1: write a program that consumes all memory resources available [u]as quick as possible[/u].
2: write a simple garbage collector, that tries to free all consumed resources.

implement your submissions using the C programming language.
deadline is due thread-start-time + 100 hours.

good luck.

Name: Anonymous 2010-09-06 9:15

Fuck off, ``hotaru''.

Name: Anonymous 2010-09-06 9:40

I'd like to provide following flowchart:


->->->->malloc->->->->|
^                     v
|                     |
^                     v
|<-<-<-<-free<-<-<-<-<-

Name: Anonymous 2010-09-06 9:46

>>3
Invalid UML.

Name: Anonymous 2010-09-06 9:51

#include <stdlib.h>
void GC_Setup (void);
int main (void) {
 GC_Setup();
 while (fork()) malloc (1024*1024*1024);
 return 0;
}

void GC_Setup (void) {
free (NULL);
}

Name: Anonymous 2010-09-06 10:09

>>1
write a program that consumes all memory resources available [bbcodefailure]as quick as possible[/bbcodefailure]
as quickly, pleb.

Name: Anonymous 2010-09-06 10:23

1. open ikarus and type #0=(#0#) and wait
2. kill it with kill

Name: Anonymous 2010-09-06 12:35

>>1


void memory_consumer(void) {
    for (;;)
        while (!sbrk(1))
            ;
}

void gargabe_collector(void) {
    for (;;)
        while (!sbrk(-1))
            ;


piece of cake

Name: Anonymous 2010-09-06 12:42

>>5
I ran your program and my system stayed responsive.  The problem is that you think malloc works like it did on your old 286 running DOS.  It doesn't allocate physical RAM on any modern OS I know of.

Try running the following program.  It only works on Linux, and it does NOT require root.  It only opens one file, /dev/random, just to seed a few PRNGs.


#include <sys/mman.h>
#include <sys/sysinfo.h>
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

#define THREADS_PER_CPU 10
static unsigned char *thrash_ptr;
static size_t thrash_psize, thrash_pcount;
static pthread_mutex_t thrash_mutex;
static int thrash_random, thrash_active;

static void thrash(void)
{
    uint32_t x[2], a = 4294967118UL, v;
    uint64_t t;
    int r;
    size_t p;
    unsigned char *ptr = thrash_ptr;
    size_t psize = thrash_psize, pcount = thrash_pcount;
    r = read(thrash_random, &x, sizeof(x));
    if (r < 0)
        err(1, "/dev/random");
    if (r < (int)sizeof(x))
        errx(1, "Could not read enough random data");
    x[0] |= !x[0];
    x[1] |= !x[1];
    while (1) {
        t = (uint64_t)a * x[0] + x[1];
        x[0] = t;
        x[1] = t >> 32;
        p = (size_t)x[0] % pcount;
        v = (size_t)x[0] / pcount;
        ptr[p * psize] = v;
    }
}

static void *thrash_thread(void *p)
{
    (void)p;
    int r;
    r = pthread_mutex_lock(&thrash_mutex);
    if (r) errx(1, "pthread_mutex_lock");
    printf("Thread %i active\n", ++thrash_active);
    r = pthread_mutex_unlock(&thrash_mutex);
    if (r) errx(1, "pthread_mutex_unlock");
    thrash();
    return NULL;
}

static void countdown(void)
{
    int i;
    fputs("Last chance to stop...", stdout);
    fflush(stdout);
    sleep(1);
    for (i = 3; i > 0; --i) {
        printf(" %i...", i);
        fflush(stdout);
        sleep(1);
    }
    fputs(" 0\n", stdout);
}

int main(int argc, char *argv[])
{
    struct sysinfo sinfo;
    int r, fd;
    void *mem;
    size_t totalram, nthread, i;
    long pagesize, nproc;
    pthread_attr_t attr;
    pthread_t *thread;
    pthread_mutexattr_t mattr;
    (void)argc;
    (void)argv;

    /* Map memory */
    r = sysinfo(&sinfo);
    if (r)
        err(1, "sysinfo");
    if (sinfo.totalram > SIZE_MAX / sinfo.mem_unit)
        errx(1, "Too much ram, not enough address space");
    totalram = (size_t)sinfo.totalram * sinfo.mem_unit;
    pagesize = sysconf(_SC_PAGESIZE);
    if (pagesize <= 0)
        errx(1, "Can't get page size");
    printf("Page size: %li\n", pagesize);
    mem = mmap(NULL, totalram, PROT_READ | PROT_WRITE,
               MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
    if (mem == (void *)-1)
        err(1, "mmap");
    thrash_ptr = mem;
    thrash_psize = pagesize;
    thrash_pcount = totalram / (size_t)pagesize
        + ((totalram % pagesize) ? 1 : 0);

    /* Count CPUs */
    nproc = sysconf(_SC_NPROCESSORS_ONLN);
    if (nproc <= 1)
        nproc = 1;
    printf("Total RAM: %zu (%zu pages of %zu bytes)\n"
           "Number of CPUs: %li\n",
           totalram, thrash_pcount, thrash_psize, nproc);

    /* Open /dev/random for seeds */
    fd = open("/dev/random", O_RDONLY);
    if (fd < 0)
        err(1, "/dev/random");
    thrash_random = fd;

    /* Create threads */
    r = pthread_mutexattr_init(&mattr);
    if (r) goto onethread;
    r = pthread_mutex_init(&thrash_mutex, &mattr);
    if (r) goto onethread;
    r = pthread_mutex_lock(&thrash_mutex);
    if (r) goto onethread;
    nthread = (size_t)nproc * THREADS_PER_CPU;
    thread = malloc(sizeof(*thread) * nthread);
    if (!thread)
        err(1, "malloc");
    r = pthread_attr_init(&attr);
    if (r) goto onethread;
    for (i = 0; i < nthread; ++i) {
        r = pthread_create(&thread[i], &attr, thrash_thread, NULL);
        if (r) break;
    }
    nthread = i;
    countdown();
    r = pthread_mutex_unlock(&thrash_mutex);
    pause();
    return 0;
onethread:
    countdown();
    puts("Using only one thread");
    thrash();
    return 0;
}

Name: >>8 2010-09-06 12:42


}

Name: Anonymous 2010-09-06 13:41

>>9
I don't think you ran or even read my program.

Name: VIPPER 2010-09-06 14:07


#include <stdlib.h>

void main(void) {
  for(;;)
    free(malloc(4096));
}


BTW, how can one get the page size in C?

Name: Anonymous 2010-09-06 14:14

>>12

#include <unistd.h>

int main(void) {
    printf("%d\n", getpagesize());
    return 0;
}

Name: Anonymous 2010-09-06 14:19

>>13
also, sysconf(_SC_PAGE_SIZE);
which is pretty much the same thing, though.

Name: VIPPER 2010-09-06 14:37

>>13
>>14
ah, excelent.

Name: Anonymous 2010-09-06 14:40

>>15
Optimize your quotes, please!

Name: Anonymous 2010-09-06 18:39

>>5,11
Your program caps out at about 600 MB memory, 55 processes, 128 GB of address space, and 100% CPU utilization.  I'm running it right now, no noticeable slowdown.

Check it yourself, fork() is returning -1 and malloc is returning NULL.

Name: Anonymous 2010-09-06 23:01

That's what you get for not checking return values.

Name: Anonymous 2010-09-07 6:54

CHECK THE RETURN VALUE OF MY ANUS

Name: Anonymous 2010-09-10 18:00

Name: Anonymous 2010-09-10 20:10

>>20
You forgot your post.

Name: 2010-09-10 21:07

>>21
You forgot your sage.

Name: 2010-09-10 21:07

>>22
You forgot your name.

Name: Anonymous 2010-09-10 21:08

>>23
You too.

Name: Anonymous 2010-09-10 21:31

>>17

top - 12:10:16 up 715 days, 11:22,  5 users,  load average: 55.08, 50.17, 4.73
Tasks:  1173 total,  64 running,  1109 sleeping,  0 stopped,   0 zombie
Cpu(s): 99.9%us,  0.1%sy, 0.0%ni, 0.0%id
Mem:    8384512m total,   7127040m used,   1257472m free


I hope my uni won't notice this...

Name: Anonymous 2010-09-10 22:29

>>25
8384512m
IHBT

Name: Anonymous 2010-09-11 3:01

>>26
Apparently >>25 is using some sort of computer that has memory measured in meters.

Hopefully this thread won't turn into a -er vs. -re shitfest... But it probably will, because British people like to draw attention to their inability to spell words properly.

Name: Anonymous 2010-09-11 5:02

>>27
Ha ha ha.

Name: Anonymous 2010-09-11 11:34

>>27
British people like to draw attention to their inability to spell words properly.
Fuck your inferior bastard language. British English is the only true English there is. Now that I think about it, it's a great thing we got scum like you off our damned island.

Name: Anonymous 2010-09-11 13:41

>>29
Sure, our language is so inferior that we kicked your sorry asses in 1775-1783 and again in 1812-1815... And then had to save your asses from Germany in World War I and World War II.
You bastards are practically French.

Name: Anonymous 2010-09-11 14:32

Name: Anonymous 2010-09-11 14:33

Name: Anonymous 2010-09-11 14:34

Name: Anonymous 2010-09-11 14:36

I'm learning to sage!

Name: Anonymous 2010-09-11 21:34

>>30
American involvement in WW1 and 2 was self-serving, and in the former, was unnecessary. How does it feel to have never been a successful colonial power, even though you continue to try to this day?

Name: Anonymous 2010-09-12 1:18

you guys are faggots...

(its C++ code though)

#include <iostream>
#define thisprogram "programname.exe"
while(1)
{
    system(thisprogram);
}

Name: Anonymous 2010-09-12 3:45

>>36
Good job, I was almost trolled. (Though one could argue that by responding IHBT already)

Name: Anonymous 2010-09-13 5:02

./$0

Name: Anonymous 2011-02-03 0:53

Name: Anonymous 2011-02-04 13:54


Don't change these.
Name: Email:
Entire Thread Thread List