Advertisement
akevintg

(CLIENT) CLIENT-SERVER (Session 7)

Jan 4th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 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.  
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <sys/sem.h>
  10. #include <sys/types.h>
  11.  
  12. struct data{
  13.     char phone_name[100];
  14.     int quantity;
  15. };
  16.  
  17. int main(){
  18.     int flag=0;
  19.     struct sembuf tes;
  20.     int hId=shmget(123456, sizeof(struct data),IPC_CREAT|0666);
  21.     int eId=semget(123456,1,IPC_CREAT|0666);
  22.     struct data *hp=(struct data *)shmat(hId,NULL,0666);
  23.     semctl(eId,0,SETVAL,0);
  24.     do{
  25.         do{
  26.             flag=0;
  27.             printf("Masukan Merek HP[Nokia|Samsung|Apple][\"exit\" to close the program]: ");
  28.             scanf("%[^\n]",hp->phone_name);
  29.             getchar();
  30.             if(strcasecmp(hp->phone_name,"nokia")==0||strcasecmp(hp->phone_name,"samsung")==0||strcasecmp(hp->phone_name,"apple")==0)
  31.                 flag=1;
  32.             else if(strcmp(hp->phone_name,"exit")==0)
  33.                 break;
  34.         }while(flag==0);
  35.         if(strcmp(hp->phone_name,"exit")!=0){
  36.             do{
  37.                 flag=0;
  38.                 printf("Masukan Jumlah HP[1...8]: ");
  39.                 scanf("%d",&hp->quantity);
  40.                 getchar();
  41.                 if(hp->quantity>0&&hp->quantity<9)
  42.                     flag=1;
  43.             }while(flag==0);
  44.         }
  45.         tes.sem_num=0;
  46.         tes.sem_op=1;
  47.         tes.sem_flg=0;
  48.         semop(eId,&tes,1);
  49.         printf("press enter...");
  50.         getchar();
  51.     }while(strcmp(hp->phone_name,"exit")!=0);
  52.     shmdt(&hp);
  53.     shmctl(hId, IPC_RMID, 0);
  54.     semctl(eId, 0, IPC_RMID);
  55.     return 0;
  56. }
  57.  
  58. // cd Desktop/UAP/AK
  59. // gcc lat02_quiz2_client.c -o c2
  60. // gcc lat02_quiz2_server.c -o s2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement