I am trying to execute 16 tasks assigned one by one to each element of an array called tasks_ready_master
. I want to avoid using OMP Tasking.
I thought about using the fork/wait method.
!$OMP SINGLE
n=16
do ff =1,n
pid=c_fork()
if (pid < 0) then
call perror('fork()' // c_null_char)
else if (pid == 0) then
call tasks_ready_master(ff+4)%f_ptr(self,var)
call exit(STATUS)
else
pid=c_wait(STATUS)
call sleep(50)
end if
end do
!$OMP END SINGLE
I referred to the C code I found on Multiple child process.
For the unix
module, I followed the steps on https://cyber.dabamos.de/programming/modernfortran/fork.html.
The idea is to create 16 child processes and each process will execute a task.
From what I understood, the code under else if (pid == 0) then
is executed by a child process.
I don't understand the c_wait
function and is it necessary to use the sleep
function here ?
For me the part under else
(parent process) is useless. Is it important ? Without it, It shows me:
fork(): Resource temporarily unavailable
Sorry but there isn't no enough documentation on fork/wait in Fortran.
(I will tag C since one of the links I posted (in my question) contains a C code and the functions used are from C.)
Thanks to the comments, I have:
n=3
do ff =1,n
pid(ff)=c_fork()
if (pid(ff) < 0) then
call perror('fork()' // c_null_char)
else if (pid(ff) == 0) then
call tasks_ready_master(ff+4)%f_ptr(self,var)
call exit(STATUS)
end if
end do
do ff=1,n
pid(ff)=c_wait(STATUS)
end do
I consider pid
as an array of 16 integer elements.