0

FSEventStreamCreate を使用して .Trash ディレクトリを監視しています。私のコールバック関数は静的関数で、.Trash が変更されるたびに実行されます。

コールバック関数では、NSPipe を使用した NSTask を介して、1 つのスクリプトを実行してステータスを取得する必要があります。[ls waitUntilExit] が最初に実行されると、関数は最初から実行されます。[ls waitUntilExit] から 2 回目に達すると、プログラムは正常に続行されます。私のコードの問題は何ですか。[1番から2番までのコードが2回実行される]

これは FSEvent の myCallbackFunction の私のコードです。

static void myCallbackFunction(
                           ConstFSEventStreamRef streamRef,
                           void *clientCallBackInfo,
                           size_t numEvents,
                           void *eventPaths,
                           const FSEventStreamEventFlags eventFlags[],
                           const FSEventStreamEventId eventIds[])
{

    ////////////////number 1////////////   
    int i;


    FILE *fp;

    char path[1035];

    /* Open the command for reading. */
    fp = popen("/bin/ls ~/.Trash/POC3.app", "r");

    if (fp == NULL) {
        printf("Failed to run command\n" );
        exit(0);
    }
    i=0;
    while (fgets(path, sizeof(path)-1, fp) != NULL) {
        i++;
    }///////if the file POC.app exists in trash execute this//////////////////
    if(i!=0){


        NSTask *ls=[[NSTask alloc]init] ;
        NSPipe *pipe1=[NSPipe pipe];
        NSData *data ;
        NSString *tmpString;

        [ls setStandardOutput:pipe1];
        NSString *execPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"script"];
        [ls setLaunchPath:execPath];
        [ls setArguments:[NSArray arrayWithObjects:@"hello",nil]];

        [ls launch];
        [ls waitUntilExit];
        ///////////////number 2/////////////////

        data = [[[ls standardOutput] fileHandleForReading] availableData];






        if ((data != nil) && [data length]) {

            tmpString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];




         }

    //some other functionality follows here
    }
}
4

1 に答える 1

0

間違いなくデッドロックがあります。スクリプトが終了するまで、スクリプトの出力を読みません。パイプバッファがいっぱいであるためにブロックされた場合、それは決して終了しません。

于 2012-04-16T13:22:01.090 に答える