管道进程通信父母对孩子的权限被拒绝

时间:2016-05-03 15:10:46

标签: c linux unix system-calls

当我构建并运行此程序时:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h> 
#include <string.h>

int main() {
    int pipefd[2], pid, n, rc, nr, status;
    char *teststring = "Hello World\n", buf[1024];
    rc = pipe(pipefd);

    if(rc < 0) {
        perror("Pipe");
        exit(1);
    }
    pid = fork();
    if(pid < 0) {
        perror("fork");
        exit(1);
    }
    if (pid == 0) {
        close(pipefd[0]);
        write(pipefd[1], teststring, strlen(teststring));
        close(pipefd[1]);
        exit(0);
    }

    // parent's code
    close(pipefd[1]);
    n = strlen(teststring);
    nr = read(pipefd[0], buf, nr);
    rc = write(1, buf, nr);
    wait(&status);
    printf("Good Work child\n");
    return 0;
}

它因“拒绝许可”消息而失败。

我在Elementary OS Freya(Linux)上测试,我看到在Netbeans下运行并从终端运行的相同行为。

问题是什么,我该如何解决?

0 个答案:

没有答案
相关问题