Linux c语言实现一个生产者和消费者

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

#define BUFFER_SIZE 10

int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t notfull = PTHREAD_COND_INITIALIZER;
pthread_cond_t notempty = PTHREAD_COND_INITIALIZER;

void insert(int item) {
    pthread_mutex_lock(&mutex);
    while (count == BUFFER_SIZE) {
        pthread_cond_wait(&notfull, &mutex);
    }
    buffer[in] = item;
    in = (in + 1) % BUFFER_SIZE;
    ++count;
    pthread_cond_signal(&notempty);
    pthread_mutex_unlock(&mutex);
}

int remove() {
    int item;
    pthread_mutex_lock(&mutex);
    while (count == 0) {
        pthread_cond_wait(&notempty, &mutex);
    }
    item = buffer[out];
    out = (out + 1) % BUFFER_SIZE;
    --count;
    pthread_cond_signal(&notfull);
    pthread_mutex_unlock(&mutex);
    return item;
}

void* producer(void* arg) {
    int i;
    for (i = 0; i < 10; ++i) {
        insert(i);
        printf("Produced: %d\n", i);
        sleep(1);
    }
    return NULL;
}

void* consumer(void* arg) {
    int val;
    while (1) {
        val = remove();
        printf("Consumed: %d\n", val);
        sleep(1);
    }
    return NULL;
}

int main() {
    pthread_t ptid, ctid;
    pthread_create(&ptid, NULL, producer, NULL);
    pthread_create(&ctid, NULL, consumer, NULL);
    pthread_join(ptid, NULL);
    pthread_join(ctid, NULL);
    return 0;
}

这段代码实现了一个简单的生产者-消费者模型,其中包含了一个有限缓冲区。它使用了条件变量来同步生产者和消费者,确保在缓冲区为空或为满时不会消耗或生产数据,从而避免了竞态条件。这个例子教会了如何在Linux环境下使用C语言编写并发程序。

FR:徐海涛(hunk Xu)

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的基于 Linux 队列的生产者消费者 C 语言实例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/stat.h> #include <mqueue.h> #include <errno.h> #include <unistd.h> #define QUEUE_NAME "/my_queue" #define MAX_MESSAGES 10 #define MAX_MSG_SIZE 256 #define MSG_BUFFER_SIZE MAX_MSG_SIZE + 10 int main(int argc, char **argv) { mqd_t mq; struct mq_attr attr; char buffer[MSG_BUFFER_SIZE]; int terminate = 0; // 设置消息队列属性 attr.mq_flags = 0; attr.mq_maxmsg = MAX_MESSAGES; attr.mq_msgsize = MAX_MSG_SIZE; attr.mq_curmsgs = 0; // 创建消息队列 mq = mq_open(QUEUE_NAME, O_CREAT | O_RDONLY, 0644, &attr); if (mq == -1) { perror("mq_open"); exit(1); } // 消费者循环等待消息 while (!terminate) { ssize_t bytes_read; bytes_read = mq_receive(mq, buffer, MSG_BUFFER_SIZE, NULL); if (bytes_read == -1) { perror("mq_receive"); exit(1); } buffer[bytes_read] = '\0'; if (strncmp(buffer, "exit", strlen("exit")) == 0) { terminate = 1; } else { printf("Received message: %s\n", buffer); } } // 关闭消息队列 mq_close(mq); mq_unlink(QUEUE_NAME); return 0; } ``` 生产者代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/stat.h> #include <mqueue.h> #include <errno.h> #include <unistd.h> #define QUEUE_NAME "/my_queue" #define MAX_MESSAGES 10 #define MAX_MSG_SIZE 256 #define MSG_BUFFER_SIZE MAX_MSG_SIZE + 10 int main(int argc, char **argv) { mqd_t mq; char buffer[MSG_BUFFER_SIZE]; int terminate = 0; // 打开消息队列 mq = mq_open(QUEUE_NAME, O_WRONLY); if (mq == -1) { perror("mq_open"); exit(1); } // 生产者循环发送消息 while (!terminate) { printf("Enter message to send (exit to terminate): "); fgets(buffer, MSG_BUFFER_SIZE, stdin); if (strncmp(buffer, "exit", strlen("exit")) == 0) { terminate = 1; } else { ssize_t bytes_sent; bytes_sent = mq_send(mq, buffer, strlen(buffer) + 1, 0); if (bytes_sent == -1) { perror("mq_send"); exit(1); } } } // 发送终止消息 if (mq_send(mq, "exit", strlen("exit") + 1, 0) == -1) { perror("mq_send"); exit(1); } // 关闭消息队列 mq_close(mq); return 0; } ``` 这个例子中,生产者循环等待用户输入消息并将其发送到消息队列中。消费者循环等待从消息队列中接收消息并将其打印到控制台上。当用户输入 "exit" 时,生产者发送一个终止消息,消费者收到终止消息后退出循环并关闭消息队列。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值