c - Why doesn't Linux prevent spawning infinite number of processes and crashing? -
with simple code below, system (ubuntu linux 14.04) crashes not letting mouse respond. had force quit power button. thought linux stable os tolerable of handling such basic program errors. did miss something?
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <semaphore.h> void check(int isokay){ if(!isokay){ printf("error\n"); abort(); } } int main(void){ #define n 1000000 int array[n]; sem_t blocker; int i; while(1){ if(!fork()){ for(i = 0; < n; ++i){ array[i] = rand(); } check(sem_init(&blocker, 0, 0) == 0); check(sem_wait(&blocker) == 0); } } return 0; }
congratulations, you've discovered fork bomb. there shell one-liners can wreak same sort of havic lot less typing on part.
it in fact possible limit number of processes user can spawn using ulimit -- see bottom of linked wikipedia articles details.
a desktop install of ubuntu not hardened server, though. it's designed usability first , foremost. if need locked down system can't crash, there better options.
Comments
Post a Comment