生产一段C语言代码
时间: 2023-07-09 11:05:12 浏览: 45
以下是一个简单的C语言代码,实现了将两个整数相加并输出结果:
```c
#include <stdio.h>
int main() {
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
```
运行结果为:
```
The sum of 10 and 20 is 30
```
相关问题
请写一段c语言代码实现生产者消费者问题
下面是一段简单的 C 代码实现生产者消费者问题:
```c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_SIZE 10 // 缓冲区大小
int buffer[BUFFER_SIZE]; // 缓冲区数组
pthread_mutex_t mutex; // 互斥锁
pthread_cond_t cond_full; // 缓冲区已满条件变量
pthread_cond_t cond_empty; // 缓冲区已空条件变量
int in = 0; // 生产者进入缓冲区的位置
int out = 0; // 消费者从缓冲区取出的位置
void *producer(void *arg)
{
int item, i;
for (i = 0; i < BUFFER_SIZE; i++) {
// 生成数据项
item = rand();
pthread_mutex_lock(&mutex);
while ((in + 1) % BUFFER_SIZE == out) { // 缓冲区已满
pthread_cond_wait(&cond_full, &mutex);
}
buffer[in] = item; // 将数据项放入缓冲区
in = (in + 1) % BUFFER_SIZE; // 生产者进入缓冲区的位置+1
pthread_cond_signal(&cond_empty); // 唤醒一个等待的消费者线程
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *consumer(void *arg)
{
int item, i;
for (i = 0; i < BUFFER_SIZE; i++) {
pthread_mutex_lock(&mutex);
while (in == out) { // 缓冲区已空
pthread_cond_wait(&cond_empty, &mutex);
}
item = buffer[out]; // 取出数据项
out = (out + 1) % BUFFER_SIZE; // 消费者从缓冲区取出的位置+1
printf("Consumer get item %d\n", item); // 消费数据项
pthread_cond_signal(&cond_full); // 唤醒一个等待的生产者线程
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main()
{
pthread_t tid1, tid2;
int ret;
// 初始化互斥锁和条件变量
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond_full, NULL);
pthread_cond_init(&cond_empty, NULL);
// 创建生产者线程和消费者线程
ret = pthread_create(&tid1, NULL, producer, NULL);
if (ret != 0) {
printf("Create producer thread error!\n");
return -1;
}
ret = pthread_create(&tid2, NULL, consumer, NULL);
if (ret != 0) {
printf("Create consumer thread error!\n");
return -1;
}
// 等待生产者线程和消费者线程结束
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
// 销毁互斥锁和条件变量
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond_full);
pthread_cond_destroy(&cond_empty);
return 0;
}
```
生产者消费者问题c语言代码
以下是一个简单的生产者消费者问题的C语言代码:
```c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 10
int buffer[BUFFER_SIZE]; // 缓冲区
int count = 0; // 缓冲区中的元素数量
int in = 0; // 缓冲区中下一个空槽位的索引
int out = 0; // 缓冲区中下一个可用元素的索引
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 互斥锁
pthread_cond_t full = PTHREAD_COND_INITIALIZER; // 缓冲区满条件变量
pthread_cond_t empty = PTHREAD_COND_INITIALIZER; // 缓冲区空条件变量
// 生产者线程函数
void* producer(void* arg) {
while (1) {
pthread_mutex_lock(&mutex); // 加锁
// 缓冲区已满,等待消费者消费后再生产
while (count == BUFFER_SIZE) {
pthread_cond_wait(&empty, &mutex);
}
// 生产一个元素,并将其放入缓冲区
buffer[in] = rand();
printf("producer, in=%d, value=%d\n", in, buffer[in]);
in = (in + 1) % BUFFER_SIZE;
count++;
pthread_cond_signal(&full); // 唤醒消费者
pthread_mutex_unlock(&mutex); // 解锁
}
return NULL;
}
// 消费者线程函数
void* consumer(void* arg) {
while (1) {
pthread_mutex_lock(&mutex); // 加锁
// 缓冲区为空,等待生产者生产后再消费
while (count == 0) {
pthread_cond_wait(&full, &mutex);
}
// 从缓冲区中取出一个元素并消费
int value = buffer[out];
printf("consumer, out=%d, value=%d\n", out, value);
out = (out + 1) % BUFFER_SIZE;
count--;
pthread_cond_signal(&empty); // 唤醒生产者
pthread_mutex_unlock(&mutex); // 解锁
}
return NULL;
}
int main() {
pthread_t producer_thread, consumer_thread;
// 创建生产者线程和消费者线程
if (pthread_create(&producer_thread, NULL, producer, NULL)) {
perror("pthread_create");
exit(1);
}
if (pthread_create(&consumer_thread, NULL, consumer, NULL)) {
perror("pthread_create");
exit(1);
}
// 等待两个线程结束
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
return 0;
}
```
这段代码实现了一个简单的生产者消费者问题,使用了互斥锁和条件变量来保证缓冲区的正确性。其中,生产者线程生产随机数并将其放入缓冲区,消费者线程从缓冲区中取出随机数并消费。如果缓冲区已满,生产者线程会等待信号,等待消费者线程消费后再进行生产;如果缓冲区为空,消费者线程会等待信号,等待生产者线程生产后再进行消费。