精华 C/C++ ---- 005

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.
 
最初由 胡说之 发布
if (getppid>1) wait(getppid());

getppid>1 ---- variable getppid is not defined. Is it a typo? should be getppid()?

u r right, should be getppid()
but even then this program is wrong.
did u try it after u change it to getppid()?

i mean i ran it, and this program would do excatly the request asks for. But i still feel this program is wrong.

now i think the reason why this program works is i use remove function, not fork or anything else.
 
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:D

I'll install RH Linux soon.
 
用cygwin仿真仿真rm, uname之类还成

至于那些细节,毕竟它是在Windows上有些东西很难“传神”。
red hat吧。
 
最初由 胡说之 发布
system("rm blablabla");

It doesn't work --- "Permission denied", neither in parent, nor in child.

Are you "root"? Do you have write permission of the file? (you need write permission to unlink a file)

I seems remember something about cgywin on FAT have problems with these file permissions, cause the bloody FAT don't have idea of "owner" or "group" :D

BTW, here is what POSIX says about unlink(path) returning EPERM:

EPERM: The file named by path is a directory, and the calling process don't have the appropriate privilieges, or the target file system prohibits using unlink() on directories.
 
最初由 胡说之 发布
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:D

I'll install RH Linux soon.


chhuili's code is right.
my code is bull shit.
to fulfil the request, we don't really need fork another process.
one is enough.
 
最初由 马甲甲 发布
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?)
马甲甲: 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 !
 
最初由 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 !

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?
 
最初由 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?
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;
}
 
试了校长的CYGWIN,怎么除了BASH其他都不能用啊,点歌EMACS的图标,没反映,WINDOWS的TASK MANAGER里面却多了个EMACS。EXE在运行
 
最初由 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;
}
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;
}
}
 
最初由 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;
}
}

It works on Linux, but it does not work on hp/ux. I used "rm -f ".
By the way, unlink and fork code work fine on AIX box as well.
 
If HP does not support "postpone"

It is hard to believe these code will work, since parent process or child process both refer the executable file, child could kill parent, but it is still alive and it is a reference of the file, so...
 
最初由 Rabbit 发布
试了校长的CYGWIN,怎么除了BASH其他都不能用啊,点歌EMACS的图标,没反映,WINDOWS的TASK MANAGER里面却多了个EMACS。EXE在运行

很多东西是optional,你要打开cygsetup里面的package看.

估计挑战者号的宇航员们就象你这样,package没装齐就飞了,出了问题赶快点help me out图标,发现没反映,晕过去了,于是,飞船就爆炸了.

P.S.我的RH Linux安装成功. :smokin:
 
后退
顶部