此代码用于搜索文件中的文本
它像这样工作
./File [text_to_search] [File1_path] [File2_path] [File3_path] ......
像
./File hello /root/Desktop/file1.txt /root/Desktop/file2.txt /root/Desktop/file3.txt
GetLine是我的功能
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
/* COLORS To Print */
#define KRED "\x1B[31m"
#define RESET "\033[0m"
int main(int argc, char *argv[])
{
if(argc < 3)
{
printf(KRED "You Must Insert Word to Search and 1 file path at least\n" RESET);
return 0;
}
int i = 0;
for (i = 2; i < argc; i++)
{
struct timeval start, end;
long mtime, secs, usecs;
gettimeofday(&start, NULL);
pid_t pid;
pid = fork();
if(pid <0)
{
printf("error\n");
}
else if (pid ==0)
{
printf ( "Child : Child’s PID: %d\n", getpid());
printf ( "Child : Parent’s PID: %d\n", getppid());
GetLines(argv[i] , argv[1]);
}
else
{
printf ( "Parent : Parent’s PID: %d\n", getpid());
printf ( "Parent : Child’s PID: %d\n", pid);
wait(NULL);
}
gettimeofday(&end, NULL);
secs = end.tv_sec - start.tv_sec;
usecs = end.tv_usec - start.tv_usec;
mtime = ((secs) * 1000 + usecs/1000.0) + 0.5;
printf("Elapsed time: " KRED "%ld " RESET "millisecs\n\n", mtime);
}
return 0;
}
答案 0 :(得分:1)
因为代码继续在两个进程上运行..一个用于父进程,一个用于yhr子进程。