Hey I was wondering if someone could help me figure out why my implementation of Peterson's Algo isn't working.
Currently my output is:
From Process total = [number > 100,000]
Child with ID -1 has just exited
Child with ID [Child ID] has just exited
End of Program
From Process 2 total = [number > 200,000]
Child with ID -1 has just exited
My output should be along the lines of:
From Process 1: total = 60234.
From Process 2: total = 100000.
Child with ID 2412 has just exited.
Child with ID 2411 has just exited.
End of Program.
For some reason one of my child processes has an ID of -1 which means there is some sort of error (I think). I have no idea what's going on with that. I'm still confused how this algorithm ever works because if turn never changes, how does the process ever get access to the critical section?
My code is as follows:
#define TRUE 1
#define FALSE 0
typedef struct
{
int value;
int flag[2] = {FALSE, FALSE};
int turn = 0;
} shared_mem;
shared_mem *total;
void process1 ()
{
int k = 0;
while (k < 100000)
{
total->flag[0] = TRUE;
total->turn = 1;
//prevent P1 from access CS
while(total->flag[1] == TRUE && total->turn == 1)
{
int k = 0;
if(k = 0)
{
total->p2Block = total->p2Block + 1;
}
k++;
};
k++;
total->value = total->value + 1;
total->flag[0] = FALSE;
}
printf("Process 2 interrupted %d times in critical section by Process 1.\n", total->p2Block);
printf ("From process1 total = %d\n", total->value);
}
void process2 ()
{
int k = 0;
while (k < 200000)
{
total->flag[1] = TRUE;
total->turn = 0;
int critical;
//prevent P2 from accessing CS
while(total->flag[0] == TRUE && total->turn == 0)
{
int k = 0;
if(k = 0)
{
total->p2Block = total->p1Block + 1;
}
k++;
};
k++;
total->value = total->value + 1;
critical++;
total->flag[1] = FALSE;
}
printf("Process 1 interrupted %d times in critical section by Process 2.\n", total->p1Block);
printf("From process2 total = %d\n", total->value);
}
main()
{
int shmid;
int pid1;
int pid2;
int ID;
int status;
int waitID1, waitID2;
char *shmadd;
shmadd = (char *) 0;
/* Create and connect to a shared memory segmentt*/
if ((shmid = shmget (SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0)
{
perror ("shmget");
exit (1);
}
if ((total = (shared_mem *) shmat (shmid, shmadd, 0)) == (shared_mem *) -1)
{
perror ("shmat");
exit (0);
}
total->value = 0;
total->flag[0] = FALSE;
total->flag[1] = FALSE;
total->turn = 0;
total->p1Block = 0;
total->p2Block = 0;
if ((pid1 = fork()) == 0)
process1();
if ((pid1 != 0) && (pid2 = fork()) == 0)
process2();
do{
waitID1 = waitpid(pid1, &status, 0);
waitID2 = waitpid(pid2, &status, 0);
if(WIFEXITED(status) && waitID1 != -1)
{
printf("Child with ID %d has just exited\n", waitID1);
}
if(WIFEXITED(status) && waitID2 != -1)
{
printf("Child with ID %d has just exited\n", waitID2);
}
}while (!WIFEXITED(status) && !WIFSIGNALED(status));
if ((pid1 != 0) && (pid2 != 0))
{
if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
{
perror ("shmctl");
exit (-1);
}
printf ("\t\t End of Program.\n");
}
}