The program has to do it:
A process P0 creates P1 and P2;
sizeof(buffer) = N (inserts with command line);
P1 inserts random values in the first N/2 elements of the buffer (N insert by user)
P2 inserts random values in the second part of the buffer
After that: P1 inverts the second part of the buffer and then the process P0 print all elements of it
If the user presses CTRL+C ---> print the buffer elements and kill all process;
I don't know why , but process P1 remains in pause. I called in the concurrent process P2 the increase of the semaphore's value ("semaphore_inv") and the wait has to decrease it to 0. For this reason the program doesn't work correctly.
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
int N;
int buff[1024];
void print(int sig) {
  int i;
  for(i=0; i<N; i++) {
     printf("Slot %d of the buffer is %d\n",i, buff[i]);   
  }
  kill(0,SIGKILL);
}
void main (int argc, char* argv[]) {
    int p1, p2;
    sem_t semaphore_inv;
    sem_t semaphore_read;
    sem_t semaphore_write;
    struct sembuf sembuf;
    N=atoi(argv[1]);
    if (N<=0 || N>=1024) {
       printf("Inserirt a value > 0 and < 1024\n");
       exit(-1);
    }
    if (argc!=2) {
       printf("Insert com  N\n");
       exit(1);
    }
    int buffer[N];
    //I insert this type of semaphore only to try it
    int sem_write = semget(IPC_PRIVATE,1,IPC_CREAT|0666);
    if (sem_write <0) printf("Error in the semaphore creation\n");
    int sem_write_b = sem_init(&semaphore_write,1,1);
    if (sem_write_b<0) printf("Error in the semaphore creation\n");
    int sem_inv = sem_init(&semaphore_inv, 1, 0);
    if (sem_inv<0) printf("Error in the semaphore creation\n");
    int sem_read = sem_init(&semaphore_read,1,0);
    if (sem_read<0) printf("Error in the semaphore creation\n");
    int ret = semctl(sem_write, 0, SETVAL, 1);
    if (ret == -1) printf("Error: semctl, with  errno %s\n", strerror(errno));
    signal(SIGINT, print);
    p1 = fork();
    p2 = fork();
    if (p1  < 0) {
       printf("P1: error, fork\n");
       exit(-2);
    }
    if (p2 < 0) {
       printf("P2: error, fork\n");
       exit(-2);
    }
    if (p1==0) {
      loop:
        sembuf.sem_num=0;
        sembuf.sem_op= -1;
        sembuf.sem_flg=0;
        semop(sem_write, &sembuf, 1);   
        int i;
        for (i=0; i<N/2; i++) {
           buffer[i] = rand();
           printf("P1: the insert value in buffer[%d] is %d\n",i ,  buffer[i]);
        }
        sem_wait(&semaphore_inv);
        printf("P1: i'm going to invert the second part of the buffer\n");
        int j=1;
        for (i=N/2; i<N; i++){
           int buffer_prev;
           buffer_prev=buffer[i];
           buffer[i] = buffer[N-j];
           buffer[N-j] = buffer_prev;
           j++;
        }
        sem_post(&semaphore_read);
        sleep(1);
       goto loop;
    }
    if (p2==0) {
     loop_b:
        sem_wait(&semaphore_write);
        int i;
        for (i=N/2; i<N; i++) {
           buffer[i] = rand();
           printf("P2: the value insert in buffer[%d] is %d\n", i, buffer[i]);
        }
        sem_post(&semaphore_inv);
        sleep(1);
       goto loop_b; 
    }
    else{
       sem_wait(&semaphore_read);
       int k;
       for (k=0; k<N; k++) {
          buff[k] = buffer[k];
          printf(" slot %d of the buffer is %d\n", buffer[k]);
       }
        sem_post(&semaphore_write);
        sembuf.sem_num=0;
        sembuf.sem_op= +1;
        sembuf.sem_flg=0;
        semop(sem_write, &sembuf, 1);
    }
}  
 
    