Advertisement

Problem with fork() and execv()

Started by March 01, 2004 09:44 AM
3 comments, last by Bleakcabal 20 years, 8 months ago
I have a program that must call execv with a command entered by the user. If I just call execv the program works but quits after execv, which I think is normal from reading man:execv. So I decided to create a child process like this ( this is in a loop ): pid = fork(); if (pid == -1) { printf("\nError"); } else if (pid == 0) { wait(NULL); execv(myStrCat("/bin/", cmd[0]), cmd); } The problem is sometimes, and I can''t find a pattern for this, the program just goes into an infinite loop where it creates child processes so fast that I can''t do anything about it. And these process do not execv anything they just do nothing ( maybe it is because execv executes cmd which must be inputed by the user ). This goes on until : 1: the max number of process that I am able to create under /etc/limits.conf is reached and my machine is unbearably slow. 2: my machine freezes after a while Which makes debugging kind of hard because it requires many reboots. Does someone know what could be happening ? I would appreciate any help anyone could give me !
WHO DO THEYTHINK THEY'REFOOLING : YOU ?
Why are you calling wait in the child process? My initial guess at the source of the problem is that the child process ends up waiting forever and the parent process does something weird somewhere after that section of code.

Advertisement
Why not just use the system() call?
You need another test there I believe.

if (pid == -1)//errorelse if (pid == 0)// this is the parent process (i think, it's been awhile)else // pid == the newly spawned child's pid// this is the child process// this is where you should call execv() 


[edited by - Malone1234 on March 1, 2004 3:59:17 PM]
Oops, your right, I have read up some more and I should call wait from the parent process.

Thanks !
WHO DO THEYTHINK THEY'REFOOLING : YOU ?

This topic is closed to new replies.

Advertisement