home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!psgrain!hippo!ee.und.ac.za!csir.co.za!olsa99!infow02!andrewr
- From: andrewr@infoware.co.za (Andrew Roos)
- Newsgroups: comp.lang.c
- Subject: Re: fork() and wait() problem...Please HELP!
- Message-ID: <1992Dec15.093050.1242@infoware.co.za>
- Date: 15 Dec 92 09:30:50 GMT
- References: <&hc1Ha#a$a@atlantis.psu.edu>
- Organization: Infoware (Pty) Ltd
- Lines: 70
-
- newton@wilbur.psu.edu (Cynthia Newton) writes:
-
- >In the following program, if I want to edit the file more than than
- >once, most of the time 'vi' just freezes.
-
- >Could you tell how to handle this kind of situation correctly?
- >I don't want to use system().
-
- >I use SunOS 4.1.1
-
- >Thanks.
-
- >-----------------------------
- >#include <stdio.h>
-
- >main() {
- > int pid;
- > int ch;
-
- >for(;;){
- > ch=chh();
- >switch(ch)
- >{
- > case 1:
- > pid=fork();
- > if(pid==0){
- > execl("/usr/ucb/vi","vi","file",NULL);
- >}
- >wait();
- >break;
- > case 2:
- > exit();
- >}
- >}
-
- >}
-
- >chh()
- >{
- >int c;
- >char s[10];
- >fprintf(stderr,"1. edit\n");
- >fprintf(stderr,"2. exit\n");
-
- >do{
- >fprintf(stderr,"Enter Choice: ");
- >gets(s);
- >c=atoi(s);
- >}while(c<0 || c>2);
- >return c;
- >}
- >--------------------------------------
- >--
- >C Newton
-
- Hi Cynthia
-
- I don't know if this is the problem, but your call to wait() is incorrect.
- Wait expects a pointer to int as an argument, which is where it stores the
- status of the child process which terminated. You can modify it to:
-
- wait((int*)0)
-
- if you are not interested in the status. It is possible that the status
- value is being written to an arbitary address which may be corrupting
- something.
-
- Hope this helps
-
- Andrew Roos
-