I have to write ac program (linux) to decide if a value is present in an array. The way I am supposed to do it is by using parallel processes (fork ). It has to be done in a divide an conquer kind of way, by splitting the array in 2 halfs, then there should pe a child process for each half. A process should return 1 if the value is found and i have to check each child until the last one. Then i have to return a sum of those returned values.
Can anyone explain me how to do this ? I am not familiar with the this whole fork thing.(Code is also helpful.) Thanks !!
in general, what is needed is:
main()
{
pid1 = fork()
if( 0 > pid1 )
then handle error and exit
if( 0 == pid1 )
then child
exit( searchHalf(ptrToFirstHalf, firstHalfLength ) )
else 0 < pid1, so parent
pid2 = fork()
if( 0 > pid2 )
then handle error and exit
if( 0 == pid2 )
then child
exit( searchHalf( ptrToSecondHalf, secondHalfLength ) )
else 0 < pid2, so parent
endif
endif
-- only parent will get here --
waitpid( pid1, &status1, 0 ) // gets returnValue from child1
waitpid( pid2, &status2, 0 ) // gets returnValue from child2
printf ... status1+status2
return
} // end function: main
search( ptrTohalf, halfLength )
{
..performSearch..
return( returnValue )
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.