为什么在C-Posix代码中有此信号量问题?

时间:2018-08-30 10:55:58

标签: c posix

程序必须这样做:

进程P0创建P1和P2;

sizeof(buffer)= N(通过命令行插入);

P1在缓冲区的前N / 2个元素中插入随机值(用户N次插入)

P2在缓冲区的第二部分插入随机值

之后:P1反转缓冲区的第二部分,然后进程P0打印缓冲区的所有元素

如果用户按下CTRL + C --->打印缓冲区元素并杀死所有进程;

我不知道为什么,但是进程P1保持暂停状态。我在并发进程P2中调用了信号量值(“ semaphore_inv”)的增加,并且等待必须将其减小为0。因此,该程序无法正常工作。

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

}  

1 个答案:

答案 0 :(得分:0)

涉及四个过程。插图:


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