SICHLD problem?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > SICHLD problem?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    SICHLD problem?  
friend_05


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-05 07:52 AM

In following code when I catching SIGCHLD, it  is not returning to
parent. it  keeps on running child process. In follwing it will not
display menu again but if I don't catch SIGCHLD it works properly.




int main()
{
int c;
char file[100];
void sig_child();
void list();
void read1(char *);
struct sigaction s,sc;
s.sa_handler=SIG_IGN;
sc.sa_handler=sig_child;
sigemptyset(&s.sa_mask);
sigemptyset(&sc.sa_mask);

s.sa_flags=0;
sc.sa_flags=0;

if(sigaction(SIGINT,&s,0)<0)
perror("sigaction_int");
if(sigaction(SIGCLD,&sc,0)<0)
perror("sigaction_cld");

while(1)
{

printf("\n\tMENU\n");
printf("1)List files\n2)Read file\n\n3)Exit\n");
scanf("%d",&c);
switch(c)
{
case 1: list();
break;
case 2:    printf("Enter the file to be read");
scanf("%s",file);
read1(file);
printf("parent");
break;
case 3: exit(0);
default: break;
}

}
}

void list()
{
char path[100];
DIR *dp;
struct dirent *dirp;
struct stat buf;
printf("Enter the path\n");
scanf("%s",path);
dp=opendir(path);
while((dirp= readdir(dp))!=NULL)
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
continue;

lstat(dirp->d_name,&buf);
if(S_ISREG(buf.st_mode))
printf("%s\n",dirp->d_name);
}
closedir(dp);

}



void read1(char *rfile)

{
pid_t pid;

if((pid=vfork())<0)
perror("fork");
else if(pid==0)
{
if((execl("/usr/bin/od","/usr/bin/od","-c",rfile,
(char*)NULL))<0)
perror("exec");
_exit(0);
}
return;
}




void sig_child()
{

pid_t pid;
int status,child_val;
printf("child catch");

if((pid=wait(&status))<0)
{
perror("pid");
printf("pid=%d\n",pid);
return;
}
if(WIFEXITED(status))
{
child_val=WEXITSTATUS(status);
printf("child exited normally %d",child_val);
// return;
}


}






[ Post a follow-up to this message ]



    Re: SICHLD problem?  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-05 07:52 AM

In article <1132205153.025845.200800@g49g2000cwa.googlegroups.com>,
"friend_05" <hirenshah.05@gmail.com> wrote:

> In following code when I catching SIGCHLD, it  is not returning to
> parent. it  keeps on running child process. In follwing it will not
> display menu again but if I don't catch SIGCHLD it works properly.

Your program displays the menu again as soon as the child process forks
and execs.  You probably don't see it because it's mixed in with the
output of od.

There's nothing in the SIGCLD handler that makes it go back and display
the menu again.  You're probably already sitting in the scanf() when the
signal handler runs.

You probably want to have the parent process call wait() in the read1()
function, so that it doesn't return until the program has finished.

>
>
>
>
> int main()
> {
>  int c;
>  char file[100];
>  void sig_child();
>  void list();
>  void read1(char *);
>  struct sigaction s,sc;
>  s.sa_handler=SIG_IGN;
>  sc.sa_handler=sig_child;
>  sigemptyset(&s.sa_mask);
>  sigemptyset(&sc.sa_mask);
>
>  s.sa_flags=0;
>  sc.sa_flags=0;
>
>  if(sigaction(SIGINT,&s,0)<0)
>     perror("sigaction_int");
>  if(sigaction(SIGCLD,&sc,0)<0)
>     perror("sigaction_cld");
>
> while(1)
> {
>
>   printf("\n\tMENU\n");
>   printf("1)List files\n2)Read file\n\n3)Exit\n");
>   scanf("%d",&c);
>    switch(c)
>    {
>      case 1: list();
>              break;
>      case 2:    printf("Enter the file to be read");
>                 scanf("%s",file);
>                 read1(file);
>                 printf("parent");
>                 break;
>     case 3: exit(0);
>      default: break;
>    }
>
> }
> }
>
> void list()
> {
>  char path[100];
>  DIR *dp;
>  struct dirent *dirp;
>  struct stat buf;
>  printf("Enter the path\n");
>  scanf("%s",path);
>  dp=opendir(path);
>  while((dirp= readdir(dp))!=NULL)
>  {
>    if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
>         continue;
>
>    lstat(dirp->d_name,&buf);
>     if(S_ISREG(buf.st_mode))
>           printf("%s\n",dirp->d_name);
>  }
>  closedir(dp);
>
> }
>
>
>
> void read1(char *rfile)
>
> {
>       pid_t pid;
>
>       if((pid=vfork())<0)
>             perror("fork");
>       else if(pid==0)
>            {
>              if((execl("/usr/bin/od","/usr/bin/od","-c",rfile,
> (char*)NULL))<0)
>                  perror("exec");
>              _exit(0);
>            }
>       return;
> }
>
>
>
>
> void sig_child()
> {
>
>  pid_t pid;
>  int status,child_val;
>  printf("child catch");
>
>  if((pid=wait(&status))<0)
>  {
>      perror("pid");
>  printf("pid=%d\n",pid);
>  return;
>  }
> if(WIFEXITED(status))
> {
>  child_val=WEXITSTATUS(status);
>  printf("child exited normally %d",child_val);
> // return;
> }
>
>
> }

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***





[ Post a follow-up to this message ]



    Re: SICHLD problem?  
friend_05


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-05 11:13 PM

hi,

I am still not getting the problem.  can you please explain it in more
detail.






[ Post a follow-up to this message ]



    Re: SICHLD problem?  
friend_05


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-05 11:13 PM

hi,

i got the problem thanks for your help.

one more question,

how to print  program completion code?






[ Post a follow-up to this message ]



    Re: SICHLD problem?  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-05 11:13 PM

In article <1132271715.439671.148130@g49g2000cwa.googlegroups.com>,
"friend_05" <hirenshah.05@gmail.com> wrote:

> hi,
>
> i got the problem thanks for your help.
>
> one more question,
>
> how to print  program completion code?

Use WEXITSTATUS() on the status that wait() provides.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:30 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register