gpt4 book ai didi

c - 访问 C linux 中 fork 之前创建的子进程中的字符串数组

转载 作者:行者123 更新时间:2023-11-30 17:57:23 25 4
gpt4 key购买 nike

我有三个进程,一个父亲和两个儿子,因为儿子们通过继承获得了在我 fork 之前创建的所有内容,我如何访问我之前从儿子或父亲那里创建的字符串数组? 我想访问我在 fork 之前创建的两个字符串数组,其中包含 string_hashes 的名称以及来自儿子和父亲的哈希值? 感谢您的帮助...

#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);

最佳答案

您不必做任何特别的事情。目前您已在 main() 中定义它们。要么全局定义它们,要么将它们传递给您的 worker() (即子函数)。

请记住,它们现在是同一事物的 3 个独立副本,因此如果一个进程更改了它们,这些更改将不会反射(reflect)在其他进程中。如果这是您的意图,那么最简单的方法可能是将它们放入共享内存中并使用互斥体保护它们。

关于c - 访问 C linux 中 fork 之前创建的子进程中的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12849708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com