#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#define SEMKEY 1234L
#define PERMS 0666
struct sembuf op_down[1] = {0,-1,0};
struct sembuf op_up[1] = {0,1,0};
int semid =-1;
int res;
void init_sem(){
semid=semget(SEMKEY,0,IPC_CREAT| PERMS);
if(semid<0){
printf("Anlegen der Semaphore\n");
semid = semget(SEMKEY,1,IPC_CREAT|PERMS);
if(semid<0){
printf("Anlegen der Sempaphore gescheitert!\n");
exit(-1);
}
res=semctl(semid,0,SETVAL,1);
}
}
void down(){
printf("down semid: %d\n",semid);
res=semop(semid,&op_down[0],1);
}
void up(){
res=semop(semid,&op_up[0],1);
}
int main(){
init_sem();
printf("Vor dem kritischen Code\n");
down();
printf("Im kritischen Code\n");
sleep(10);
up();
return(0);
} |