linux - Diposing a child process created with fork() in C -
my c homework programme multiplies 2 matrices. each entry of product matrix calculated kid process created fork()
. kid calculates , sends info parent using pipe. if product matrix has size 10x10, create 100 kid processes. works fine.
then noticed big matrices, not working. after doing inspection, realized it's because won't create more kid processes after number (the pid
returns negative). like, sort of limit, makes sense.
indeed, can't expect computer allow programme spawn several thousands of kid processes, programme can't multiply super big matrices. alright.
then occurred me: well, don't need all kid processes immediately. create 100, allow them thing, , create next 100, , on until necessary calculations done matrix product.
my programme loop iterates rows * columns
times. each iteration, makes kid process. decided that, every 100 iterations, set sleep()
thing. hope when sleep()
thing done, 100 previous kid processes die out, "freeing" space necessary next 100 batch. alas, did not create difference: programme behaves same (except slower, of course).
so, given sleep()
thing did not work, suspicion not "killing" kid processes. how kid process dies:
// close pipe! close(fd[0]); close(fd[1]); // exit exit(0);
and parent, after reading data, closes pipe:
read(fd[0], buffer, sizeof(buffer)); close(fd[0]); close(fd[1]);
so question is: since unable create new kid processes because have created many, seems not disposing of old processes. how can dispose them correctly?
what sleep(3)
putting process temporarily sleep, still keeps alive.
as stated, need create sure children-process dead before creating more. that, children need exit(2)
when finish job, , parent process should utilize wait(2)
on them in order reap them, , create space in process table.
a possible solution fork(2)
children processes part of job, wait(2)
them finish before fork
ing more of them other part of job.
c linux process fork
No comments:
Post a Comment