int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_wait(sem_t *sem);
int sem_post(sem_t *sem);
int sem_destroy(sem_t *sem);
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//线程函数
void *thread_func(void *msg);
sem_t sem;//信号量
#define MSG_SIZE 512
int main()
{
int res = -1;
pthread_t thread;
void *thread_result = NULL;
char msg[MSG_SIZE];
//初始化信号量,其初值为0
res = sem_init(&sem, 0, 0);
if(res == -1)
{
perror("semaphore intitialization failedn");
exit(EXIT_FAILURE);
}
//创建线程,并把msg作为线程函数的参数
res = pthread_create(&thread, NULL, thread_func, msg);
if(res != 0)
{
perror("pthread_create failedn");
exit(EXIT_FAILURE);
}
//输入信息,以输入end结束,由于fgets会把回车(n)也读入,所以判断时就变成了“endn”
printf("Input some text. Enter 'end'to finish...n");
while(strcmp("endn", msg) != 0)
{
fgets(msg, MSG_SIZE, stdin);
//把信号量加1
sem_post(&sem);
}
printf("Waiting for thread to finish...n");
//等待子线程结束
res = pthread_join(thread, &thread_result);
if(res != 0)
{
perror("pthread_join failedn");
exit(EXIT_FAILURE);
}
printf("Thread joinedn");
//清理信号量
sem_destroy(&sem);
exit(EXIT_SUCCESS);
}
void* thread_func(void *msg)
{
//把信号量减1
sem_wait(&sem);
char *ptr = msg;
while(strcmp("endn", msg) != 0)
{
int i = 0;
//把小写字母变成大写
for(; ptr[i] != ' '; ++i)
{
if(ptr[i] >= 'a' && ptr[i] <= 'z')
{
ptr[i] -= 'a' - 'A';
}
}
printf("You input %d charactersn", i-1);
printf("To Uppercase: %sn", ptr);
//把信号量减1
sem_wait(&sem);
}
//退出线程
pthread_exit(NULL);
}
printf("Input some text. Enter 'end'to finish...n");
while(strcmp("endn", msg) != 0)
{
if(strncmp("TEST", msg, 4) == 0)
{
strcpy(msg, "copy_datan");
sem_post(&sem);
}
fgets(msg, MSG_SIZE, stdin);
//把信号量加1
sem_post(&sem);
}
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//线程函数
void *thread_func(void *msg);
sem_t sem;//信号量
sem_t sem_add;//增加的信号量
#define MSG_SIZE 512
int main()
{
int res = -1;
pthread_t thread;
void *thread_result = NULL;
char msg[MSG_SIZE];
//初始化信号量,初始值为0
res = sem_init(&sem, 0, 0);
if(res == -1)
{
perror("semaphore intitialization failedn");
exit(EXIT_FAILURE);
}
//初始化信号量,初始值为1
res = sem_init(&sem_add, 0, 1);
if(res == -1)
{
perror("semaphore intitialization failedn");
exit(EXIT_FAILURE);
}
//创建线程,并把msg作为线程函数的参数
res = pthread_create(&thread, NULL, thread_func, msg);
if(res != 0)
{
perror("pthread_create failedn");
exit(EXIT_FAILURE);
}
//输入信息,以输入end结束,由于fgets会把回车(n)也读入,所以判断时就变成了“endn”
printf("Input some text. Enter 'end'to finish...n");
sem_wait(&sem_add);
while(strcmp("endn", msg) != 0)
{
if(strncmp("TEST", msg, 4) == 0)
{
strcpy(msg, "copy_datan");
sem_post(&sem);
//把sem_add的值减1,即等待子线程处理完成
sem_wait(&sem_add);
}
fgets(msg, MSG_SIZE, stdin);
//把信号量加1
sem_post(&sem);
//把sem_add的值减1,即等待子线程处理完成
sem_wait(&sem_add);
}
printf("Waiting for thread to finish...n");
//等待子线程结束
res = pthread_join(thread, &thread_result);
if(res != 0)
{
perror("pthread_join failedn");
exit(EXIT_FAILURE);
}
printf("Thread joinedn");
//清理信号量
sem_destroy(&sem);
sem_destroy(&sem_add);
exit(EXIT_SUCCESS);
}
void* thread_func(void *msg)
{
char *ptr = msg;
//把信号量减1
sem_wait(&sem);
while(strcmp("endn", msg) != 0)
{
int i = 0;
//把小写字母变成大写
for(; ptr[i] != ' '; ++i)
{
if(ptr[i] >= 'a' && ptr[i] <= 'z')
{
ptr[i] -= 'a' - 'A';
}
}
printf("You input %d charactersn", i-1);
printf("To Uppercase: %sn", ptr);
//把信号量加1,表明子线程处理完成
sem_post(&sem_add);
//把信号量减1
sem_wait(&sem);
}
sem_post(&sem_add);
//退出线程
pthread_exit(NULL);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有