|
Home > Archive > Unix Programming > November 2005 > SICHLD problem?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| friend_05 2005-11-17, 2: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;
}
}
| |
| Barry Margolin 2005-11-17, 2: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 ***
| |
| friend_05 2005-11-17, 6:13 pm |
| hi,
I am still not getting the problem. can you please explain it in more
detail.
| |
| friend_05 2005-11-17, 6:13 pm |
| hi,
i got the problem thanks for your help.
one more question,
how to print program completion code?
| |
| Barry Margolin 2005-11-17, 6: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 ***
|
|
|
|
|