Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdbool.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. #include<semaphore.h>
  7. #include<unistd.h>
  8. #include<stdio.h>
  9. #include<stdlib.h>
  10. #include<sys/wait.h>
  11. #include<sys/mman.h>
  12. #include<stdbool.h>
  13. #include <sys/stat.h>        /* For mode constants */
  14.        #include <fcntl.h>
  15. #include<math.h>
  16. #define ARRAY_MAX (1024)
  17. #define MAX (256)
  18.  
  19. #define osAssert(cond, userMsg) osErrorFatal(cond, userMsg,__FILE__,__LINE__)
  20.  
  21. void osErrorFatal(bool cond, const char* userMsg, const char* file, const int line){
  22.     if(!cond){
  23.         perror(userMsg);
  24.         fprintf(stderr,"File:%s:%d\n",file, line);
  25.         exit(EXIT_FAILURE);
  26.     }
  27. }
  28.  
  29. typedef struct {
  30. sem_t inDataReady;
  31. float array[ARRAY_MAX];
  32. unsigned arrayLen;
  33. } OsInputData;
  34.  
  35. void *getmem(char* name,int *size){
  36.  
  37.     int memfd=shm_open(name,O_RDWR,0600);
  38.     osAssert(-1!=memfd,"smh");
  39.  
  40.     struct stat finfo;
  41.     osAssert(-1!=fstat(memfd,&finfo),"stat");
  42.     *size=finfo.st_size;
  43.  
  44.     void *addr;
  45.  
  46.     osAssert(MAP_FAILED!=(addr=(mmap(NULL,*size,PROT_READ | PROT_WRITE,MAP_SHARED,memfd,0))),"mmap");
  47.     close(memfd);
  48.     return addr;
  49. }
  50.  
  51. int main(int argc,char** argv){
  52.  
  53.     osAssert(argc == 3,"args missing");
  54.     int size;
  55.     int size1;
  56.  
  57.     OsInputData* mem1=getmem(argv[1],&size);
  58.     OsInputData* mem2=getmem(argv[2],&size1);
  59.  
  60.     mem2->arrayLen=0;
  61.  
  62.     osAssert(-1!=sem_wait(&(mem1->inDataReady)),"sem wait");
  63.  
  64.     for(int i=0;i<mem1->arrayLen;i++){
  65.         int broj=mem1->array[i];
  66.         int br=0;
  67.         unsigned mask=1<<(sizeof(int)*8-1);
  68.         while(mask){
  69.             if(broj&mask)
  70.                 br++;
  71.            
  72.         }
  73.         if(br>=4){
  74.             mem2->array[mem2->arrayLen++]=mem1->array[i];
  75.         }
  76.     }
  77.  
  78.     osAssert(-1!=sem_post(&(mem2->inDataReady)),"sme post");
  79.     osAssert(-1!=munmap(mem1,size),"mem1 munmap");
  80.     osAssert(-1!=munmap(mem2,size1),",e,2 munmap");
  81.  
  82.  
  83.  
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement