Fork bomb in a while loop in C, that occurs after exiting a program -



Fork bomb in a while loop in C, that occurs after exiting a program -

`#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv){ int pid = 0; int forever; static char s; //uses s in while loop, press s 1 time each process //terminate each process //char *s; can't work because points start of char array. //i don't know why. int * doesn't that. pid = fork(); if(pid>0)// kid cannot see it's own process id pid 0 if kid printf("this parent.\n"); else if(pid == 0) printf("this child.\n"); while(s != 's'){//one quote character, 2 quotes char array forever = fork(); if(forever>0) printf("parent process\n"); else printf("child process\n"); s = getchar();//i tried making *s causes segmentation fault } homecoming 0; }`

this causing problem. when execute programme terminal, can exit programme entering 's' 1 time each process, , terminal goes normal. or appears. when press "up" arrow when in terminal, go endless loop , fork bomb. how avoid this?

change s to

int s = 0;

because getchar returns int , need it. no point making static (no harm either here, bad/unusual style). note need initialization, unlike when static.

then check getchar homecoming value error/eof, <0 , terminate on also. should prepare fork bomb, because caused getchar starting homecoming error. add together prints see happening, print error message or eof status.

also should check fork homecoming value errors well, in case.

c loops while-loop fork

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -