Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-检索子对象';s大于8位的退出状态_C_Exec_Fork - Fatal编程技术网

C-检索子对象';s大于8位的退出状态

C-检索子对象';s大于8位的退出状态,c,exec,fork,C,Exec,Fork,注意:为了简单起见,我没有包含太多的错误检查,我的示例代码也没有任何实际用途 我想要什么: int main(int argc, char** argv) { int ch = fork(); if(ch == -1) { perror(NULL); }else if(ch == 0) { // In child, invoke process execl("/path/process", "process", 0);

注意:为了简单起见,我没有包含太多的错误检查,我的示例代码也没有任何实际用途

我想要什么:

int main(int argc, char** argv) {
    int ch = fork();
    if(ch == -1) {
        perror(NULL);
    }else if(ch == 0) {
        // In child, invoke process
        execl("/path/process", "process", 0);
    }else {
        // In parent, retrieve child exit code
        int status = 0;
        wait(&status);
        // print exit status
        if(WIFEXITED(status)) printf("%d\n", WEXITSTATUS(status));
    }
}
我想要一个
fork()
作为子进程并让它使用
execl()调用进程的程序。然后,我的父进程检索该进程的退出代码。这相当微不足道

我的尝试:

int main(int argc, char** argv) {
    int ch = fork();
    if(ch == -1) {
        perror(NULL);
    }else if(ch == 0) {
        // In child, invoke process
        execl("/path/process", "process", 0);
    }else {
        // In parent, retrieve child exit code
        int status = 0;
        wait(&status);
        // print exit status
        if(WIFEXITED(status)) printf("%d\n", WEXITSTATUS(status));
    }
}
我的问题:

int main(int argc, char** argv) {
    int ch = fork();
    if(ch == -1) {
        perror(NULL);
    }else if(ch == 0) {
        // In child, invoke process
        execl("/path/process", "process", 0);
    }else {
        // In parent, retrieve child exit code
        int status = 0;
        wait(&status);
        // print exit status
        if(WIFEXITED(status)) printf("%d\n", WEXITSTATUS(status));
    }
}
WEXITSTATUS()。具体来说,
过程执行一个计算,结果可能大于8位。它甚至可能是负数,在这种情况下,需要最高位来表示正确的值

我还尝试了什么:

int main(int argc, char** argv) {
    int ch = fork();
    if(ch == -1) {
        perror(NULL);
    }else if(ch == 0) {
        // In child, invoke process
        execl("/path/process", "process", 0);
    }else {
        // In parent, retrieve child exit code
        int status = 0;
        wait(&status);
        // print exit status
        if(WIFEXITED(status)) printf("%d\n", WEXITSTATUS(status));
    }
}
另外,在环顾四周时,我发现了
pipe()
函数。但是,我不确定在这种情况下如何使用它,因为在调用
execl()
之后,我无法从子文件中写入文件描述符


那么,我如何才能检索大于8位的子进程的退出状态呢?

我不认为您试图实现的目标是可能的,因为在Linux中(实际上我认为它是UX特有的),进程退出代码是8位数字(最大256):0-255(按照惯例,0表示成功,其他任何东西都表示错误)很多东西都依赖于这个事实(包括你使用的宏)。以下面的代码为例:

// a.c
int main() {
    return 257;
}
如果编译它(
gcc a.c
),并运行生成的可执行文件(a.out)检查(
echo$?
)其退出代码(将被操作系统截断;hmm还是shell?),它将输出1(环绕算法):257%256=1


作为您提到的另一种选择,您可以使用
pipe
(非常具有描述性)或sockets(
AF\u UNIX
类型)。

我认为您试图实现的目标是不可能的,因为在Linux中(实际上我认为它是UX特定的),进程退出代码是8位数字(最大256):0-255(按照惯例,0意味着成功,其他任何东西都意味着错误)很多东西都依赖于这一事实(包括您使用的宏)。下面是一段代码:

// a.c
int main() {
    return 257;
}
如果编译它(
gcc a.c
),并运行生成的可执行文件(a.out)检查(
echo$?
)其退出代码(将被操作系统截断;hmm还是shell?),它将输出1(环绕算法):257%256=1

作为您提到的另一种选择,您可以使用
pipe
(非常具有描述性)或sockets(
AF\u UNIX
类型)。

此代码来自:

作家c

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";

    /* create the FIFO (named pipe) */
    mkfifo(myfifo, 0666);

    /* write "Hi" to the FIFO */
    fd = open(myfifo, O_WRONLY);
    write(fd, "Hi", sizeof("Hi"));
    close(fd);

    /* remove the FIFO */
    unlink(myfifo);

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
int-fd;
char*myfifo=“/tmp/myfifo”;
/*创建FIFO(命名管道)*/
mkfifo(myfifo,0666);
/*向FIFO写入“Hi”*/
fd=打开(仅限myfifo、O_);
写(fd,“Hi”,sizeof(“Hi”);
关闭(fd);
/*卸下FIFO*/
取消链接(myfifo);
返回0;
}
读卡器.c

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_BUF 1024

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[MAX_BUF];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("Received: %s\n", buf);
    close(fd);

    return 0;
}
#包括
#包括
#包括
#包括
#定义最大值为1024
int main()
{
int-fd;
char*myfifo=“/tmp/myfifo”;
char buf[MAX_buf];
/*打开、读取并显示来自FIFO的信息*/
fd=打开(仅限myfifo、Ordu);
读取(fd、buf、MAX_buf);
printf(“收到:%s\n”,buf);
关闭(fd);
返回0;
}
代码(可能是父/读取器)应该删除fifo节点,可以通过调用
rm
。 否则,fifo条目将保持存在,即使在重新引导过程中也是如此,就像任何其他文件一样。

此代码来自:

作家c

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";

    /* create the FIFO (named pipe) */
    mkfifo(myfifo, 0666);

    /* write "Hi" to the FIFO */
    fd = open(myfifo, O_WRONLY);
    write(fd, "Hi", sizeof("Hi"));
    close(fd);

    /* remove the FIFO */
    unlink(myfifo);

    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
int-fd;
char*myfifo=“/tmp/myfifo”;
/*创建FIFO(命名管道)*/
mkfifo(myfifo,0666);
/*向FIFO写入“Hi”*/
fd=打开(仅限myfifo、O_);
写(fd,“Hi”,sizeof(“Hi”);
关闭(fd);
/*卸下FIFO*/
取消链接(myfifo);
返回0;
}
读卡器.c

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_BUF 1024

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[MAX_BUF];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("Received: %s\n", buf);
    close(fd);

    return 0;
}
#包括
#包括
#包括
#包括
#定义最大值为1024
int main()
{
int-fd;
char*myfifo=“/tmp/myfifo”;
char buf[MAX_buf];
/*打开、读取并显示来自FIFO的信息*/
fd=打开(仅限myfifo、Ordu);
读取(fd、buf、MAX_buf);
printf(“收到:%s\n”,buf);
关闭(fd);
返回0;
}
代码(可能是父/读取器)应该删除fifo节点,可以通过调用
rm
。 否则,fifo条目将保持存在,甚至在重新引导时也是如此,就像任何其他文件一样