如何区分一个子进程和其他子进程(How to distinguish one child process from other child processes)

编程入门 行业动态 更新时间:2024-10-26 13:26:14
如何区分一个子进程和其他子进程(How to distinguish one child process from other child processes)

我有上课的任务,我对这部分要求感到困惑。 因此,我们需要创建一个具有n个进程的多进程字计数器,n将是该程序的输入参数。 每个过程需要对输入文件的选择部分进行它们自己的迷你字数。 因此,基本上输入的文件将被分成1 / n个部分并在n个进程之间分割。

我理解如何通过for循环分叉进程以及如何使用管道将子进程中的迷你字数发送到父进程,但我不确定如何告诉某个进程执行输入文件的选择部分。

您是否会使用它们的PID值来检查它们是哪个进程然后为它们分配任务?

到目前为止这是我的代码。

#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MSGLEN 64 #define MESSES 3 int main(){ int fd[2]; pid_t pid; int result; //Creating a pipe result = pipe (fd); if (result < 0) { //failure in creating a pipe perror("pipe error\n"); exit (1); } //Creating a child process for(int i = 0; i < MESSES; i++){ if ((pid = fork()) < 0) { //failure in creating a child perror ("fork error\n"); exit(2); } if(pid == 0) break; } if (pid == 0) { // ACTUALLY CHILD PROCESS char message[MSGLEN]; //Clearing the message memset (message, 0, sizeof(message)); printf ("Enter a message: "); //scanf ("%s",message); fgets (message, 1024, stdin); close(fd[0]); //Writing message to the pipe write(fd[1], message, strlen(message)); close(fd[1]); close(fd[0]); exit (0); } else { //Parent Process char message[MSGLEN]; char *ptr; long wc; close(fd[1]); while (1) { //Clearing the message buffer memset (message, 0, sizeof(message)); //Reading message from the pipe if(read(fd[0], message, sizeof(message)) == 0) exit(0); printf("Message entered %s\n",message); /* Message entered needs to be in the format of number first space then string for it to work */ wc = 0; wc = strtol(message, &ptr, 10); printf("The number(unsigned long integer) is %ld\n", wc); printf("String part is %s", ptr); } close(fd[0]); wait(NULL); // exit(0); } return 0; }

I have an assignment for class and I am confused on this part of the requirements. So we need to make a multi process word counter with n number of processes and n will be an input argument for the program. Each process needs to do their own mini word count of a select portion of the inputted file. So essentially the inputted file will be divided into 1/n parts and split between n processes.

I understand how to fork the processes through a for loop and how to use pipes to send the mini word count from the children processes to the parent process, but I unsure of how to tell a certain process to do a select part of the input file.

Would you use their PID values to check which process they are then assign them their task?

This is my code so far.

#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MSGLEN 64 #define MESSES 3 int main(){ int fd[2]; pid_t pid; int result; //Creating a pipe result = pipe (fd); if (result < 0) { //failure in creating a pipe perror("pipe error\n"); exit (1); } //Creating a child process for(int i = 0; i < MESSES; i++){ if ((pid = fork()) < 0) { //failure in creating a child perror ("fork error\n"); exit(2); } if(pid == 0) break; } if (pid == 0) { // ACTUALLY CHILD PROCESS char message[MSGLEN]; //Clearing the message memset (message, 0, sizeof(message)); printf ("Enter a message: "); //scanf ("%s",message); fgets (message, 1024, stdin); close(fd[0]); //Writing message to the pipe write(fd[1], message, strlen(message)); close(fd[1]); close(fd[0]); exit (0); } else { //Parent Process char message[MSGLEN]; char *ptr; long wc; close(fd[1]); while (1) { //Clearing the message buffer memset (message, 0, sizeof(message)); //Reading message from the pipe if(read(fd[0], message, sizeof(message)) == 0) exit(0); printf("Message entered %s\n",message); /* Message entered needs to be in the format of number first space then string for it to work */ wc = 0; wc = strtol(message, &ptr, 10); printf("The number(unsigned long integer) is %ld\n", wc); printf("String part is %s", ptr); } close(fd[0]); wait(NULL); // exit(0); } return 0; }

最满意答案

使用fork时要记住的关键是父和子共享相同的内存,并将父项所有内容的副本传递给子项。 此时孩子已经分叉了父母的数据

在下面的代码中,我们计算了我们创建了多少个进程。 如果你想在孩子身上使用这个作为参数,你就可以了,即第n个孩子获得价值n

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #define PROCESS_COUNT 50 int main(void) { pid_t pid; size_t pid_count = 0; //pid_t pid_array[PROCESS_COUNT]; for(int i = 0; i < PROCESS_COUNT; i++) { if ((pid = fork()) < 0) { perror ("fork error\n"); exit(2); } if (pid == 0) {//child size_t n = 0; size_t p = getpid(); while(n++ < 2) { //Next line is illustration purposes only ie I'm taking liberties by //printing a pid_t value printf("child %zu has pid_count == %zu\n", p, pid_count); sleep(1); } exit (0); } else { //Count how many process we've created. pid_count++; int status; waitpid( -1, &status, WNOHANG); } } wait(NULL); return 0; }

如果你想真正想要的话,可以使用管道或共享内存来使用IPC。 有很多方法可以将数据从一个进程传递到另一个进程,有时像临时文件这样简单的东西就足够了。 对于你的问题我会使用mmap,但它不需要那么复杂

The key thing to remember when using fork is that the parent and child share the same memory and a copy of everything the parent has is passed to the child. At which point the child has now forked the parents data.

In the code below we're counting how many processes we've created. You could if you wanted use this as an argument in the child ie the nth child gets value n.

#include <stdio.h> #include <unistd.h> #include <stdlib.h> #define PROCESS_COUNT 50 int main(void) { pid_t pid; size_t pid_count = 0; //pid_t pid_array[PROCESS_COUNT]; for(int i = 0; i < PROCESS_COUNT; i++) { if ((pid = fork()) < 0) { perror ("fork error\n"); exit(2); } if (pid == 0) {//child size_t n = 0; size_t p = getpid(); while(n++ < 2) { //Next line is illustration purposes only ie I'm taking liberties by //printing a pid_t value printf("child %zu has pid_count == %zu\n", p, pid_count); sleep(1); } exit (0); } else { //Count how many process we've created. pid_count++; int status; waitpid( -1, &status, WNOHANG); } } wait(NULL); return 0; }

If you want to get really fancy you can use IPC using pipes or shared memory. There are lots of ways to get data from one process to another, sometimes something as simple as temporary files is more than sufficient. For your problem I'd use mmap but it does not need to be that complicated

更多推荐

本文发布于:2023-08-01 04:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   个子   distinguish   processes   process

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!