Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:47:05+00:00 2026-06-12T18:47:05+00:00

i have two son processes that read from a file and send the results

  • 0

i have two son processes that read from a file and send the results to the father process, but when the childs have sent the strings the father receives the strings plus some other strange chars … how do i null terminate the string received and how do i make the father wait for all the sons that might have more work to do after sending some of the results…because the sons might send other results later …
Thanks for the help …

#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <strings.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>

    #define max_chars_string 1000
    #define n_childs 2

    pid_t childs[n_childs];
    int channel[n_childs][2];

    void read_lines(char * filename, char (*pointer)[max_chars_string],int init_read,int n_lines);
    void get_strings_hash(char (*pointer_strings)[max_chars_string],char (*pointer_hashes)[max_chars_string],int total_lines);

    void worker(int mypipe,char filename[],int n_lines){ // meter as funcoes de ler passwords no filho
        int resources[2];// numero de linhas que cada processo tem de ler
        int i = 0;

        //definicao arrays
        char strings_hashes[n_lines][max_chars_string];//aray de string com as strings lidas do ficheiro
            char * pointer_strings = &strings_hashes[0][0];//ponteiro para o inicio do array das hashes

        read_lines(filename,strings_hashes,0,n_lines); // le as strings do ficheiro e passa para o array

        for(i = 0;i<n_lines;i++){
            printf("%s \n",strings_hashes[i]);
        }
        printf("[%d] My parent is: %d\n", getpid(), getppid());

        //open pipe to write and close pipe to read

        close(channel[mypipe][0]);
        i = 0;
        int incr = 0;
        while (i<n_lines) {
            printf("[Son] Password sent e  %s: \n",strings_hashes[incr]);
             write(channel[mypipe][1], strings_hashes[incr], strlen(strings_hashes[incr]));
             incr++;
             i++;
        }
        exit(0);
    }

    int main(int argc, char **argv)
    {
        char *filename;
        int status;//status do processos filho
        int resources[2];// numero de linhas que cada processo tem de ler
        int n_lines; //numero de linhas do ficheiro
        int i = 0;

          // Create a pipe
        filename = (char*)malloc(strlen(argv[1])*sizeof(char)+1);

        if(argc !=3){
            fprintf(stderr, "Usage : %s [text_file] [cores]",argv[0]);
            exit(0);
        }

        strcpy(filename,argv[1]);

        char get_file [strlen(filename)];
        strcpy(get_file,filename);

     // start the processes
       for(i = 0; i <atoi(argv[2]);i++){
            pipe(channel[i]);
            childs[i] = fork();

            if(childs[i] == -1){
               perror("Failed to fork");
               return 1;
           }
           if (childs[i] == 0)
           {
              worker(i,get_file,n_lines);
           }
           close(channel[i][1]);
       }

               i = 0;
               int k = 0;
               int fd;
               fd_set read_set;
               FD_ZERO(&read_set);
               char string_lida [30];

               // working father
               printf("[%d] I'm the father!\n", getpid());
               printf("[Father]orking ...\n");

                //unammed_pipes connection
                while(k<n_childs){

                    FD_SET(channel[0][0], &read_set);
                    for(i=0;i<n_childs;i++){
                            FD_SET(channel[i][0], &read_set);
                            if(fd<channel[i][0]){      fd=channel[i][0];
                            }
                        }

                    if(select(fd+1,&read_set,NULL,NULL,NULL)>0){
                        for(i=0;i<n_childs;i++){
                            if(FD_ISSET(channel[i][0], &read_set)){                    
                                read(channel[i][0],string_lida,sizeof(string_lida));
                                printf("[Father]pipe %d - string lida:%s\n",i,string_lida);

                                k++;                 
                            }
                        }
                    }
                    fd=1;
                }
              //
              //waiting for childs ...
            for(i=0;i<n_childs;i++){
                wait(&status);
                printf("waiting for childs \n");
              }

      return 0;
    }  


    void get_strings_hash(char (*pointer_strings)[max_chars_string],char (*pointer_hashes)[max_chars_string],int total_lines)//vai ao array de strings e corta a parte de hash e mete num array
    {
        int i = 0;
        char *strings;
        char *hash;

        for(i = 0;i<total_lines;i++){
                strings = (char*)malloc(strlen(pointer_strings)*sizeof(char)+1);
                strcpy(strings,*pointer_strings);
                hash = (char*)malloc(strlen(pointer_strings)*sizeof(char)+1);
                find_hash(strings,hash);
                strcpy(*pointer_hashes,hash);
            pointer_hashes++;
            pointer_strings++;
        }

    }


    void read_lines(char * filename, char (*pointer)[max_chars_string],int init_read,int n_lines){ 
        FILE *fp;
        char str[max_chars_string];
        int i =0;

        if((fp = fopen(filename, "r"))==NULL) {
          printf("Cannot open file.\n");
          exit(1);
        }

        if(init_read>0 && init_read<=n_lines){
         for(i = 0;i<init_read;i++){
             fgets(str, sizeof str, fp);
           for(i = init_read;i<n_lines;i++){
               fgets(str, sizeof str, fp);
               strcpy(*pointer, str); //copia para a posicao actula do ponteiro
               pointer++;
           }
         }
        }
        if(init_read<=n_lines && init_read==0){
           for(i = init_read;i<n_lines;i++){
                fgets(str, sizeof str, fp);
               strcpy(*pointer, str); //copia para a posicao actula do ponteiro
               pointer++;
           }
         }


      fclose(fp);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:47:06+00:00Added an answer on June 12, 2026 at 6:47 pm

    When the child does

    write(channel[mypipe][1], strings_hashes[incr], strlen(strings_hashes[incr]));
    

    strlen() doesn’t count the null terminator of the string. So when the parent reads from the pipe, it doesn’t receive a null-terminated string. Then when it does:

    printf("[Father]pipe %d - string lida:%s\n",i,string_lida);
    

    It keeps printing past the end of what was written, because there’s no string terminator.

    Also, there’s no guarantee that the amount read by read() will match up with the amount written each time write() was called. If the child writes two strings before the parent reads from the pipe, it may get all or parts of both, or it could get even less than was written.

    You need to implement a way to indicate where the string boundaries are. You could send the size of the string first as one or two bytes, or you could use a null terminator. When the parent is reading, it needs to do this in a loop until it gets a whole string, and if it goes past the end it may need to save it in a buffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files client.php and server.php. The client file send a HTTP request
i have three process, one father and two sons,since the sons get by heritage
I have two classes (MVC view model) which inherits from one abstract base class.
I have two div tags, first div is the father and the second div
I have two tables that look like this: Products: id category name description active
I have two tables, products(product_id) and sex(product_id,sex_id). I am trying to DELETE rows from
Okay, I have two different background .jpgs that I want to used as the
We have two nodes in a cluster. Both run an ASP.NET web application that
I have two queries. The first returns some results and the second one returns
I have a Clojure main application that depends on several Clojure libraries, two of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.