Cod sursa(job #2489877)

Utilizator lucianistratiIstrati Lucian lucianistrati Data 9 noiembrie 2019 12:42:16
Problema Aria Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 2.34 kb
#include <sys/mman.h>
	
#include <sys/stat.h>
	
#include <fcntl.h>
	
#include <stdio.h>
	
#include <unistd.h>
	
#include <errno.h>
	
#include <sys/types.h>
	
#include <sys/wait.h>
	
#include <stdlib.h>
	
#include <string.h>
	
#include <pthread.h>
	
//#include <libpthread>
	
//Laburi 1 , 2, 0, 1
	
int a[3][3],b[3][3],c[4][4];
	
struct Coord
	
{
	
   int x,y;
	
};
	
pthread_t Threaduri[4][4];
	
void * hello(void *v)
	
{
	
	char *who = (char*) v;
	
	printf("Hello, %s!", who);
	
	return NULL;
	
}
	
 
	
char *strrev(char *str)
	
{
	
      char *p1,*p2;
	
      if (!str || !*str)
	
            return str;
	
      for(p1=str,p2=str+strlen(str)-1;p2>p1;++p1,--p2)
	
      {
	
            *p1^=*p2;
	
            *p2^=*p1;
	
            *p1^=*p2;
	
      }
	
      return str;
	
}
void * stringReverse(void *v)
{
	char *who = (char*) v;
    char aux;
	int i;
	printf("%s\n", strrev(who));	
	return NULL;
}
	
void init()
	
{
	
    a[0][0]=1;
	
	a[0][1]=2;
	
  	a[1][0]=3;
	
	a[1][1]=4;
	
	b[0][0]=1;
	
	b[0][1]=2;
	
  	b[1][0]=3;
	
	b[1][1]=4;
	
}
int rez=-1;
void* inmultire_element(void *v)	
{
    int i,j,k;
    //printf("a");
    for(i=0;i<=1;i++)
	{    
	  for(j=0;j<=1;j++)
      {
      c[i][j]=0;
      for(k=0;k<=1;k++)
      {
         c[i][j] += a[i][k]*b[k][j];
      }
      }
	}
	pthread_exit(0);
}
	
void inmultire()
	
{
	
	int i,j,p,q;
	
	for(i=0;i<=1;i++)
	 for(j=0;j<=1;j++)
	{
	
		struct Coord* z = (struct Coord *)malloc(sizeof(struct Coord));
	    z->x=i;
	    z->y=j;
        pthread_create(&Threaduri[i][j], NULL, inmultire_element, &z/*(void*)(s)*/); 
    } 
    for(i=0;i<=1;i++)  
     for(j=0;j<=1;j++)
    {
	    pthread_join(Threaduri[i][j], NULL);
	}   
}
void afisare()	
{
 	int i,j;	
	for(i=0;i<=1;i++)
		{
			for(j=0;j<=1;j++)
			{
				printf("%d ",c[i][j]);
			}
		printf("\n");
		}		
}
int main(int argc, char *argv[])	
{
	init();	
	int i,k=0;
	pthread_t thr;

    char s[50],t[50];
	
    for(i=2;i<strlen(argv[0]);i++)
	
	{
	
		s[k++]=argv[0][i];
	
	}
	
	s[k]='\0';
	
	k=0;
	
	for(i=0;i<strlen(argv[1]);i++)
	
	{
	
		t[k++]=argv[1][i];
	
	}
	
	t[k]='\0';
	
	//printf("%s %s",s,t);	
	
	int result;
	
	//printf("hey\n");
	
	// -- Program 1
	if (pthread_create(&thr, NULL, stringReverse, t)) {
	
	  perror(NULL);
	
	  return errno;
	
	}
	
	if(pthread_join(thr, &result)){	
	
		perror(NULL);
	
		return errno;	
	
	}
	
	//Program 2
	
   inmultire();
	
   afisare();
	
}