Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //link soal : https://www.dropbox.com/s/mbcwtg81dvfa9gc/O1-T0316-RG01-07.pdf?dl=0
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- struct data{
- char phone_name[100];
- int quantity;
- };
- int main(){
- int total;
- srand(time(NULL));
- int random_satuan=rand()%2000000+5000000;
- struct sembuf tes;
- int hId=shmget(123456, sizeof(struct data),IPC_CREAT|0666);
- int eId=semget(123456,1,IPC_CREAT|0666);
- struct data *hp=(struct data *)shmat(hId,NULL,0666);
- do{
- tes.sem_num=0;
- tes.sem_op=-1;
- tes.sem_flg=0;
- semop(eId,&tes,1);
- if(strcmp(hp->phone_name,"exit")!=0){
- printf("Nama HP : %s\n",hp->phone_name);
- printf("Quantity : %d\n",hp->quantity);
- total=random_satuan*hp->quantity;
- printf("perUnit : %d\n",random_satuan);
- printf("Total : %d\n",total);
- }
- else
- printf("Keluar Program...");
- }while(strcmp(hp->phone_name,"exit")!=0);
- shmdt(&hp);
- shmctl(hId, IPC_RMID, 0);
- semctl(eId, 0, IPC_RMID);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment