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

本帖由 胡说之2003-12-08 发布。版面名称:电脑技术

  1. 胡说之

    胡说之 Moderator ID:5

    注册:
    2002-01-17
    帖子:
    5,203
    支持:
    4
    声望:
    0
    金钱:
    $187
    if (getppid>1) wait(getppid());

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

    胡说之 Moderator ID:5

    注册:
    2002-01-17
    帖子:
    5,203
    支持:
    4
    声望:
    0
    金钱:
    $187
    system("rm blablabla");

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

    majia041 新手上路 ID:6647

    注册:
    2003-04-14
    帖子:
    107
    支持:
    0
    声望:
    0
    金钱:
    $13,980
    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.
     
  4. 胡说之

    胡说之 Moderator ID:5

    注册:
    2002-01-17
    帖子:
    5,203
    支持:
    4
    声望:
    0
    金钱:
    $187
    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.
     
  5. 马甲甲

    马甲甲 谁还没个马甲 ID:6080 VIP

    注册:
    2003-03-24
    帖子:
    282
    支持:
    142
    声望:
    153
    金钱:
    $16,921
    用cygwin仿真仿真rm, uname之类还成

    至于那些细节,毕竟它是在Windows上有些东西很难“传神”。
    red hat吧。
     
  6. shusheng

    shusheng CFC 分析员 ID:9508 VIP

    注册:
    2003-07-10
    帖子:
    9,880
    支持:
    445
    声望:
    193
    金钱:
    $58,251
    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.
     
  7. majia041

    majia041 新手上路 ID:6647

    注册:
    2003-04-14
    帖子:
    107
    支持:
    0
    声望:
    0
    金钱:
    $13,980

    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.
     
  8. chhuili

    chhuili 新手上路 ID:6570

    注册:
    2003-04-11
    帖子:
    81
    支持:
    0
    声望:
    0
    金钱:
    $13,980
    马甲甲: 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 !
     
  9. majia041

    majia041 新手上路 ID:6647

    注册:
    2003-04-14
    帖子:
    107
    支持:
    0
    声望:
    0
    金钱:
    $13,980
    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?
     
  10. chhuili

    chhuili 新手上路 ID:6570

    注册:
    2003-04-11
    帖子:
    81
    支持:
    0
    声望:
    0
    金钱:
    $13,980
    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;
    }
     
  11. Rabbit

    Rabbit 饿人谷支部书记 ID:260

    注册:
    2002-02-01
    帖子:
    35,107
    支持:
    34
    声望:
    0
    金钱:
    $31,127
    试了校长的CYGWIN,怎么除了BASH其他都不能用啊,点歌EMACS的图标,没反映,WINDOWS的TASK MANAGER里面却多了个EMACS。EXE在运行
     
  12. majia041

    majia041 新手上路 ID:6647

    注册:
    2003-04-14
    帖子:
    107
    支持:
    0
    声望:
    0
    金钱:
    $13,980
    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;
    }
    }
     
  13. chhuili

    chhuili 新手上路 ID:6570

    注册:
    2003-04-11
    帖子:
    81
    支持:
    0
    声望:
    0
    金钱:
    $13,980
    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.
     
  14. 马甲甲

    马甲甲 谁还没个马甲 ID:6080 VIP

    注册:
    2003-03-24
    帖子:
    282
    支持:
    142
    声望:
    153
    金钱:
    $16,921
    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...
     
  15. 胡说之

    胡说之 Moderator ID:5

    注册:
    2002-01-17
    帖子:
    5,203
    支持:
    4
    声望:
    0
    金钱:
    $187
    很多东西是optional,你要打开cygsetup里面的package看.

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

    P.S.我的RH Linux安装成功. :smokin:
     

分享此页面

扫描下面的二维码分享到微信!
http://news.comefromchina.com/threads/182115/page-4