0

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

Kevin
  • 1
  • 3

1 Answers1

0

The basic operations on a semaphore are "acquire" -- waiting until the value is non-zero and atomically decrement it, and "release" -- increment the value (thus allowing someone waiting for am "acrquire" to proceed.)

So to allow only one process to access a critical section like you describe with a semaphore, you would initialize the semaphore value to 1, acquire the semaphore before the critical section, and release it afterwards. So your first "if(true)" should be "acquire" and the second should be "release"


So how do you actually do this in C? Well, C does not have any native semaphore types or operations. There are a number of different libraries that provide them -- the SysV semctl/semop calls, POSIX sem_open, WIN32 semaphores, and probably others. C11 does have native mutex types, but they are only process local (so can only be used for multithreading in a single process)

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • but if I do it like that, it doesn't work. And I'm not blocking other processes, but myself. – Kevin Jun 29 '20 at 19:51
  • All processes need to acquire the semaphore, so whoever gets it first will proceed and others will block. You can't block someone who's already acquired the resource until they release it -- that doesn't make any sense. – Chris Dodd Jun 29 '20 at 19:53
  • How do I use POSIX sem_open in that scenario, I don't understand it really – Kevin Jun 29 '20 at 19:53
  • See [semaphore implementation](https://stackoverflow.com/questions/15182328/semaphore-implementation) – David C. Rankin Jun 29 '20 at 20:10