4
I am creating a sequence of processes through the command fork
, but when I went to list the processes generated by the code, I found that there was a greater amount than I had created. Because of this?
In doing 10
forks
were generated 1024 lawsuits.
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, const char **argv){
int fork_id[10];
int x;
for(x=0; x<10; x++){
fork_id[x] = fork();
if(!fork_id){
execl("/usr/bin/sleep", "5", NULL);
}
}
sleep(10);
return 0;
}
To verify the amount of processes I used the ps
:
ps aux | grep test_fork | grep -v grep | wc -l
instead of this pipeline you could have used "pgrep -c test_fork" :)
– zentrunix
it’s a good idea tbm, I didn’t know how to use pgrep but I’ll look a little bit about it
– Brumazzi DB
after you learn about pgrep and pkill your problems are over :)
– zentrunix