fork() returns 正数而不是零
fork() returns positive number instead of zero
我有一个问题,为什么 fork() returns 正数而不是零,当我在 code.i 中只使用它一次时,我一直在寻找解决方案几个小时但是没有什么帮助我。
这是代码
#include <stdio.h>
#include <stdlib.h> //for exit
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> //for sleep(),execvp()
#include <ctype.h>
#include <fcntl.h>
#define IN 0
#define OUT 1
#define SIZE 9
int main(int argc, char * argv[]) {
int pipe_descs[2];
int matrix[SIZE][SIZE];
int fdr, fdw; // file descriptors
int i;
int status=0;
/*if (pipe(pipe_descs) == -1) {
fprintf(stderr, "cannot open");
exit(1);
}*/
fdr = open(argv[1], O_RDONLY); // open files
fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fdr < 0 || fdw < 0) { //validation for error
perror("failed to open input or output files");
exit(EXIT_FAILURE);
}
removeSpaces(matrix, fdr, fdw);
status=fork();
if (status < 0) {
fputs("error in fork", stderr);
exit(EXIT_FAILURE);
}
if(status == 0) {
printf("got to child");
dup2(pipe_descs[IN],0);
close(pipe_descs[IN]);
dup2(pipe_descs[OUT],4);
close(STDOUT_FILENO);
}
close(fdr); // close the files
close(fdw);
exit(EXIT_SUCCESS);
}
fork()
returns 在父进程中成功的子进程的 PID(进程 ID)。 -1 失败。请参阅 man fork
(http://man7.org/linux/man-pages/man2/fork.2.html) 的摘录:
On success, the PID of the child process is returned in the parent,
and 0 is returned in the child. On failure, -1 is returned in the
parent, no child process is created, and errno is set appropriately.
到 fork()
return 秒时,您有两个(几乎)相同的进程 运行 - parent 和 child。就好像你 运行 你的程序两次。
parent 进程从 fork() - child 进程的 PID 得到一个正值 return。 child 得到零。
行 "got to child" 未打印,因为 printf
在打印换行符之前缓冲输出,并且您在 stdout 句柄有机会刷新缓冲区之前关闭它。在其末尾插入 \n
,或删除 close(STDOUT_FILENO)
行。
我有一个问题,为什么 fork() returns 正数而不是零,当我在 code.i 中只使用它一次时,我一直在寻找解决方案几个小时但是没有什么帮助我。 这是代码
#include <stdio.h>
#include <stdlib.h> //for exit
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> //for sleep(),execvp()
#include <ctype.h>
#include <fcntl.h>
#define IN 0
#define OUT 1
#define SIZE 9
int main(int argc, char * argv[]) {
int pipe_descs[2];
int matrix[SIZE][SIZE];
int fdr, fdw; // file descriptors
int i;
int status=0;
/*if (pipe(pipe_descs) == -1) {
fprintf(stderr, "cannot open");
exit(1);
}*/
fdr = open(argv[1], O_RDONLY); // open files
fdw = open("gg.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fdr < 0 || fdw < 0) { //validation for error
perror("failed to open input or output files");
exit(EXIT_FAILURE);
}
removeSpaces(matrix, fdr, fdw);
status=fork();
if (status < 0) {
fputs("error in fork", stderr);
exit(EXIT_FAILURE);
}
if(status == 0) {
printf("got to child");
dup2(pipe_descs[IN],0);
close(pipe_descs[IN]);
dup2(pipe_descs[OUT],4);
close(STDOUT_FILENO);
}
close(fdr); // close the files
close(fdw);
exit(EXIT_SUCCESS);
}
fork()
returns 在父进程中成功的子进程的 PID(进程 ID)。 -1 失败。请参阅 man fork
(http://man7.org/linux/man-pages/man2/fork.2.html) 的摘录:
On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.
到 fork()
return 秒时,您有两个(几乎)相同的进程 运行 - parent 和 child。就好像你 运行 你的程序两次。
parent 进程从 fork() - child 进程的 PID 得到一个正值 return。 child 得到零。
行 "got to child" 未打印,因为 printf
在打印换行符之前缓冲输出,并且您在 stdout 句柄有机会刷新缓冲区之前关闭它。在其末尾插入 \n
,或删除 close(STDOUT_FILENO)
行。