1>
要求:
源代码:
#include <myhead.h>
int num = 520;
pthread_mutex_t mutex;
void *task1(void *arg)
{
printf("11111111111111\n");
pthread_mutex_lock(&mutex);
num = 1314;
sleep(3);
printf("task1:num = %d\n", num);
//4、释放锁资源
pthread_mutex_unlock(&mutex);
}
void *task2(void *arg)
{
printf("22222222222\n");
pthread_mutex_lock(&mutex);
num++;
sleep(3);
printf("task2:num = %d\n", num);
//4、释放锁资源
pthread_mutex_unlock(&mutex);
}
int main(int argc, char const *argv[])
{
pthread_mutex_init(&mutex, NULL);
pthread_t tid1, tid2;
if (pthread_create(&tid1, NULL, task1, NULL) != 0)
{
printf("tid1 create error\n");
return 0;
}
if (pthread_create(&tid2, NULL, task2, NULL) != 0)
{
printf("tid2 create error\n");
return 0;
}
printf("tid1:%#lx, tid2:%#lx\n", tid1, tid2);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
效果图:
2>
要求:
源代码:
(1)
#include <myhead.h>
sem_t sem;
void *task1(void *arg)
{
int num = 5;
while (num--)
{
sleep(1);
printf("我生产了一辆特斯拉\n");
sem_post(&sem);
}
pthread_exit(NULL);
}
void *task2(void *arg)
{
int num = 5;
while (num--)
{
sem_wait(&sem);
printf("我消费了一辆特斯拉\n");
}
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
sem_init(&sem, 0, 0);
pthread_t tid1, tid2;
if (pthread_create(&tid1, NULL, &task1, NULL) != 0)
{
printf("tid1 create error\n");
}
if (pthread_create(&tid2, NULL, &task2, NULL) != 0)
{
printf("tid2 create error\n");
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
sem_destroy(&sem);
return 0;
}
(2)
#include <myhead.h>
sem_t sem1, sem2, sem3;
void *task1(void *arg)
{
while (1)
{
sem_wait(&sem1);
sleep(1);
printf("A");
fflush(stdout);
sem_post(&sem2);
}
pthread_exit(NULL);
}
void *task2(void *arg)
{
while (1)
{
sem_wait(&sem2);
printf("B");
fflush(stdout);
sem_post(&sem3);
}
pthread_exit(NULL);
}
void *task3(void *arg)
{
while (1)
{
sem_wait(&sem3);
printf("C");
fflush(stdout);
sem_post(&sem1);
}
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
sem_init(&sem1, 0, 1);
sem_init(&sem2, 0, 0);
sem_init(&sem3, 0, 0);
pthread_t tid1, tid2, tid3;
if (pthread_create(&tid1, NULL, &task1, NULL) != 0)
{
printf("tid1 create error\n");
}
if (pthread_create(&tid2, NULL, &task2, NULL) != 0)
{
printf("tid2 create error\n");
}
if (pthread_create(&tid3, NULL, &task3, NULL) != 0)
{
printf("tid3 create error\n");
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}
效果图:
(1)
(2)
源代码:
3>
要求:
源代码:
#include <myhead.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *task1(void *arg)
{
int num = 5;
while (num--)
{
sleep(1);
printf("%#lx:生产了一辆特斯拉\n", pthread_self());
//pthread_cond_signal(&cond);
}
pthread_cond_broadcast(&cond);
pthread_exit(NULL);
}
void *task2(void *arg)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("%#lx:消耗了一辆特斯拉\n", pthread_self());
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
pthread_t tid1, tid2, tid3, tid4, tid5, tid6;
if (pthread_create(&tid1, NULL, task1, NULL) != 0)
{
printf("tid1 error\n");
return 0;
}
if (pthread_create(&tid2, NULL, task2, NULL) != 0)
{
printf("tid2 error\n");
return 0;
}
if (pthread_create(&tid3, NULL, task2, NULL) != 0)
{
printf("tid3 error\n");
return 0;
}
if (pthread_create(&tid4, NULL, task2, NULL) != 0)
{
printf("tid4 error\n");
return 0;
}
if (pthread_create(&tid5, NULL, task2, NULL) != 0)
{
printf("tid5 error\n");
return 0;
}
if (pthread_create(&tid6, NULL, task2, NULL) != 0)
{
printf("tid6 error\n");
return 0;
}
printf("tid1:%#lx, tid2:%#lx, tid3:%#lx, tid4:%#lx, tid5:%#lx, tid6:%#lx\n",
tid1, tid2, tid3, tid4, tid5, tid6);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_join(tid4, NULL);
pthread_join(tid5, NULL);
pthread_join(tid6, NULL);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
return 0;
}
效果图:
4>
要求:
源代码:
#include <myhead.h>
#define MAXSIZE 128
int main(int argc, char const *argv[])
{
int pipefd[2] = {0};
if (pipe(pipefd) == -1)
{
perror("pipe error");
return -1;
}
printf("pipefd[0]=%d, pipefd[1]=%d\n", pipefd[0], pipefd[1]);
sleep(2);
pid_t pid = fork();
if (pid > 0)
{
close(pipefd[0]);
char wbuf[MAXSIZE] = "";
while (1)
{
bzero(wbuf, sizeof(wbuf));
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf) - 1] = 0;
write(pipefd[1], wbuf, strlen(wbuf));
if (strcmp(wbuf, "quit") == 0)
{
break;
}
}
close(pipefd[1]);
wait(NULL);
}
else if (pid == 0)
{
close(pipefd[1]);
char rbuf[MAXSIZE] = "";
while (1)
{
bzero(rbuf, sizeof(rbuf));
read(pipefd[0], rbuf, sizeof(rbuf));
printf("父进程传出的信息为:%s\n", rbuf);
if (strcmp(rbuf, "quit") == 0)
{
break;
}
}
close(pipefd[0]);
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
return 0;
}
效果图:
5>
要求:
源代码:
//create.c
#include<myhead.h>
int main(int argc, char const *argv[])
{
if(mkfifo("./mkfifo",0664)==-1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm mkfifo");
return 0;
}
//snd.c
#include <myhead.h>
int main(int argc, char const *argv[])
{
int wfd = -1;
if ((wfd = open("./mkfifo", O_WRONLY)) == -1)
{
perror("open error");
return -1;
}
char wbuf[128] = "";
while (1)
{
sleep(1);
printf("请输入:\n");
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf)-1]=0;
write(wfd, wbuf, strlen(wbuf));
if (strcmp(wbuf, "quit") == 0)
{
break;
}
}
close(wfd);
return 0;
}
///rec.c
#include <myhead.h>
int main(int argc, char const *argv[])
{
int rfd = -1;
if ((rfd = open("./mkfifo", O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
char rbuf[128] = "";
while (1)
{
bzero(rbuf, sizeof(rbuf));
read(rfd, rbuf, sizeof(rbuf));
if (strcmp(rbuf, "quit") == 0)
{
break;
}
printf("接收到的消息为:%s\n", rbuf);
}
close(rfd);
return 0;
}
效果图:
6>
要求:
源代码:
//create.c
#include <myhead.h>
int main(int argc, char const *argv[])
{
if (mkfifo("./mkfifo", 0664) == -1)
{
perror("mkfifo error");
return -1;
}
if (mkfifo("./mkfifo_sec", 0664) == -1)
{
perror("mkfifo_sec error");
return -1;
}
getchar();
system("rm mkfifo");
system("rm mkfifo_sec");
return 0;
}
//snd.c
#include <myhead.h>
int main(int argc, char const *argv[])
{
pid_t pid = fork();
if (pid > 0)
{
int wfd = -1;
if ((wfd = open("./mkfifo", O_WRONLY)) == -1)
{
perror("open error");
return -1;
}
char wbuf[128] = "";
while (1)
{
sleep(1);
printf("请输入:\n");
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf) - 1] = 0;
write(wfd, wbuf, strlen(wbuf));
if (strcmp(wbuf, "quit") == 0)
{
break;
}
}
close(wfd);
wait(NULL);
}
else if (pid == 0)
{
int rfd = -1;
if ((rfd = open("./mkfifo_sec", O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
char rbuf[128] = "";
while (1)
{
bzero(rbuf, sizeof(rbuf));
read(rfd, rbuf, sizeof(rbuf));
if (strcmp(rbuf, "quit") == 0)
{
break;
}
printf("接收到的消息为:%s\n", rbuf);
}
close(rfd);
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
return 0;
}
//rec.c
#include <myhead.h>
int main(int argc, char const *argv[])
{
pid_t pid = fork();
if (pid > 0)
{
int rfd = -1;
if ((rfd = open("./mkfifo", O_RDONLY)) == -1)
{
perror("open error");
return -1;
}
char rbuf[128] = "";
while (1)
{
bzero(rbuf, sizeof(rbuf));
read(rfd, rbuf, sizeof(rbuf));
if (strcmp(rbuf, "quit") == 0)
{
break;
}
printf("接收到的消息为:%s\n", rbuf);
}
close(rfd);
wait(NULL);
}
else if (pid == 0)
{
int wfd = -1;
if ((wfd = open("./mkfifo_sec", O_WRONLY)) == -1)
{
perror("open error");
return -1;
}
char wbuf[128] = "";
while (1)
{
sleep(1);
printf("请输入:\n");
fgets(wbuf, sizeof(wbuf), stdin);
wbuf[strlen(wbuf) - 1] = 0;
write(wfd, wbuf, strlen(wbuf));
if (strcmp(wbuf, "quit") == 0)
{
break;
}
}
close(wfd);
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
return 0;
}