0

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);
    }

}  
SBJ
  • 1
  • 4
  • Sorry, if the description it's not clear – SBJ Aug 30 '18 at 10:59
  • Can I only use System V semaphores, to use in this case semaphores correctly? – SBJ Aug 30 '18 at 11:03
  • See [this](https://stackoverflow.com/questions/16400820/how-to-use-posix-semaphores-on-forked-processes-in-c). If you use SystemV semaphores or any semaphores or any memory that you want to share between processes you must place them into a shared memory. That's why threads are simpler - no need to do that. – KamilCuk Aug 30 '18 at 11:06
  • Ah, ok. Thank you. – SBJ Aug 30 '18 at 11:11
  • `p1 = fork(); p2 = fork();` After this, you have **four** processes. – joop Aug 30 '18 at 11:15
  • 1
    Sorry, why are 4 and not 3? P0 with the first fork() create the first process, with the second fork() one another process or not? – SBJ Aug 30 '18 at 11:25
  • @joop - are you sure? If those two calls were made from the same original process, then you should now have 3 processes. – ryyker Aug 30 '18 at 11:39
  • Because, after `p1 = fork();` , there are **two** processes (remember: fork() returns twice!) Both these processes will execute `p2 = fork();`, so you end up with `2*2` := four processes. – joop Aug 30 '18 at 12:00
  • Now it works! Thanks to all of you! – SBJ Aug 30 '18 at 13:01

1 Answers1

0

There are four processes involved. Illustration:


#include <stdio.h>
#include <unistd.h>

int main(void)
{
int pid = -2, pid1 = -2, pid2 = -2;


pid1 = fork();
pid2 = fork();
mypid = getpid();

printf("Pid= {%d, %d %d}\n", mypid, pid1,pid2);

return 0;
}
joop
  • 4,330
  • 1
  • 15
  • 26