c - 为什么我在 C-Posix 代码中遇到这个信号量问题?

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

}  

最佳答案

涉及四个流程。插图:

<小时/>
#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;
}

关于c - 为什么我在 C-Posix 代码中遇到这个信号量问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52095312/

相关文章:

bash - POSIX 替代多变量赋值与读取

C 在特定 IP 上打开套接字

c++ - 在 Windows 中从 CMD 执行 c-programfiles

c - 如何将一个字符数组中的每第 n 个字符复制到另一个字符数组?

c - 如何从 bash 向 C 程序提供输入?

将十六进制(1字节)的字符转换为2个字符的字符串

c - 该函数是否被视为内联?

c - 从标准输入读取()

c - 从 pthread_create 将多个参数传递给线程函数

multithreading - POSIX 线程与 Win32 线程