I'm creating a parent-child process in C and these processes are using an array of characters as a shared memory and I want the execution to be in this sequence
родител->дете->родител->дете->родител->дете
.... and so on, i am usingWait(NULL)
in parent but the execution go in the sequence of родител->дете->родител->родител->родител ....
I am trying to do this without semaphores or any ting else am still a novice Linux programmerint main(void)
{
if (fork( ) == 0)
{ //child
if( (id = shmget(key, sizeof(char[n]), 0)) == -1 )
{
exit(1);
}
shm = shmat(id, 0, 0);
if (shm == (char *) -1)
exit(2);
.......................//some work
..........................
}
else //parent
{
if( (id = shmget(key, sizeof(char[n]), 0666 | IPC_CREAT)) == -1 )
{
exit(1);
}
shm = shmat(id, 0, 0); //attach shared memory to pointer
if (shm == (char *) -1)
exit(2); //error while atatching
....
.....
do
{
//parent turn here
wait(NULL);
....................................
//some work ..................
}
while(done!=1);
shmdt(NULL);
if( shmctl(id, IPC_RMID, NULL) == -1 )//delete the shared memory
{
perror("shmctl");
exit(-1);
}
}
exit(0);
}