简体   繁体   中英

How to distinguish one child process from other child processes

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;
}

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 .

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 child gets value . 个子代获得值

#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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM