Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

c - fork x number of processes


#include 
#include 
#include 
#include 

void forkChildren (int nChildren) {
    int i;
    pid_t pid;
    for (i = 1; i <= nChildren; i++) {
        pid = fork();
        if (pid == -1) {
            /* error handling here, if needed */
            return;
        }
        if (pid == 0) {
            printf("I am a child: %d PID: %d\n",i, getpid());
            // we could use sisgsuspend instead of pause, due 
            // to a race condition in pause where the signal
            // could be delivered just before the call to pause
            // sleep (5);
            pause();
            return;
        }
    }
}

int main (int argc, char *argv[]) {
    if (argc < 2) {
        forkChildren (2);
    } else {
        forkChildren (atoi (argv[1]));
    }
    return 0;
}

c - generate high cpu utilization

It'd be nice if I can pass the number of processors I want to use. I'll figure it out when I'm done reviewing for RHCVA.

#include 

int main() {
    while(1){}
}