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 8660929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:22:46+00:00 2026-06-12T16:22:46+00:00

i have three process, one father and two sons,since the sons get by heritage

  • 0

i have three process, one father and two sons,since the sons get by heritage everything that was created before i forked how do i access the arrays of strings that i have created before from the son or the father?
i want to access the two arrays of strings that i have created before i forked with the name of string_hashes and hashes from the sons and the father?
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>




#define PIPE_NAME   "np_workmaster"
#define max_chars_string 1000
#define n_childs 2

typedef struct
{
  int a;
  int b;
} numbers;



pid_t childs[n_childs];

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(){ // meter as funcoes de ler passwords no filho
    char * ant = "";
    char hash_char;

    printf("[%d] I'm the son!\n", getpid());
    printf("[%d] My parent is: %d\n", getpid(), getppid());
    exit(0);
}

void pparent(){// sera o ultimo processo a terminar mostrando as passwords ja recebidas do dispatcher que recebe dos outros filhos
    printf("[%d] I'm the father!\n", getpid());
    //dispatcher() // criar funcao dispatcher que envia dados ao outro processo...

  int fd;
  if ((fd=open(PIPE_NAME, O_WRONLY)) < 0)
  {
    perror("Cannot open pipe for writing: ");
    exit(0);
  }

  // Do some work  
  while (1) {
       // here i  want to access arrays strings_hashes and hashes from the father and sons

    printf("[CLIENT] Sending (%d,%d) for adding\n", n.a, n.b);
    write(fd, &n, sizeof(numbers));
    sleep(2);
  }

  return 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
    //char * chave_iniciadora = "";
    int n_lines; //numero de linhas do ficheiro
    int i = 0;



    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]);
    n_lines = count_lines(filename); // contem o numero de linhas
    //definicao arrays
    char strings_hashes[n_lines][max_chars_string];//aray de string com as strings lidas do ficheiro
    char hashes[n_lines][max_chars_string]; // array de strings com as hashes
    char * pointer_string = &strings_hashes[0][0]; // ponteiro para o inicio do array das strings lidas do ficheiro
    char * pointer_hashes = &hashes[0][0];//ponteiro para o inicio do array das hashes

    //funcoes
    share_resources(atoi(argv[2]),n_lines,resources);
    read_lines(filename,strings_hashes,0,n_lines); // le as strings do ficheiro e passa para o array
    get_strings_hash(strings_hashes,hashes,n_lines);
     //
    for(i = 0; i<n_lines;i++){
        printf("%s",strings_hashes[i]);
    }
    for(i = 0; i<n_lines;i++){
        printf("%s",hashes[i]);
    }

    //   
  for(i = 0; i <atoi(argv[2]);i++){
        childs[i] = fork();
        if(childs[i] == -1){
           perror("Failed to fork");
           return 1;
       }
       if (childs[i] == 0)
       {
          worker();
       }
       else
       {
          pparent();
          wait(&status);
          if (!status)
            printf("\nOK\n");
          else 
            printf("\nSomething is wrong...\n");
    }
   }
  return 0;
}



///////////////////////////////////////////////////////////////////



//funcionar
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++;
    }

}



//funcionar
int count_lines(char * filename){ 
    FILE *fp;
    char str[max_chars_string];
    int i =0;

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

    while(!feof(fp)) {
        while(fgets(str, sizeof str, fp)) {
           i++;
      }

  }
  fclose(fp);
  return i;
}
//funcionar
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-12T16:22:46+00:00Added an answer on June 12, 2026 at 4:22 pm

    You don’t have to do anything special. Currently you have them defined in main(). Either define them globally or pass them to your worker() (i.e. child) function.

    Keep in mind they are now 3 separate copies of same thing so if one process changes them those changes will not be reflected in the other processes. If that is your intent then it probably easiest to put them in shared memory and protect them with a mutex.

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

Sidebar

Related Questions

I have a three-step process that is entirely reliant upon JavaScript and Ajax to
Suppose we have a text editor application that runs with one separate process per
In My application have time consuming process.There fore i try to do that operation
I have some code that creates a Process instance and later starts it. There's
Have three divs in a container that I want to float over a large
Ok, I have two tables that I am joining and doing a select on
I have one solution with three projects. DomainModel (C# Library with ADO.NET Entity Framework)
Say I have file.php with three functions and an echo statement: function one() {
I have three windows services in one C# project. Using the installer class (it
I am facing one problem regarding threading scenario. I have three threads in my

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.