多线程 printf() 与 c 中的信号量
Multithreading printf() with semaphores in c
我想要实现的是三个独立的打印一个接一个地执行,打印信息“What a wonderful world!”。相反,我得到的是“多么美妙的美妙世界世界”。我认为 pthread_join() 有问题。当用户输入ctrl+c时程序结束。
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
sem_t Asem;
sem_t Bsem;
sem_t Csem;
void *print_WhatA(){
while(1){
sem_wait(&Asem);
printf("What A ");
sem_post(&Bsem);
}
}
void *print_Wonderful(){
while(1){
sem_wait(&Bsem);
printf("Wonderful ");
sem_post(&Csem);
}
}
void *print_World(){
while(1){
sem_wait(&Csem);
printf("World!\n");
sem_post(&Asem);
}
}
void main(){
sem_init(&Asem,0,0); //initialization of the first semaphore
sem_init(&Bsem,0,1); //initialization of the second semaphore
sem_init(&Csem,0,2); //initialization of the third semaphore
pthread_t A_thread;
pthread_t B_thread;
pthread_t C_thread;
pthread_create(&A_thread,NULL, print_WhatA,NULL); //thread creation for print_WhatA function
pthread_create(&B_thread,NULL, print_Wonderfull,NULL); //thread creation for print_Wonderful function
pthread_create(&C_thread,NULL, print_World,NULL); //thread creation for print_World function
pthread_join(A_thread,NULL);
pthread_join(B_thread,NULL);
pthread_join(C_thread,NULL);
}
您应该将信号量 B 和 C 初始化为 0,将信号量 A 初始化为 1。
这有效:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
sem_t Asem;
sem_t Bsem;
sem_t Csem;
void *print_WhatA(){
while(1){
sem_wait(&Asem);
printf("What A ");
sem_post(&Bsem);
}
}
void *print_Wonderful(){
while(1){
sem_wait(&Bsem);
printf("Wonderful ");
sem_post(&Csem);
}
}
void *print_World(){
while(1){
sem_wait(&Csem);
printf("World!\n");
sem_post(&Asem);
}
}
void main(){
sem_init(&Asem,0,1); //initialization of the first semaphore
sem_init(&Bsem,0,0); //initialization of the second semaphore
sem_init(&Csem,0,0); //initialization of the third semaphore
pthread_t A_thread;
pthread_t B_thread;
pthread_t C_thread;
pthread_create(&A_thread,NULL, print_WhatA,NULL); //thread creation for print_WhatA function
pthread_create(&B_thread,NULL, print_Wonderful,NULL); //thread creation for print_Wonderful function
pthread_create(&C_thread,NULL, print_World,NULL); //thread creation for print_World function
pthread_join(A_thread,NULL);
pthread_join(B_thread,NULL);
pthread_join(C_thread,NULL);
}
我想要实现的是三个独立的打印一个接一个地执行,打印信息“What a wonderful world!”。相反,我得到的是“多么美妙的美妙世界世界”。我认为 pthread_join() 有问题。当用户输入ctrl+c时程序结束。
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
sem_t Asem;
sem_t Bsem;
sem_t Csem;
void *print_WhatA(){
while(1){
sem_wait(&Asem);
printf("What A ");
sem_post(&Bsem);
}
}
void *print_Wonderful(){
while(1){
sem_wait(&Bsem);
printf("Wonderful ");
sem_post(&Csem);
}
}
void *print_World(){
while(1){
sem_wait(&Csem);
printf("World!\n");
sem_post(&Asem);
}
}
void main(){
sem_init(&Asem,0,0); //initialization of the first semaphore
sem_init(&Bsem,0,1); //initialization of the second semaphore
sem_init(&Csem,0,2); //initialization of the third semaphore
pthread_t A_thread;
pthread_t B_thread;
pthread_t C_thread;
pthread_create(&A_thread,NULL, print_WhatA,NULL); //thread creation for print_WhatA function
pthread_create(&B_thread,NULL, print_Wonderfull,NULL); //thread creation for print_Wonderful function
pthread_create(&C_thread,NULL, print_World,NULL); //thread creation for print_World function
pthread_join(A_thread,NULL);
pthread_join(B_thread,NULL);
pthread_join(C_thread,NULL);
}
您应该将信号量 B 和 C 初始化为 0,将信号量 A 初始化为 1。 这有效:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
sem_t Asem;
sem_t Bsem;
sem_t Csem;
void *print_WhatA(){
while(1){
sem_wait(&Asem);
printf("What A ");
sem_post(&Bsem);
}
}
void *print_Wonderful(){
while(1){
sem_wait(&Bsem);
printf("Wonderful ");
sem_post(&Csem);
}
}
void *print_World(){
while(1){
sem_wait(&Csem);
printf("World!\n");
sem_post(&Asem);
}
}
void main(){
sem_init(&Asem,0,1); //initialization of the first semaphore
sem_init(&Bsem,0,0); //initialization of the second semaphore
sem_init(&Csem,0,0); //initialization of the third semaphore
pthread_t A_thread;
pthread_t B_thread;
pthread_t C_thread;
pthread_create(&A_thread,NULL, print_WhatA,NULL); //thread creation for print_WhatA function
pthread_create(&B_thread,NULL, print_Wonderful,NULL); //thread creation for print_Wonderful function
pthread_create(&C_thread,NULL, print_World,NULL); //thread creation for print_World function
pthread_join(A_thread,NULL);
pthread_join(B_thread,NULL);
pthread_join(C_thread,NULL);
}