Powered by Invision Power Board


  Reply to this topicStart new topicStart Poll

> fork() in nested loops?
posof
Posted: May 9 2005, 06:33 AM
Quote Post


Newbie
*

Group: Members
Posts: 2
Member No.: 980
Joined: 9-May 05



I am trying to simulate a parallel algorithm by creating processes.
Each process will be a processor and will try to multiply two matrices..

Anyway i want to cretae cube structure so that each process will have a different i,j and s values. But output is not as i wanted.

Here is my code and output:
int main(void)
{
int f=1,i,j,s;
int i2,j2,s2;
int processor[10][10][10];
int i3=1,j3=1,s3=1;

int m=2,n=2,k=2;

int processor_count=m*n*k;

signal(12,(void *) signal12);

for (i=1; i<=m; i++)
{
for(j=1; j<=k; j++)
{
for(s=1; s<=n; s++)
{
if (f>0)
f=fork();
if (f==-1)
{
printf("fork error....\n");
exit(1);
}
if (f==0)
break;
else
{
processor[i][j][s]=f;
i3=i;
j3=j;
s3=s;
printf("I am [%d][%d][%d] \n",i3,j3,s3);
}
}
}
}
if (f>0) /*mother */
{
sonsem=semget(SEMKEY, 1, 0700|IPC_CREAT);
semctl(sonsem, 0, SETVAL,0);

sleep(1);

for (i2=1; i2<=m; i2++)
for (j2=1; j2<=k; j2++)
for(s2=1; s2<=k; s2++)
kill(processor[i2][j2][s2], 12);

sem_wait(sonsem,processor_count);

printf("All child processes killed\n");
semctl(sonsem,0,IPC_RMID,0);
}
else /*cocuk */
{
pause();
sonsem=semget(SEMKEY, 1,0);
printf("I am [%d][%d][%d] \n",i3,j3,s3);
sem_signal(sonsem,1);
}
return(0);
}

Output:
I am [1][1][1] These are written by mother process while assigning the values.
I am [1][1][2]
I am [1][2][1]
I am [1][2][2]
I am [2][1][1]
I am [2][1][2]
I am [2][2][1]
I am [2][2][2]

I am [1][1][1] This line printed twice?
I am [1][1][1]
I am [1][1][2]
I am [1][2][1]
I am [1][2][2]
I am [2][1][1]
I am [2][1][2]
I am [2][2][1]

It does not print I am [2][2][2]?Why first two processes has same i,j and s value?

PS i have printed pid for each process.It cretaes 8 processes with different IDs.

Can anybody see what am i doing wrong?
Thanx for any help
PMEmail Poster
Top
posof
Posted: May 9 2005, 01:16 PM
Quote Post


Newbie
*

Group: Members
Posts: 2
Member No.: 980
Joined: 9-May 05



i have solved the problem..thanx a lot :)
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
« Next Oldest | Programming/Languages | Next Newest »

Topic Options Reply to this topicStart new topicStart Poll

 



[ Script Execution time: 0.0847 ]   [ 12 queries used ]   [ GZIP Enabled ]




Partners: Cambridge Plus :: <Link Available> :: Electronic Circuit Design :: <Link Available>
Unix Man Pages / Linux Man Pages :: HiFi Forum :: SIP VoIP Phone & Provider Reviews :: UNIX/Linux Forum Archives

More info on advertising on Unix/Linux Forum