第一题
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<semaphore.h>
char buf[] = "1234567";
sem_t sem1, sem2;
int flag = 0;//0打印,1逆致
void* callback1(void* arg)
{
while (1)
{
if (0 == flag)
{
sem_wait(&sem2);
sem_wait(&sem1);
printf("%s\n", buf);
flag = 1;
sem_post(&sem1);
}
sem_post(&sem2);
}
}
void* callback2(void* arg)
{
while (1)
{
if (1 == flag)
{
sem_wait(&sem2);
sem_wait(&sem1);
int i = 0;
int j = strlen(buf)-1;
while (i < j)
{
char temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
i++;
j--;
}
flag = 0;
sem_post(&sem1);
}
sem_post(&sem2);
}
pthread_exit(0);
}
int main(int argc, const char *argv[])
{
sem_init(&sem1, 0, 1);
sem_init(&sem2, 0, 1);
pthread_t tid1, tid2;
if (pthread_create(&tid1, NULL, callback1, NULL) != 0)
{
printf("pthread_create failed\n");
printf("line: %d\n", __LINE__);
return -1;
}
if (pthread_create(&tid2, NULL, callback2, NULL) != 0)
{
printf("pthread_create failed\n");
printf("line: %d\n", __LINE__);
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
第二题
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
char buf;
int fp_r;
off_t size;
pthread_mutex_t mutex_r;
pthread_cond_t cond;
int flag = 0; //0读取 1打印
void* callback_r(void* arg)
{
printf("1\n");
lseek(fp_r, 0, SEEK_SET);
for (int i=0; i<size; i++)
{
pthread_mutex_lock(&mutex_r);
if (0 != flag)
{
pthread_cond_wait(&cond, &mutex_r);
}
if (read(fp_r, &buf, 1) < 0)
{
perror("read");
return NULL;
}
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex_r);
}
printf("jieshu");
pthread_exit(NULL);
}
void* callback_w(void* arg)
{
printf("2\n");
for (int i=0; i<size; i++)
{
pthread_mutex_lock(&mutex_r);
if (1 != flag)
{
pthread_cond_wait(&cond, &mutex_r);
}
if (write(1, &buf, 1) < 0)
{
perror("write");
return NULL;
}
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex_r);
}
printf("jieshu");
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
fp_r = open("./create.c", O_RDWR);
if (fp_r < 0)
{
perror("open");
fprintf(stderr, "line: %d\n", __LINE__);
return -1;
}
size = lseek(fp_r, 0, SEEK_END);
printf("%ld\n", size);
pthread_t tid_r, tid_w;
pthread_mutex_init(&mutex_r, NULL);
pthread_cond_init(&cond, NULL);
if (pthread_create(&tid_r, NULL, callback_r, NULL) < 0)
{
fprintf(stderr, "pthread_create failed");
return -1;
}
pthread_detach(tid_r);
if (pthread_create(&tid_w, NULL, callback_w, NULL) < 0)
{
fprintf(stderr, "pthread_create failed");
return -1;
}
pthread_join(tid_r, NULL);
pthread_join(tid_w, NULL);
pthread_mutex_destroy(&mutex_r);
pthread_cond_destroy(&cond);
close(fp_r);
return 0;
}
第三题
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
pthread_t tid_a, tid_b, tid_c;
pthread_mutex_t mutex;
pthread_cond_t cond;
int flag = 0; //0 a 1 b 2 c
void* callback_a(void* arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (0 != flag)
{
pthread_cond_wait(&cond, &mutex);
}
printf("%ld\n", tid_a);
flag = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callback_b(void* arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (1 != flag)
{
pthread_cond_wait(&cond, &mutex);
}
printf("%ld\n", tid_b);
flag = 2;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* callback_c(void* arg)
{
while (1)
{
pthread_mutex_lock(&mutex);
if (2 != flag)
{
pthread_cond_wait(&cond, &mutex);
}
printf("%ld\n", tid_c);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
if (pthread_create(&tid_a, NULL, callback_a, NULL) < 0)
{
fprintf(stderr, "pthread_create failed, line: %d\n",__LINE__);
return -1;
}
if (pthread_create(&tid_b, NULL, callback_b, NULL) < 0)
{
fprintf(stderr, "pthread_create failed, line: %d\n",__LINE__);
return -1;
}
if (pthread_create(&tid_c, NULL, callback_c, NULL) < 0)
{
fprintf(stderr, "pthread_create failed, line: %d\n",__LINE__);
return -1;
}
pthread_join(tid_a, NULL);
pthread_join(tid_b, NULL);
pthread_join(tid_c, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}