I'm trying to make a program that takes a text from a file, and after that, it tells me if that text contains a string that I want tho search. The idea is that I must do everything with a process and multiple threads. The number of threads it's a variable number (I should tell from the terminal how many threads I want). If for example, I want 4 threads, the text from the file will be split into 4 strings, and every thread will work with their own string and will search in that string what word I want to find.
For verifying myself that everything works ok, I tried to print some variables on the screen (in server.c). And here I encountered a problem and I can't figure it out. Even if I told the program to print on the screen the "init" variable, it doesn't print anything. Size is printed, nr_thr is printed, everything it's ok until I reach the part when I say to print "init". And obvious, the entire program it's not working.
How can I fix it? It's ok the idea of the program?
This is server.c
###########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);
}
This is client.c, where I'm calling everything.
#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;
}