akevintg

MATERI UAP I (SERVER)

Jan 19th, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. //LINK SOAL : https://www.dropbox.com/s/s0qki5su9pdoj8d/Screen%20Shot%202016-01-19%20at%208.07.24%20PM.png?dl=0
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <sys/ipc.h>
  8. #include <sys/sem.h>
  9. #include <sys/shm.h>
  10. #include <sys/types.h>
  11.  
  12. struct data{
  13.     char namaUser[100];
  14.     char namaUserDepan[100];
  15.     char namaUserBelakang[100];
  16.     char genre[100];
  17.     char title[100];
  18. };
  19.  
  20. int main(){
  21.     struct sembuf s;
  22.     int h=shmget(123456,sizeof(struct data),IPC_CREAT|0666);
  23.     int e=semget(123456,1,IPC_CREAT|0666);
  24.     semctl(e,0,SETVAL,0);
  25.     int flag,i;
  26.     struct data *buku=(struct data*)shmat(h,NULL,0666);
  27.     printf("==========================================\nE-LIBRARY\nSERVER SIDE\n==========================================\n");
  28.     do{
  29.         printf("\nWaiting For Confirmation\n");
  30.         s.sem_num=0;
  31.         s.sem_op=-1;
  32.         s.sem_flg=0;
  33.         semop(e,&s,1);
  34.         if(strcasecmp("Exit",buku->namaUser)!=0){
  35.             //karena harus di balik printnya
  36.             //nama bekalang, nama depan
  37.             printf("Nama User : %s, %s\n",buku->namaUserBelakang,buku->namaUserDepan);
  38.             srand(time(NULL));
  39.             int randomAngka;
  40.             randomAngka=rand()%900+100;
  41.             printf("Book Code : %c%c%d\n",buku->title[0],buku->title[1],randomAngka);
  42.             int price,randomPrice;
  43.             randomPrice=rand()%71+30;
  44.             if(strcasecmp("Horror",buku->genre)==0){
  45.                 price=500*randomPrice;
  46.             }
  47.             else if(strcasecmp("Science",buku->genre)==0){
  48.                 price=800*randomPrice;
  49.             }
  50.             else if(strcasecmp("Fantasy",buku->genre)==0){
  51.                 price=600*randomPrice;
  52.             }
  53.             printf("Book Price : Rp. %d,00\n",price);
  54.             int pay;
  55.             do{
  56.                 printf("Masukan Payment : ");
  57.                 scanf("%d",&pay);fflush(stdin);getchar();
  58.             }while(pay<price);
  59.             printf("Change : Rp. %d,00\n",pay-price);
  60.         }
  61.     }while(strcasecmp("Exit",buku->namaUser)!=0);
  62.     shmdt(&buku);
  63.     shmctl(h,IPC_RMID,0);
  64.     semctl(e,0,IPC_RMID);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment