最初由 胡说之 发布
if (getppid>1) wait(getppid());
getppid>1 ---- variable getppid is not defined. Is it a typo? should be getppid()?
最初由 胡说之 发布
system("rm blablabla");
It doesn't work --- "Permission denied", neither in parent, nor in child.
最初由 胡说之 发布
Honest speaking, neith majia041's nor chhuili's code works in my bloody cygwin. ---- I think I found the reason why NASA's space shuttle blew up! They might use cygwin
I'll install RH Linux soon.
马甲甲: You are right. It does not remove the program file on HP-UX, even though the return code is 0. I confirmed on Solaris, Mac and Linux, it works as expected. I have tested it on AIX.最初由 马甲甲 发布
kill process 比较合适,这儿用得的确不好。
After running the toy, it is removed from file system.
Someone mentioned it works on HP, but mine does not, I think it also depends on which verison. (complicated?)
最初由 chhuili 发布
马甲甲: You are right. It does not remove the program file on HP-UX, even though the return code is 0. I confirmed on Solaris, Mac and Linux, it works as expected. I have tested it on AIX.
Regards !
It seems no way to do it. The program can't remove a file while it is in using on hp/ux. I use the fork() to create a new process, it works on Solaris and Linux, but it does not work on hp/ux. I am not sure how the fork code works for you above without modification. I am using GNU gcc 2.95.3. The fork code was modified as following:最初由 majia041 发布
i don't have hp/ux. so if u want to remove the file, what u will do?
fork a new process on hp/ux?
this code is totally wrong. it works on linux and other os because of unlink.最初由 chhuili 发布
It seems no way to do it. The program can't remove a file while it is in using on hp/ux. I use the fork() to create a new process, it works on Solaris and Linux, but it does not work on hp/ux. I am not sure how the fork code works for you above without modification. I am using GNU gcc 2.95.3. The fork code was modified as following:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
int main(int argc, char** argv)
{
pid_t pt;
int status;
if ((pt=fork()) != 0)
{
kill(getpid(), SIGIO);
}
else
{
if (getppid()>1) waitpid(getppid(), &status, WNOHANG);
remove(argv[0]);
}
return 0;
}
最初由 majia041 发布
this code is totally wrong. it works on linux and other os because of unlink.
can u try this on hp/ux, thanks. because i don't have hp/ux in my computer.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char** argv)
{
pid_t pt,ppt;
if ((pt=fork()) != 0 )
return 0;
else
{
if ((ppt=fork()) != 0 )
return 0;
else
{
char cmd[18];
strcpy(cmd, "rm ");
strcat(cmd, argv[0]);
system(cmd);
return 0;
}
}