akevintg

QUIZ II Operating System (SERVER)

Jan 6th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. // LINK SOAL : https://www.dropbox.com/sh/wussgfrqrmazf9g/AADnp_e-zKkHZWXyAFDQ5yJFa?dl=0
  2. // LINK TEMPLATE : http://pastebin.com/Rc7A08xG
  3.  
  4. #include <stdio.h>
  5. #include <sys/ipc.h>
  6. #include <sys/types.h>
  7. #include <sys/shm.h>
  8. #include <sys/sem.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. struct data{
  14.     char s_name[31];
  15.     char s_address[51];
  16.     char r_name[31];
  17.     char r_address[51];
  18.     char i_name[16];
  19.     float weight;
  20.     char type[10];
  21. };
  22.  
  23. int main(){
  24.     int random1,random2,randomhuruf;//ascii A-Z 65-90
  25.     float totalCost;//karena harus ada comanya
  26.     struct sembuf s;
  27.     int h=shmget(123456,sizeof(struct data),IPC_CREAT|0666);//h=shmid
  28.     int e=semget(123456,1,IPC_CREAT|0666);//e=semid
  29.     semctl(e,0,SETVAL,0);
  30.     struct data *deliv=(struct data*)shmat(h,NULL,0666);
  31.     printf("SNE Delivery Service - Client\n\n");
  32.     int flag=0;
  33.     do{
  34.         s.sem_num=0;
  35.         s.sem_op=-1;
  36.         s.sem_flg=0;
  37.         semop(e,&s,1);
  38.         if(strcasecmp(deliv->s_name,"exit")!=0){//pastiin input bukan exit
  39.             printf("SNE Delivery Receipt\n------------------------------------------\n");
  40.             srand(time(NULL));
  41.             random1=rand()%10;
  42.             srand(time(NULL));
  43.             random2=rand()%10;
  44.             srand(time(NULL));
  45.             randomhuruf=rand()%26+65;//baca jarak random 26 dimulai dari 65(A=65)
  46.             printf("Deliver Code \t\t: TK%d%d%c\n",random1,random2,randomhuruf);//printfnya satu2,%d%d%c
  47.             printf("Sender Name\t\t: %s\n",deliv->s_name);// "\t" untuk tab
  48.             printf("Sender Address\t\t: %s\n",deliv->s_address);
  49.             printf("Receiver Name\t\t: %s\n",deliv->r_name);
  50.             printf("Receiver Address\t: %s\n",deliv->r_address);
  51.             printf("Item Name\t\t: %s\n",deliv->i_name);
  52.             printf("Item Weight\t\t: %.2f kg(s)\n",deliv->weight);// %.2f = 2 angak di belakang koma
  53.             printf("Delivery Type\t\t: %s\n",deliv->type);
  54.             if(strcmp(deliv->type,"Express")==0)
  55.                 totalCost=deliv->weight*27500;
  56.             else
  57.                 totalCost=deliv->weight*15000;
  58.             printf("Total Cost\t\t: Rp %.2f\n",totalCost);
  59.             printf("\nThank you for the delivery\n\n");
  60.         }
  61.     }while(strcasecmp(deliv->s_name,"exit")!=0);
  62.     shmdt(&deliv);
  63.     shmctl(h,IPC_RMID,0);
  64.     semctl(e,0,IPC_RMID);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment