I have a multiprocess and I want to block other processes with Semaphores to stop the acces for the function in the middle of the pseudo code.
while(true){
fork()
reset buf
getting input
while(getting input){
if(true) {give only acces to this process for the next function}
function()
if(true){stop acces to only that one process for the function}
reset buf
getting input
}
}
So I want to activate and close the Semaphores with the statements, to give the acces only to that one process which activate the statement, other processes has to wait.
How to implement this in C?
I hope my probleme is clear enough to understand
EDIT 1:
right now I try to use sysV
unsigned short marker2[2];
struct sembuf sem_down;
sem_down.sem_num = 0;
sem_down.sem_op = -1;
sem_down.sem_flg = 0;
struct sembuf sem_up;
sem_up.sem_num = 0;
sem_up.sem_op = +1;
sem_up.sem_flg = 0;
sem_id = semget (IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | 0600);
marker2[0] = 1;
semctl(sem_id, 1, SETALL, marker2);
while(true) {
fork();
while(input){
if (strncmp("BEG", inputBuffer[0], 3) == 0) {
semop (sem_id, &sem_down, 1);
printf("blocked from %d\n", getpid());
}
conditions(inputBuffer[0],
inputBuffer[1],
inputBuffer[2],
cfd, semID1, shmID);
if (strncmp("END", inputBuffer[0], 3) == 0) {
semop (sem_id, &sem_up, 1);
printf("open by %d\n", getpid());
}
}
}
right now, other processes doesn't get blocked
Edit 2:
sem_t semvar;
sem_init(&semvar, 0,1);
while(true) {
fork();
while(input){
if (strncmp("BEG", inputBuffer[0], 3) == 0) {
sem_wait(&semvar);
printf("blocked from %d\n", getpid());
}
conditions(inputBuffer[0],
inputBuffer[1],
inputBuffer[2],
cfd, semID1, shmID);
if (strncmp("END", inputBuffer[0], 3) == 0) {
sem_post(&semvar);
printf("open by %d\n", getpid());
}
}
still doesn't work