Tag:

创建单独的子函数来计算文件中的每个字母

我正在尝试为每个需要在文件中计数的letter创建单独的child process 。 我在parent process读取文件,但输出全为零。 我不明白我做错了什么。 我需要为每个字母使用子进程,但我不确定如何为每个字母创建单独的进程。 请帮忙! 这是我的代码: #include #include #include #include #include #include #include int main(int argc, char **argv){ char characters[26] = { “abcdefghijklmnopqrstuvwxyz” }; int counter[26] = { 0 }; int n = 26; int c; char ch; if (argc < 2) return 1; pid_t pids[26]; for (int i = 0; i […]

fork,pipe exec和dub2

这段代码应该打印“输出来自’ls -l’:”并附加’ls -l’的结果,但它没有…有没有人有这个问题的线索? #include #include #include #include void readStringFromFile (int file, char * readbuffer) { int nbytes = read(file, readbuffer, sizeof(readbuffer)); readbuffer[nbytes] = 0; } int main(int argc, char const *argv[]) { int fd[2]; pipe(fd); if (fork()==0)//child process { close(fd[0]); dup2(fd[1],1); int retValue = execl(“/bin/ls”,”ls”,”-l”, NULL); printf(“Exec failed: retValue = %d\n”,retValue); } else { int […]

关于fork和printf / write

可能重复: fork()和输出 通过运行: #include int main() { fork(); printf(“b”); if (fork() == 0) { write(1, “a”, 1); }else{ write(1, “c”, 1); } return 0; } 我有cbcabbab ,有人可以向我解释输出吗? 如果可能的话,是否有工具可以逐步查看运行程序?

C命名管道(fifo)。 父进程陷入困境

我想创建一个简单的程序,该fork,并且子进入命名管道,父进程从命名管道读取和显示。 问题是它进入了父进程,进行了第一次printf然后它变得奇怪,它没有做任何其他事情,没有进入第二个printf,它只是在控制台中输入的方式。 #include #include #include #include #include #include #include void main() { char t[100]; mkfifo(“myfifo”,777); pid_t pid; pid = fork(); if (pid==0) { //execl(“fifo2″,”fifo2”,(char*)0); char r[100]; printf(“scrie2->”); scanf(“%s”,r); int fp; fp = open(“myfifo”,O_WRONLY); write(fp,r,99); close(fp); printf(“exit kid \n”); exit(0); } else { wait(0); printf(“entered parent \n”); // <- this it prints // whats below this […]

通过多个进程从stdin读取

我正在编写一个程序,其中父进程使用fork()创建N个子进程(N作为参数提供),以便每个子进程直接由这一个父进行分叉。 每个子进程都需要从stdin读取一行,然后打印到屏幕上。 编译并执行程序后,文本通过如下文本文件提供: ./prog1 3 < fileWithText.txt 我的问题是,我希望看到每个孩子都“争取”阅读输入,但我实际看到的是,总是只有一个子进程负责处理输入。 这是我用于子进程的代码: void do_child_reader(int j) { int pid; int counter = 0; char* buffer; char* readCheck; int readerRunning = TRUE; pid = getpid(); buffer = (char*)malloc(BUFFER_SIZE * sizeof(char)); while (readerRunning == TRUE) { readCheck = fgets(buffer, BUFFER_SIZE, stdin); if (readCheck == NULL) { readerRunning = FALSE; } else […]

使用pipe():如何允许多个子进程同时执行

我正在使用pipe()按索引拆分文件,将该索引发送到子进程,让子进程计算其指定文件块中数字的总和,并将其总和返回给父进程。 我的孩子似乎按顺序执行,我希望他们同时执行,以使这个过程更有效率。 这是我正在使用的代码: #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int numchild; struct timeval stop, start; int i, j, len, ret, fpos=0, val, count=0, total=0, alltotal=0; pid_t pid; int nums = 1000; FILE * file; printf(“How many children to use: “); scanf(“%d”, &numchild); printf(“\nWill use %d child process(es).\n”, numchild); […]

强制使用`exec`创建的程序执行无缓冲的I / O.

我正在尝试使用pipe , fork和exec与C中的外部程序进行交互。 我想强制外部程序执行无缓冲的I / O. 这是我的代码到目前为止的相关代码段: … pid = fork(); if (pid == (pid_t) 0) { /* child process */ /* make pipe connections to standard streams */ dup2(wpipe[0], STDIN_FILENO); dup2(rpipe[1], STDOUT_FILENO); /* close pipe endings */ close(wpipe[0]); close(rpipe[0]); close(wpipe[1]); close(rpipe[1]); /* unbuffered I/O */ setvbuf(stdin, NULL, _IONBF, BUFSIZ); setvbuf(stdout, NULL, _IONBF, BUFSIZ); if […]

用于多个流程的管道

目前正在做一些功课,并且很难过。 目标是生成100,000个数字并将它们全部加在一起,方法是将工作分成10个进程(每个10,000个数字) 我想我已经想出了如何分叉进程(希望如此),但是使用Pipe()来传递每个子进程的小计不起作用……下面的程序为每个子进程返回44901,为运行总计返回449010。 我努力奋斗但我觉得这很简单,我应该能够理解。 main() { int i; pid_t pid; int status = 0; int fd[2]; int runningTotal = 0; pipe(fd); int t; int r; for (i = 0; i < 10; i++) { pid = fork(); if (pid == 0){ close(fd[0]); t = ChildProcess(); write(fd[1], &t, sizeof(t)); exit(0); } close(fd[1]); read(fd[0], &r, sizeof(r)); runningTotal […]

管道作为stdin / stdout进程通信。

我正在学习管道而且我遇到了问题。 我希望我的程序能够像: grep [word to find] [file to search] | grep -i [without word] | wc -l grep [word to find] [file to search] | grep -i [without word] | wc -l它编译并且没有错误地工作,但它没有输出(至少不在stdout上,因为我希望它做)。 奇怪的是,当我尝试在最后一个分叉中打印它时,它会在stdin上打印出来。 我没有改变这个叉子或parrent过程中的标准输出因此对我来说似乎很奇怪。 我正在尝试关闭未使用的管道并刷新stdout(它还在做什么吗?),但可能还有更多要做的事情。 #include #include #include void help() { printf( “Usage of the program:\n” “\t./alagrep [fileToSearch] [wordToFind] [wordToExpel]\n”); } int main(int argc, char […]

与fork()混淆

我很难理解fork()命令在不同场景下的作用。 以下是我的书中的一些示例代码: int main() { int a = 12; int b = 9; int fid = fork(); if (fid == 0) { a++; } else { wait(NULL); b = b – 5; } printf(“program exit. a = %d, b = %d\n”, a, b); return 0; } 有人可以告诉我fork()命令在这种情况下正在做什么,也许还有一些例子可以澄清一下吗?