Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的线程问题_C_Multithreading_Process - Fatal编程技术网

C语言中的线程问题

C语言中的线程问题,c,multithreading,process,C,Multithreading,Process,我试图制作一个程序,从一个文件中提取一个文本,然后,它会告诉我该文本是否包含我想要搜索的字符串。我的想法是,我必须用一个进程和多个线程来完成所有事情。线程数这是一个可变的数字(我应该从终端知道我需要多少线程)。例如,如果我想要4个线程,文件中的文本将被分割成4个字符串,每个线程将使用自己的字符串,并在该字符串中搜索我想要查找的单词 为了验证自己一切正常,我尝试在屏幕上打印一些变量(在server.c中)。在这里我遇到了一个问题,我想不出来。即使我告诉程序在屏幕上打印“init”变量,它也不会打印

我试图制作一个程序,从一个文件中提取一个文本,然后,它会告诉我该文本是否包含我想要搜索的字符串。我的想法是,我必须用一个进程和多个线程来完成所有事情。线程数这是一个可变的数字(我应该从终端知道我需要多少线程)。例如,如果我想要4个线程,文件中的文本将被分割成4个字符串,每个线程将使用自己的字符串,并在该字符串中搜索我想要查找的单词

为了验证自己一切正常,我尝试在屏幕上打印一些变量(在server.c中)。在这里我遇到了一个问题,我想不出来。即使我告诉程序在屏幕上打印“init”变量,它也不会打印任何内容。尺寸已打印,nr_thr已打印,一切正常,直到我到达要打印“init”的零件。很明显,整个程序都不起作用了

我怎样才能修好它? 这个节目的想法怎么样

这是server.c

Server.c##########
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构argum{
字符串[100];
char-cautat[100];
};
空*尾(空*数据){
内特雷斯;
printf(“螺纹中的AM ajuns”);
结构argum*argum=数据;
int i,j,k;
if(strstr(argum->string,argum->cautat)){
printf(“字符串形式的Exista”);
rez=1;
}
否则{
printf(“字符串形式的Nu exista asa ceva”);
rez=0;
}
返回(无效*)rez;
}
int main(int argc,字符**argv){
struct argum*argum=malloc(sizeof(struct argum));
字符缓冲区[1000];
国际天然气公司;
int df1;
输入,输出;
int init=0;
int i;
char*a[100];
字符b[1000];
nr_thr=atoi(argv[3]);
printf(“\n Nr threaduri:%d\n”,Nr\u thr);
df1=打开(argv[1],O_RDWR);
如果(df1==-1){
佩罗尔(“爱欲者”);
//出口(1);
}
int size=lseek(df1,0,SEEK_END);
printf(“\n大小:%d\n”,大小);
lseek(df1,0,SEEK_集);

对于(i=0;iOn)的
printf
问题,请看这里:是的,它与那里的一个建议一起工作。现在我发现我的线程根本没有开始工作。知道如何修复它吗?首先,“argum->string[100]”是不允许的,因此UB:(如果根本不使用线程,程序可以工作吗?是的,程序可以拆分文本,所有内容都在它需要的地方。我认为问题在于结构和指针,但不知道在哪里。
###########Server.c##########
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>

struct argum{
  char string[100];
  char cautat[100];
};


void *cautare(void *data){
  int rez;
  printf("AM ajuns in thread");
  struct argum *argum = data;
  int i,j,k;
  if (strstr(argum->string,argum->cautat)){
    printf("Exista in string");
    rez=1;
  }
  else{
    printf("Nu exista in string asa ceva");
    rez=0;
  }

  return (void*)rez;
}

int main(int argc, char**argv){
  struct argum *argum = malloc(sizeof(struct argum));
  char buffer[1000];
  int nr_thr;
  int df1;
  ssize_t in, out;
  int init=0;
  int i;
  char *a[100];
  char b[1000];

  nr_thr=atoi(argv[3]);
  printf("\n Nr threaduri:%d \n",nr_thr);
  df1 = open(argv[1], O_RDWR);

  if (df1 == -1){
      perror("eroare deschidere ");
      //exit(1);
  }

  int size=lseek(df1, 0, SEEK_END);
  printf("\nSize:%d\n",size);

  lseek(df1, 0, SEEK_SET);

  for(i=0; i<=size; i++){
    if((lseek(df1, i, SEEK_SET))==-1){
      perror("Eroare in fisier");
    }

    in=read(df1,&buffer[i],1);
  }

  printf("In fisier este textul:");
  puts(buffer);

  init = size/nr_thr;

  printf("\nInitial: %d ",init);

  int k=0;
  int l=0;
  int j;

  while(init<=size){
    for(i=init; i<=size/nr_thr; i++){
      b[k]=buffer[i];
      k++;
    }
      a[l]=b;
      init=size/nr_thr;
      if(nr_thr>0){
        nr_thr--;
        l++;
      }
      k=0;
    }


  int rezultat;

  int *return_vals = malloc(nr_thr * sizeof(int));

  pthread_t threads[10];

  for(i=0; i<nr_thr; ++i){
    puts(a[i]);
    argum->string[100]= *a[i];
    argum->cautat[100]= *argv[2];
    pthread_create(&threads[i],NULL,cautare,argum);
  }

  for(i=0; i<nr_thr; ++i){
    rezultat=pthread_join(threads[i],(void**) &return_vals[i]);
    }

  for(i = 0; i < nr_thr; i++)
  printf("Thread %d returned: %d\n", i, return_vals[i]);

  exit(1);
}
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(){
  int id,d, stare;
  int nr_thr;
  char nume_fis[100], nume_cuv[100];
  char thr[100];
  d=1;
  while(d==1){
    printf("> ");
    scanf("%s %s %d",nume_fis,nume_cuv,&nr_thr);

    sprintf(thr,"%d",nr_thr);
    id=fork();

    if(id<0){
      perror("Eroare la creare proces");
      return -1;
    }
    else if(id==0){ //fiu
      execl("./server","server",nume_fis,nume_cuv,thr,NULL);
      perror("eroare la exec");
    }
    else{
      wait(&stare);
      if(WIFEXITED(stare)){
        printf("%d",WEXITSTATUS(stare));
      }
    }

    printf("\nContinuati?(1 pt da)");
    scanf("%d",&d);
  }
  return 0;
}