Hi Everyone,
I have been trying for the past few days to come up with a solution for the following:
Use a delay adjustment parameter K in the critical section to adjust the speed of the display process to show that without semaphore protection the displayed contents of the buffer are randomly interleaved.
So I have a program that creates a semaphore, forks N children, and each child does a sprintf where they put some data into a buffer. At this point, there are two options semaphore protection to display the contents or not. My semaphore works fine.
What I am struggling with is how to use this argv K to create a delay when displaying the contents of my buffer. I have tried using dummy loops, sleep(random number here), neither accomplish what I need them to.
Do you guys have any suggestions?? Maybe I am approaching this wrong entirely..I dont know. Here is what I have so far in specifics to this issue:
sprintf (buf, "\n i %d: process ID:%6ld parent ID:%6ld child ID:%6ld\n", i, (long)getpid(), (long)getppid(), (long)childpid);
strsize = strlen(buf) + 1;
pause_time = rand()%3;
sleep(pause_time);
if (opt == 'n') //no semaphore protection
{
for(j = 0; j < strsize; ++j)
{
printf("%c", buf[j]);
pause_time = rand()%5;
sleep(pause_time);
for(l = 0; l < 100000; ++l)
l = l + 1;
}