akevintg

(SERVER) CLIENT-SERVER (Session 7)

Jan 4th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. //link soal : https://www.dropbox.com/s/mbcwtg81dvfa9gc/O1-T0316-RG01-07.pdf?dl=0
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7.  
  8. #include <sys/sem.h>
  9. #include <sys/shm.h>
  10. #include <sys/types.h>
  11. #include <sys/ipc.h>
  12.  
  13. struct data{
  14.     char phone_name[100];
  15.     int quantity;
  16. };
  17.  
  18. int main(){
  19.     int total;
  20.     srand(time(NULL));
  21.     int random_satuan=rand()%2000000+5000000;
  22.     struct sembuf tes;
  23.     int hId=shmget(123456, sizeof(struct data),IPC_CREAT|0666);
  24.     int eId=semget(123456,1,IPC_CREAT|0666);
  25.     struct data *hp=(struct data *)shmat(hId,NULL,0666);
  26.     do{
  27.         tes.sem_num=0;
  28.         tes.sem_op=-1;
  29.         tes.sem_flg=0;
  30.         semop(eId,&tes,1);
  31.         if(strcmp(hp->phone_name,"exit")!=0){
  32.             printf("Nama HP : %s\n",hp->phone_name);
  33.             printf("Quantity : %d\n",hp->quantity);
  34.             total=random_satuan*hp->quantity;
  35.             printf("perUnit : %d\n",random_satuan);
  36.             printf("Total : %d\n",total);
  37.         }
  38.         else
  39.             printf("Keluar Program...");
  40.     }while(strcmp(hp->phone_name,"exit")!=0);
  41.     shmdt(&hp);
  42.     shmctl(hId, IPC_RMID, 0);
  43.     semctl(eId, 0, IPC_RMID);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment