我对C中的分叉过程有疑问。我理解这段代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#define NP 5
int main()
{
pid_t pid;
for(int i=0;i<NP;i++) // loop will run n times (n=5)
{
pid = fork();
if (pid) {
printf("[PARENT] pid %d \n",getpid());
}
else if(pid == 0)
{
printf("[SON] pid %d from [PARENT] pid %d\n",getpid(),getppid());
exit(0);
//break;
}
else {
printf("fork error\n");
exit(1);
}
}
for(int i=0;i<NP;i++) { // loop will run n times (n=5)
wait(NULL);
printf("Finish waiting for a process \n");
}
return 0;
}
输出:
./forkexample
[PARENT] pid 5377
[PARENT] pid 5377
[SON] pid 5378 from [PARENT] pid 5377
[PARENT] pid 5377
[PARENT] pid 5377
[SON] pid 5381 from [PARENT] pid 5377
[PARENT] pid 5377
[SON] pid 5379 from [PARENT] pid 5377
Finish waiting for a process
[SON] pid 5380 from [PARENT] pid 5377
Finish waiting for a process
Finish waiting for a process
Finish waiting for a process
[SON] pid 5382 from [PARENT] pid 5377
Finish waiting for a process
我理解fork是如何工作的,孩子在fork之后开始行。 fork()为子节点返回0。孩子进入pid == 0区块并中断/退出for循环。
但现在在这种情况下我需要为文件中的每一行(list.txt)分叉一个进程。我的list.txt有3行:001.txt,002.txt,003.txt(数据文件夹中包含3个文件的每个文件一个)。我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAXBUFFER 50
int main(int argc, char **argv)
{
printf("--beginning of program\n");
//write a C program that takes as arguments a number C and a directory name dir
char buffer[MAXBUFFER];
if(argc !=3) {
printf("Error: two integers needed. c and directory \n");
return 0;
}
int c = atoi (argv[1]);
printf("c: %d \n", c);
printf("directory: %s \n", argv[2]);
//The main program, using the system call system, outputs in a file list.txt the list of files in
//directory dir.
sprintf(buffer, "cd %s && ls -p | grep -v / -> ../list.txt", argv[2]);
printf("command to execute: %s \n", buffer);
system(buffer);
//Then it reads the content of the file list.txt, and for each read line (a filename)
char buf[1000];
FILE * ptr_file;
ptr_file =fopen("list.txt","r");
if (!ptr_file)
return 1;
int nump = 0;
printf("The parent MAIN process id: %d\n", getpid());
pid_t pid;
while (fgets(buf,1000, ptr_file)!=NULL) {
/* fgets: If a newline is read, it is stored into the buffer.
int len=strlen(buf);
if(buf[len-1]=='\n') {
buf[len-1]='\0';
}*/
pid = fork();
if (pid) {
printf("[PARENT] pid %d \n",getpid());
}
else if(pid == 0)
{
printf("[SON] pid %d from [PARENT] pid %d\n",getpid(),getppid());
exit(0);
//break;
}
else {
printf("fork error\n");
exit(1);
}
}
printf("Out of while. I'm process: %d \n - number of lines: %d", getpid(), nump);
printf("--end of program--\n");
return 0;
}
输出是这样的:
./es02 2 data
--beginning of program
c: 2
directory: data
command to execute: cd data && ls -p | grep -v / -> ../list.txt
The parent MAIN process id: 5936
[PARENT] pid 5936
[SON] pid 5941 from [PARENT] pid 5936
[PARENT] pid 5936
[SON] pid 5942 from [PARENT] pid 5936
[PARENT] pid 5936
[PARENT] pid 5936
[SON] pid 5944 from [PARENT] pid 5936
[PARENT] pid 5936
[PARENT] pid 5936
[SON] pid 5945 from [PARENT] pid 5936
[PARENT] pid 5936
Out of while. I'm process: 5936
- number of lines: 0--end of program--
[SON] pid 5946 from [PARENT] pid 5936
[SON] pid 5943 from [PARENT] pid 1
[SON] pid 5947 from [PARENT] pid 1
所以,我希望找到像上一个例子一样的三个过程,但是,有三个以上,因为孩子进入区块并退出。此外,我知道我暂时没有等待,但我认为这不会影响循环的行为。
我没有问如何解决问题,只是为什么循环因不创建3个子进程而表现不同。非常感谢你