作业
1.在第一题的基础上加上一个需求:要求打印,倒置线程,顺序执行。出现的现象为先打印1234567,后打印7654321,不使用flag
2.创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取到的内容打印到终端上,类似实现cat一个文件。cat数据完毕后,要结束两个线程。
1.代码
#include <stdio.h>
#include <head.h>
char buf[] = "1234567";
int flag = 0;
pthread_mutex_t mutex;
sem_t sem1, sem2;
void *callBack1(void *arg)
{
while(1)
{
if (sem_wait(&sem1) < 0)
{
ERR_MSG("sem_wait");
return NULL;
}
printf("%s\n", buf);
if (sem_post(&sem2) < 0)
{
ERR_MSG("sem_post");
return NULL;
}
}
pthread_exit(NULL);
return NULL;
}
void *callBack2(void *arg)
{
int i = 0;
int j = strlen(buf) - 1;
while (1)
{
if (sem_wait(&sem2) < 0)
{
ERR_MSG("sem_wait");
return NULL;
}
while (i < j)
{
char t = buf[i];
buf[i] = buf[j];
buf[j] = t;
i++;
j--;
}
if (sem_post(&sem1) < 0)
{
ERR_MSG("sem_post");
return NULL;
}
i = 0;
j = strlen(buf) - 1;
}
pthread_exit(NULL);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
if(sem_init(&sem1, 0 , 1) < 0)
{
ERR_MSG("sem_init");
return -1;
}
if(sem_init(&sem2, 0 , 0) < 0)
{
ERR_MSG("sem_init");
return -1;
}
printf("sem_init success __%d__\n", __LINE__);
if (pthread_create(&tid1, NULL, callBack1, NULL) != 0)
{
fprintf(stderr, "pthread_create fail __%d__\n", __LINE__);
return -1;
}
if (pthread_create(&tid2, NULL, callBack2, NULL) != 0)
{
fprintf(stderr, "pthread_create fail __%d__\n", __LINE__);
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
结果
2.代码
#include <stdio.h>
#include <head.h>
struct Msg
{
int fd_r;
off_t size;
};
char c = 0;
pthread_mutex_t mutex;
sem_t sem1, sem2;
void *callBack1(void *arg)
{
int fd_r = ((struct Msg*)arg) -> fd_r;
off_t size = ((struct Msg*)arg) -> size;
lseek(fd_r, 0, SEEK_SET);
for (int i = 0; i < size; i++)
{
if (sem_wait(&sem1) < 0)
{
ERR_MSG("sem_wait");
return NULL;
}
read(fd_r, &c, 1);
if (sem_post(&sem2) < 0)
{
ERR_MSG("sem_post");
return NULL;
}
}
printf("\n线程1准备退出\n");
pthread_exit(NULL);
return NULL;
}
void *callBack2(void *arg)
{
off_t size = ((struct Msg*)arg) -> size;
for (int i = 0; i < size; i++)
{
if (sem_wait(&sem2) < 0)
{
ERR_MSG("sem_wait");
return NULL;
}
printf("%c", c);
if (sem_post(&sem1) < 0)
{
ERR_MSG("sem_post");
return NULL;
}
}
printf("\n线程2准备退出\n");
pthread_exit(NULL);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex, NULL);
if(sem_init(&sem1, 0 , 1) < 0)
{
ERR_MSG("sem_init");
return -1;
}
if(sem_init(&sem2, 0 , 0) < 0)
{
ERR_MSG("sem_init");
return -1;
}
printf("<---若出现线程退出提示,可以按回车结束该程序--->\n");
int fd_r = open("./1.c", O_RDONLY);
if (fd_r < 0)
{
ERR_MSG("open");
return -1;
}
off_t size = lseek(fd_r, 0, SEEK_END);
struct Msg filemsg = {fd_r, size};
pthread_t tid1, tid2;
if (pthread_create(&tid1, NULL, callBack1, &filemsg) != 0)
{
fprintf(stderr, "pthread_create fail __%d__\n", __LINE__);
return -1;
}
if (pthread_create(&tid2, NULL, callBack2, &filemsg) != 0)
{
fprintf(stderr, "pthread_create fail __%d__\n", __LINE__);
return -1;
}
char s;
s = getchar();
sem_destroy(&sem1);
sem_destroy(&sem2);
pthread_mutex_destroy(&mutex);
close(fd_r);
printf("文件打印完毕\n");
return 0;
}
结果
思维导图