Thread: Semaphors Understanding/Semafor with fork()

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    12

    Semaphors Understanding/Semafor with fork()

    Hi, I need to help with semafor implementation understanding. I meant I understood the logic of it based on available sources, but during implementation I am not able to get the desire outcome.

    My goal is to lead with the help of semafor writing into pipes.

    This is my implementation:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    #include <semaphore.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/wait.h>
    
    
    #define SEM_WRITE "SemaphorForControlWrite"
    
    
    int main()
    {
        if(mkfifo("mySemaphorPipe", 0777) == -1)
        {
            if(errno != EEXIST)
            {
                printf("Something is wrong in Create...\n");
                return 1;
            }
        }
    
    
        sem_unlink(SEM_WRITE);
    
    
        sem_t *sem_PipeWrite = sem_open(SEM_WRITE, IPC_CREAT, 0660, 0);
    
    
        if(fork()==0)
        {
            // Create few children
            fork();
            fork();
            fork();
    
    
            while ( 1 )
            {   
                sem_wait(sem_PipeWrite);
                int fd = open("mySemaphorPipe", O_WRONLY);
                if (fd == -1)
                {
                    printf("Something is wrong in Open...\n");
                    return 1;
                }
    
    
                char buf[ BUFSIZ ];
                srand(getpid());
                sprintf( buf, "Process: %d - Send Number: %d", getpid(), rand() % 10000 );
                
                if(write( fd, buf, strlen(buf))==-1)
                {
                    printf("Write ERRoR of process %d\n", getpid());
                    fflush(stdout);
                }
                //sleep (1);
                close(fd);
                sem_post(sem_PipeWrite);
            }
            return 0;
        }
    
    
        printf("READ Pipe ... \n");
        fflush(stdout);
    
    
        while(1)
        {
            int fd = open("mySemaphorPipe", O_RDWR);
            if (fd == -1)
            {
                printf("Something is wrong in Open...\n");
                return 1;
            }
            char recievedData[1024]="";
            if(read(fd, &recievedData, sizeof(recievedData)) == -1)
            {
                printf("Something is wrong in read...\n");
                return 1;
            }
            printf("I get from pipe: %s", recievedData);
            fflush(stdout);
            close(fd);
         }
    
    
        return 0;
    }
    anzwaz if I use semafors, I will gain data only from one process and I cannot figure out where I am doing mistake in my logic.
    Could I ask for help and need to explain where I am doing mistake and how to avoid it.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    How many processes do you want? Because you 7 running, I think.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    12
    Quote Originally Posted by hamster_nz View Post
    How many processes do you want? Because you 7 running, I think.
    I don't care about a specific count. And in this case i think i have 8+1 process.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Hey! Found it....

    For sem_open, you need O_CREAT, not IPC_CREAT.

    Also, your name (SEM_WRITE) MUST begin with a leading slash.

    And the semaphore needs an initial value of 1.

    And you should test the return value from sem_open()

    Code:
    #define SEM_WRITE "/testsem"
    
        sem_t *sem_PipeWrite = sem_open(SEM_WRITE, O_CREAT, 0660, 1);
        if(sem_PipeWrite == SEM_FAILED) {
            perror("Something is wrong");
            return 0;
        }
    The other named pipe should most likely have mode 0666, not 0777
    Last edited by hamster_nz; 11-29-2020 at 04:29 PM.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Oh, you also need to have a non-blocking open on the pipe:

    Code:
           int fd = open("mySemaphorPipe", O_RDWR|O_NONBLOCK);
    From "man 7 fifo"

    The FIFO must be opened on both ends (reading and writing) before data can be passed. Normally, opening the FIFO blocks until the other end is opened also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-27-2014, 03:53 AM
  2. Replies: 3
    Last Post: 02-25-2013, 03:20 PM
  3. Understanding fork() example
    By Frankie15 in forum C Programming
    Replies: 8
    Last Post: 10-02-2012, 03:47 PM
  4. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 PM
  5. understanding fork() ???
    By ^^B.H.A^^ in forum C Programming
    Replies: 2
    Last Post: 12-05-2003, 11:12 AM

Tags for this Thread