Search code examples
clinuxforkwritetofile

Record and write processes onto a file using C (linux)


Can someone help me with this small problem please? I am trying to create a fork bomb and record how many processes a computer creates before something weird happens, but I can't figure out how to write the processes into a text file. I looked around the internet and couldn't find a solution for my problem and I tried doing different things but that didn't work with me also. If you guys can help me out on this matter that would be awesome! Here's my code so far (far from perfect, I know.. D: ).

    while(1)
    {
        fork(); // Fork bomb
        write(fd, '\n', sizeof(buf));
    }

Thanks in advance


Solution

  • Open a file for write+append.

    Each forked process will inherit the file descriptor.

    In each forked child process, write a single null byte to the file descriptor.

    When everything crashes, the size of the file, in bytes, will tell you how many processes were started.