Visit Our UNIX and Linux User Community


Daemon...Zombie?? Please help me


 
Thread Tools Search this Thread
Top Forums Programming Daemon...Zombie?? Please help me
# 1  
Old 05-09-2005
Question Daemon...Zombie?? Please help me

Hello,
i am very very puzzled,
im doing this project for school, its a deamon logger, but anyways
I'm supposed to run the daemon, let it run on the backgroun, and then run a different program (from command like prompt). but when i run my daemon, it never goes back to the nova> prompt. Smilie
i dont know what that means, and what i did wrong... someone please help.
Below is my code:

*Basically what it's supposed to do it look for files in a given direcotry, and append them to the Main Log File, and delete the old files. Only keeping the MainLogFile. So yea..... i got to work as a regular program, but when i tried to make into a daemon, it just freezes up, after eerything is done, not letting me run the main program. Smilie
i dont know if i'm exmplaining myself right.... but PLEASE HELP ME!!!!

----Tanya Smilie

Code:
int filedata(char *pathname, int x);
int List()
//list files in the current working directory
{
        //check if already a daemon
        if(getppid() ==1) return(0);

        //fork
        int i;
        i = fork();
        if(i<0) exit(1);        //fork error, exit
        if(i>0) exit(0);        //parent exits
        //child continues......
        setsid();       //obtain new process group

        //variable declarations
        struct dirent *d;
        char *NewDir;
        DIR *dp;
        char buffer[50];
        static int x=0;
        chdir("LogFiles");
        NewDir = getcwd(buffer, 50);
        cout << "Dir: " << NewDir << endl;
        dp = opendir(NewDir);
        while(d = readdir(dp))
        {
                if(d->d_ino != 0)
                {
                        filedata(d->d_name,x);
                        x++;
                }

        }
        closedir(dp);
        //signla handlers
        signal(SIGCHLD, SIG_IGN);       //ignore the child
        signal(SIGTSTP, SIG_IGN);       //ignore tty signals
        sigset(SIGHUP, SIG_IGN);
        sigset(SIGINT, SIG_IGN);
        sigset(SIGPOLL, SIG_IGN);
        sigset(SIGCLD, SIG_IGN);
        sigset(SIGTERM, SIG_IGN);
        return (0);
}

int filedata(char *pathname, int x)
//list file detail in the directory
{
        struct stat statbuf;
        ifstream  in;   //reading

        if(stat(pathname, &statbuf) == -1)
        {
                cout << "could not stat " << pathname << endl;
                return (-1);
        }
        cout << pathname << endl;
        string Msg, ToFileFinal;
        string ToFile;
        if(x <2)
        {
                cout << ". and .. right now...." << endl;
        }
        else
        {
                cout << "FileName: " << pathname << endl;
                //read from a file
                in.open(pathname);
                getline(in, ToFile);
                getline(in,Msg);
                cout << "ToFile: " << ToFile << endl;
                cout << "Message: " << Msg << endl;
                //write to a file
                ToFileFinal = "../";
                ToFileFinal.append(ToFile);
                cout << "the output will be written to: " << ToFileFina
                cout << endl;
                ofstream out;
                out.open(ToFileFinal.c_str(), ios::app);
                out << Msg;
                out << endl;
                out << "*** | *** | *** | *** | *** | ***" << endl;
                //close the files before leaving
                in.close();
                out.close();
                //delete the input files
                cout << "about to remove..." << pathname << endl;
                remove(pathname);
                wait(30);
        }
        return (statbuf.st_mode);

}

int main()
{
                List();
                while(1) sleep(1);//run
                return (0);
}


Last edited by Perderabo; 05-09-2005 at 09:48 PM.. Reason: Add code tags for readability
# 2  
Old 05-09-2005
Please make sure that you have read our rules. And note:
(6) Do not post classroom or homework problems.

But we sometimes bend this rule when a student has done a lot of work. So I'll make a few comments based on a quick scan of your code. This line of code:
while(1) sleep(1);//run
is dangerous. Why do you have it? I would delete it. If you meant to do something like:
while (1) { List() ; sleep(1) ; }
then you have the fork() in the wrong place. You need to fork() once at the beginning of the program. At present, your program is running List() once and then falling into your infinite sleep loop. You have some re-writing to do.

wait(30);
is illegal and also dangerous. I don't even know what you want to happen there. The last thing you do in List is to ignore some signals. You should ignore signals earlier. If you put List() in a loop, you need to move the signal calls to near the beginning somewhere.

