#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
sem_t sem_filled;
sem_t sem_empty;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int head = 0;
int tail = 0;
char loop_buf[10][100];
int pipefd[2];
void *printf_thread()
{
//char buf[100];
while(1){
//read(pipefd[0],buf,sizeof(buf));
//printf("~~~ read is %s\n",buf);
int sval;
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
sem_wait(&sem_filled);
pthread_mutex_lock(&mutex);
printf(" 11 ~~~~~~~read is %s\n",loop_buf[tail]);
tail = tail+1 > 9 ? 0 : tail+1;
pthread_mutex_unlock(&mutex);
sem_post(&sem_empty);
//sleep(2);
}
}
void *printf_thread2()
{
//char buf[100];
while(1){
//read(pipefd[0],buf,sizeof(buf));
//printf("~~~ read is %s\n",buf);
int sval;
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
sem_wait(&sem_filled);
pthread_mutex_lock(&mutex);
printf(" 22 ~~~~~~~read is %s\n",loop_buf[tail]);
tail = tail+1 > 9 ? 0 : tail+1;
pthread_mutex_unlock(&mutex);
sem_post(&sem_empty);
//sleep(2);
}
}
void *printf_thread3()
{
//char buf[100];
while(1){
//read(pipefd[0],buf,sizeof(buf));
//printf("~~~ read is %s\n",buf);
int sval;
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
sem_wait(&sem_filled);
pthread_mutex_lock(&mutex);
printf(" 33 ~~~~~~~read is %s\n",loop_buf[tail]);
tail = tail+1 > 9 ? 0 : tail+1;
pthread_mutex_unlock(&mutex);
sem_post(&sem_empty);
//sleep(2);
}
}
int
main(int argc, char *argv[])
{
//pid_t cpid;
int i = 0;
//if (pipe(pipefd) == -1) {
// perror("pipe");
// exit(EXIT_FAILURE);
//}
int sval;
sem_init(&sem_empty,0,9);
sem_init(&sem_filled,0,0);
//sem_getvalue(&g_sem, &sval);
//printf("sem value is %d\n",sval);
pthread_t threadid;
pthread_create(&threadid,NULL,printf_thread,NULL);
pthread_create(&threadid,NULL,printf_thread2,NULL);
pthread_create(&threadid,NULL,printf_thread3,NULL);
char buf[100];
while(i<200){
//gets(buf);
//write(pipefd[1],buf,sizeof(buf));
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
i++;
sprintf(buf,"%d",i);
if(!sem_trywait(&sem_empty)){
printf("@insert is %s\n",buf);
strcpy(loop_buf[head],buf);
head = head+1 > 9 ? 0 : head+1;
sem_post(&sem_filled);
}else{
printf("!!!!lose %s\n",buf);
}
//usleep(1);
}
while(1)sleep(2);
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
sem_t sem_filled;
sem_t sem_empty;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int head = 0;
int tail = 0;
char loop_buf[10][100];
int pipefd[2];
void *printf_thread()
{
//char buf[100];
while(1){
//read(pipefd[0],buf,sizeof(buf));
//printf("~~~ read is %s\n",buf);
int sval;
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
sem_wait(&sem_filled);
pthread_mutex_lock(&mutex);
printf(" 11 ~~~~~~~read is %s\n",loop_buf[tail]);
tail = tail+1 > 9 ? 0 : tail+1;
pthread_mutex_unlock(&mutex);
sem_post(&sem_empty);
//sleep(2);
}
}
void *printf_thread2()
{
//char buf[100];
while(1){
//read(pipefd[0],buf,sizeof(buf));
//printf("~~~ read is %s\n",buf);
int sval;
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
sem_wait(&sem_filled);
pthread_mutex_lock(&mutex);
printf(" 22 ~~~~~~~read is %s\n",loop_buf[tail]);
tail = tail+1 > 9 ? 0 : tail+1;
pthread_mutex_unlock(&mutex);
sem_post(&sem_empty);
//sleep(2);
}
}
void *printf_thread3()
{
//char buf[100];
while(1){
//read(pipefd[0],buf,sizeof(buf));
//printf("~~~ read is %s\n",buf);
int sval;
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
sem_wait(&sem_filled);
pthread_mutex_lock(&mutex);
printf(" 33 ~~~~~~~read is %s\n",loop_buf[tail]);
tail = tail+1 > 9 ? 0 : tail+1;
pthread_mutex_unlock(&mutex);
sem_post(&sem_empty);
//sleep(2);
}
}
int
main(int argc, char *argv[])
{
//pid_t cpid;
int i = 0;
//if (pipe(pipefd) == -1) {
// perror("pipe");
// exit(EXIT_FAILURE);
//}
int sval;
sem_init(&sem_empty,0,9);
sem_init(&sem_filled,0,0);
//sem_getvalue(&g_sem, &sval);
//printf("sem value is %d\n",sval);
pthread_t threadid;
pthread_create(&threadid,NULL,printf_thread,NULL);
pthread_create(&threadid,NULL,printf_thread2,NULL);
pthread_create(&threadid,NULL,printf_thread3,NULL);
char buf[100];
while(i<200){
//gets(buf);
//write(pipefd[1],buf,sizeof(buf));
sem_getvalue(&sem_empty, &sval);
printf("sem_empty value is %d ",sval);
i++;
sprintf(buf,"%d",i);
if(!sem_trywait(&sem_empty)){
printf("@insert is %s\n",buf);
strcpy(loop_buf[head],buf);
head = head+1 > 9 ? 0 : head+1;
sem_post(&sem_filled);
}else{
printf("!!!!lose %s\n",buf);
}
//usleep(1);
}
while(1)sleep(2);
}