|
-
March 30th, 2009, 07:35 AM
#1
processes problems!
ok...i wrote a program that takes user input within 5 secs 3 times ... the user should enter a number before the timer expires...
here is the code
Code:
/* This program checks if the user has "5" as input before the countdown is over or not*/
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
int main()
{
int x,i=0,pid,pid2;
system("clear");
for(x=0;x<3;x++)
{
pid=-1;pid2=-1;
if((pid=fork()) == 0) //child process- the countdown timer
{
pid=getpid();
for(i=5;i>0;i--) //start a 5 sec countdown
{
printf("X=%d\tenter number(ppid=%d,pid=%d)",x,getppid(),getpid());
printf("\t\t\t\t\t\t\t%d\a\n",i);
sleep(1);
}
exit(666+x); //exit when 5 secs are over
}
else //parent process
{
if((pid2=fork()) == 0) //create a seperate child process for input
{
pid2=getpid();
printf("\n\nscanf pid2=%d\n\n",pid2);
scanf("%d",&i);
kill(pid,SIGKILL); //when input is recieved kill the timer process
if(i==5) //check the user input
{
printf("\nYES i=5(pid=%d) for x=%d\n\n",getpid(),x);
}
else
{
printf("\nNO i!=5(pid=%d) for x=%d\n\n",getpid(),x);
}
exit(777+i);
}
wait(); //wait for child process...timer process.
sleep(3); //take a deep breath
printf("\n\npid2=%d(i must execute only after wait() is over)\n\n",pid2);
if((kill(pid2,SIGKILL))==0) //after the timer child process finishes ,if input process is still running...kill it
{
printf("\nNO i!=5(pid=%d) for x=%d\n\n",getpid(),x);//default msg
}
}
}
printf("THE END(pid=%d\n",getpid()); //this should be executed when all other child processes are over.
return 0;
}
but i get a lot of unexpected behaviour...
this output is one example
Code:
#i wont be entering any input whatsoever...
X=0 enter number(ppid=24800,pid=24802) 5
scanf pid2=24803
X=0 enter number(ppid=24800,pid=24802) 4
X=0 enter number(ppid=24800,pid=24802) 3
X=0 enter number(ppid=24800,pid=24802) 2
X=0 enter number(ppid=24800,pid=24802) 1
pid2=24803(i must execute only after wait() is over)
NO i!=5(pid=24800) for x=0 #its ok till here...it waited till timer exited then executed the rest,killing scanf process.
X=1 enter number(ppid=24800,pid=24812) #new timer process 5
scanf pid2=24813 #new child scanf process
X=1 enter number(ppid=24800,pid=24812) 4
X=1 enter number(ppid=24800,pid=24812) 3
pid2=24813(i must execute only after wait() is over) #why didnt it wait till timer process exited?
NO i!=5(pid=24800) for x=1 #this killed my scanf before the timer expired.
X=1 enter number(ppid=24800,pid=24812) 2
X=2 enter number(ppid=24800,pid=24817) 5
scanf pid2=24818 # x=2 has started before x=1 even finished
X=1 enter number(ppid=24800,pid=24812) 1
#the sleep(3) statement never executed again
X=2 enter number(ppid=24800,pid=24817) 4
X=2 enter number(ppid=24800,pid=24817) 3
X=2 enter number(ppid=24800,pid=24817) 2
pid2=24818(i must execute only after wait() is over)
NO i!=5(pid=24800) for x=2
THE END(pid=24800 #the parent exits , leaving the timer process an orphan
[c_d@localhost C scratchpad]$ X=2 enter number(ppid=1,pid=24817) 1
the unexpected behaviour is highlighed with red...and commentaries are written by side of output in blue...
please comment.
Last edited by creeping death; March 30th, 2009 at 07:37 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|