Your instructor probably told you to check if the parent pid is 1, but your instructor is wrong. That is not the definition of a daemon. As it happens, you can probably get away with test you have. But the definition of a daemon is a program with no controlling terminal. You could check that by attempting to open /dev/tty. A daemon must fail but a non-damon must succeed.
# 3  
Old 05-10-2005
last question... i promise. :)

Hi,
i understand what you say about the while(1), sleep(1), someone had actually told me to put it in on another forum(which wasn't that helpfull at all),
but my question is about the fork, when you said i need to fork in the begining, do you mean all the forking is done in the main(), and it just calls the function from there?(instead of forking in the List())
.... i was thinking of putting
for(;Smilie{ open directory, and do all the file stuff here}, but that isn't working either.
I'm sorry to bugg you with this... i think i'm just having problems with the logic here. like you said it goes into infinate sleep.... so i though an infinite loop would help. but is it in the wrong place?

thanks again
Tanya R.
# 4  
Old 05-10-2005
I don't understand what your program is supposed to be doing, so I don't know what it should look like. I would tend to fork and then ignore signals in main. It's not wrong to do stuff like that in a function. But I don't know if you want to repeatedly call List in a loop or not. If it is called repeatedly, then you need to remove one-time initialization from it. I will close this thread now. If you still have problems you need to talk to your instructor.

Previous Thread | Next Thread
Test Your Knowledge in Computers #545
Difficulty: Medium
Global constants are variables whose values can be changed once the variable is set.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Zombie in C

hello all, when we are creating a process by using fork, if the child process terminates before parent, the child process exists as zombie.. My doubt is when that child process terminates, how come that process exists further and show as a zombie process..can anyone help me to clear about this? (1 Reply)
Discussion started by: aarathy
1 Replies

2. Red Hat

zombie

Hi, Linux redhat 5.5 top shows that i have 20 zombie process : Tasks: 357 total, 1 running, 336 sleeping, 0 stopped, 20 zombie Cpu(s): 0.2%us, 0.3%sy, 0.0%ni, 99.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 24949400k total, 2363052k used, 22586348k free, 227084k buffers... (1 Reply)
Discussion started by: yoavbe
1 Replies

3. UNIX for Advanced & Expert Users

Zombie process

What is the overhead associated with zombie process?Is it running out of process-ID?:confused: Since some information is stored in process table.. Thanks in Advance (4 Replies)
Discussion started by: jois
4 Replies

4. UNIX for Dummies Questions & Answers

what is zombie , how to kill it ,

Hello I try to googled it , but I dint get sufficient answer :( .. When I can see zombie running on server do they consume system resources or not ? I have read that is not good to kill them with signal 9 cause it might cause more troubles .. why is kill -9 so harmfull? thanks (2 Replies)
Discussion started by: kvok
2 Replies

5. AIX

zombie process

Is there an equivilant to the preap command in AIX that would allow me to get rid of a zombie process. I am new to AIX, moving over from Solaris and in the past I have been able to preap the pid on the defunct process to clean them up. I have looked around and the best I can see is that it may... (3 Replies)
Discussion started by: sboots
3 Replies

6. Solaris

zombie process

dear friends, in an interview they asked me what is zombie process. how we can identifying these process.if can you kill all zombie process. (8 Replies)
Discussion started by: sijocg
8 Replies

7. Shell Programming and Scripting

Zombie process

Hi I need help because I don't know if it is possible to add a find inside a cat. like I have a file with the pid of the process that use to became zombie. And I have the same pid stored in the var (pid1) now, I have no clue how to check if the the find finds the pid or even if it's... (2 Replies)
Discussion started by: ruben.rodrigues
2 Replies

8. UNIX for Advanced & Expert Users

zombie daemon process!!

My daemon process is the child of init and init has the responsibility to remove it, once it turns zombie. But I want to ask why the daemon process which is child of init turns zombie in the first place. What measures I have to take to avoid this? rish (1 Reply)
Discussion started by: rish2005
1 Replies

9. UNIX for Advanced & Expert Users

Zombie process

I would like to create a zombie process so that I can test monitoring software functionality. Any techniques? (2 Replies)
Discussion started by: swhitney
2 Replies

10. UNIX for Dummies Questions & Answers

zombie program

When you run a ps -ef and if the status is a Z (zombie) does that mean the same as not responding? (Like a windows machine). Also has anyone here heard of the program called 'top' (I've found it on our Solaris 7 machines) If you have you might be able to help me. I need to know if there is a... (8 Replies)
Discussion started by: merlin
8 Replies

Featured Tech Videos