Welcome To My blog hope you enjoy ^_^
Powered by Blogger.
RSS

Contoh codingan Sisop server

Hai hai masih bersama gue Aurum kali ini mau ngeshare codingan Sisop lab moga bermanfaat yah kawan :)




#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#include<unistd.h>
char *buffer;
#define flush scanf("%c",&buffer);
#include<time.h>

struct data{
  char name[40];
  char email[40];
  char vehicle[40];
  int qty;
};

int main(){
  // deklarasi shm / shared memory
  int shmid = shmget(123456, sizeof(struct data), IPC_CREAT|0666);
  struct data *p = (struct data *) shmat(shmid, 0, 0666);

  // deklarasi sem / semaphore
  int semid = semget(123456, 1, IPC_CREAT|0666);
  semctl(semid, 0, SETVAL, 0);

  // struct sembuf
  struct sembuf op;
  op.sem_num = 0;
  op.sem_op = -1;
  op.sem_flg = 0;

  system("clear");
  printf("==SERVER==\n");
  printf("==========\n\n");

  srand(time(NULL));

  while(1){
    semop(semid, &op, 1);
 
    //OUTPUT
    if(strcmp(p->name,"exit") == 0) break;
 
    char kode[5];
    kode[0] = rand()%('Z' - 'A' +1) + 'A';
    kode[1] = rand()%('Z' - 'A' +1) + 'A';
    kode[2] = rand()%( '9' - '0' +1) + '0';
    kode[3] = rand()%( '9' - '0' +1) + '0';
    kode[4] = '\0';
 
    printf("Kode    : %s\n", kode);
    printf("Name    : %s\n", p->name);
    printf("Email   : %s\n", p->email);
    printf("Vehicle : %s\n", p->vehicle);
    printf("Quatity : %d\n", p->qty);
 
 
 
  }

  shmdt(p);
  shmctl(shmid, IPC_RMID, 0);
  semctl(semid,  0, IPC_RMID);

  getchar();
  return 0;
}

HAPPY WATCHING ^_^
PANTENGIN TERUS BLOGGER AURUM
KARENA KALAU GAK
BAKAL RUGI

^_^

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS