0

I have two mac applications X and Y. X is launching Y as child process using NSTask. And Y is reading input from user using getchar(). When running process X from Terminal. The child process X is hanging on getchar(). Looks like stdin is not working at all.

// Code for Process X

`int launchChihldProcess(NSString *process, NSArray *arguments)
{
    // Create child process task.
    NSTask *task = [NSTask new];
    task.launchPath = process;
    if (arguments != nil) {
        task.arguments = arguments;
    }
    NSLog(@"launching Child process: %@ %@", task.launchPath, task.arguments);
    NSFileHandle *inHandle = [NSFileHandle fileHandleWithStandardInput];
    NSFileHandle *outHandle = [NSFileHandle fileHandleWithStandardOutput];
    task.standardInput = inHandle;
    task.standardOutput = outHandle;

    [task launch];
    [task waitUntilExit];
    int status = task.terminationStatus;
    return status;
}

int main(int argc, const char * argv[]) {
    {
        NSArray *args = NSProcessInfo.processInfo.arguments;
        NSMutableArray *launchArgs = [NSMutableArray arrayWithCapacity: args.count];
        for(int index = 1; index < args.count; ++index) [launchArgs addObject: args[index]];
        //TODO: Point to child process path.
        NSString *app_path = [[NSBundle mainBundle] pathForResource:@"Y" ofType:@"app"];

        app_path = [app_path stringByAppendingString:@"/Contents/MacOS/Y"];
        return launchChihldProcess(app_path, launchArgs);
    }
    return NSApplicationMain(argc, argv);
}`

// Code for Y:

int main(int argc, const char * argv[]) {
    NSLog(@"Child process:: main()");
    std::cout << "Enter 1 or 2 to proceed: " << std::endl;
    char c;
    do
    {
        c = getchar();
    } while (!(c == '1' || c == '2'));

    std::cout << "Received input is: " << c << std::endl;
    return NSApplicationMain(argc, argv);
}

When running the process X from Terminal, The child process Y is hanging on 1getchar()`. Any idea?

0 Answers0