niedziela, 2 września 2018

pthread_create, pthread_join (gcc)

vim three_creads.c

#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define max 100
struct int_params
{
 int int_value;
 int count;
};
struct double_params
{
 double double_value;
 int count;
};
struct char_params
{
 char char_value;
 int count;
};

int *int_tab;
char *char_tab;
double *double_tab;



void init_Tables()
{

 int i;
 int_tab=(int*) calloc (max,sizeof(int));
 char_tab=(char*) calloc(max,sizeof(char));
 double_tab=(double*) calloc(max,sizeof(double));
 for(i=0;i<max;i++)
 {
     int_tab[i]=i+1;
     char_tab[i]='=';
     double_tab[i]=sqrt(1.*int_tab[i]);
 }


}

void delete_Tables()
{
    free(double_tab);
    free(char_tab);
    free(int_tab);
}
void *int_Print(void *value)
{
    int i;
    struct int_params *temp=(struct int_params*) value;
    for(i=0;i<temp->count;i++)
    {
        printf("%d ",temp->int_value);
        printf("\n");
    }
   
    return NULL;
}

void *char_Print(void *value)
{
    int i;
    struct char_params *temp=(struct char_params*) value;
    for(i=0;i<temp->count;i++)
    {
     printf("%c ",temp->char_value);
     printf("\n");
    }
    return NULL;
}
void *double_Print(void *value)
{
    int i;
    struct double_params *temp=(struct double_params*) value;
    for(i=0;i<temp->count;i++)
    {
     printf("%.2f ",temp->double_value);
     printf("\n");
    }
    return NULL;
}


pthread_t int_id,char_id,double_id;

int main()
{
    int i;                
    init_Tables();
    struct int_params i_P[max];
    struct char_params c_P[max];
    struct double_params d_P[max];
    for(i=0;i<max;i++)
    {
        i_P[i].int_value=int_tab[i];
        i_P[i].count=max;
        c_P[i].char_value=char_tab[i];
        c_P[i].count=max;
        d_P[i].double_value=double_tab[i];
        d_P[i].count=max;
    }
    pthread_create(&int_id,NULL,&int_Print,&*i_P);
    pthread_create(&char_id,NULL,&char_Print,&*c_P);
    pthread_create(&double_id,NULL,&double_Print,&*d_P);
   
    pthread_join(int_id,NULL);
    pthread_join(char_id,NULL);
    pthread_join(double_id,NULL);

    delete_Tables();

 return 0;
}


gcc -o three_creads three_creads.c -lpthread -lm

sobota, 1 września 2018

Dynamic libraries in C (gcc)

vim my_cpp_function.so

bool test_First(int value)
{
 int i,sum;
 sum=0;
 for(i=1;i<value+1;i++)
  if(value%i==0)
   ++sum;
 if(sum==2)
  return true;
 else
  return false;
}
int is_First(int value)
{
 if(test_First(value))
  return 1;
 else
  return -1;
}
double value_Average(int *tab, int size);
{
 int i,sum;
 double value;
 value=1.0;
 sum=0;
 for(i=0;i<size;i++)
 {
  sum+=tab[i];
 
 }
 value=(1.*sum)/(1.*size);
 return value;

}






vim test_dynamic.c

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#define max 1000
int main()
{
int *tab_First;
char *text_Error;
int is_F(int value);
double v_A(int *tab, int size);
double average;
int i,j;
#ifdef __cplusplus
 extern "C"
{
#endif
 int  is_First (int value);
 double value_Average(int *tab, int size);
#ifdef __cplusplus
}
#endif
void *handle=dlopen("my_cpp_function.so",RTLD_LAZY);
if(!handle)
{
 fprintf(stderr,"%s\n",dlerror());
 exit(EXIT_FAILURE);
}
dlerror();
*(void**)(&is_F)=dlsym(handle,"is_First");


if((text_Error=dlerror())!=NULL)
{
 fprintf(stderr,"%s\n",text_Error);
 exit(EXIT_FAILURE);
}

*(void**)(&v_A)=dlsym(handle,"value_Average");
if((text_Error=dlerror())!=NULL)
{
 fprintf(stderr,"%s\n",text_Error);
 exit(EXIT_FAILURE);
}


tab_First=(int*) calloc(max,sizeof(int));
if(tab_First==NULL)
 exit(1);
i=0;
j=2;
do
{
 if(is_F(j))
 {
  tab_First[i]=j;
  ++i;
 }
 ++j;
}while(i<max);
average=v_A(tab_First,max);
for(i=0;i<max;i++)
{
 printf("%d \n",tab_First[i]);
}
printf("Average: %.2f ",average);
printf("\n");
dlclose(handle);
free(tab_First);
 return 0;
}


gcc  -shared -rdynamic  -o test_dynamic test_dynamic.c -ldl