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 ]
|