作业要求:
程序代码:
#include<myhead.h>
int num = 520;
pthread_mutex_t mutex;
void* task1(void* arg)
{
pthread_mutex_lock(&mutex);
num = 1314;
sleep(3);
printf("task1:num = %d\n", num);
pthread_mutex_unlock(&mutex);
}
void* task2(void* arg)
{
pthread_mutex_lock(&mutex);
num++;
sleep(1);
printf("task2:num = %d\n", num);
pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex, NULL);
pthread_t thread1, thread2;
if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \
pthread_create(&thread2, NULL, task2, NULL) != 0)
{
perror("thread create error");
return -1;
}
printf("thread1:%#lx, thread2:%#lx\n", thread1, thread2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
运行结果:
程序代码:
#include<myhead.h>
//创建无名信号量
sem_t sem1,sem2,sem3;
//定义线程1
void *task1(void *arg)
{
int num=5;
while(num--)
{
sem_wait(&sem1);
sleep(1);
printf("A");
fflush(stdout);
sem_post(&sem2);
}
pthread_exit(NULL);
}
//定义线程2
void *task2(void *arg)
{
int num=5;
while(num--)
{
sem_wait(&sem2);
sleep(1);
printf("B");
fflush(stdout);
sem_post(&sem3);
}
pthread_exit(NULL);
}
//定义线程3
void *task3(void *arg)
{
int num=5;
while(num--)
{
sem_wait(&sem3);
sleep(1);
printf("C");
fflush(stdout);
sem_post(&sem1);
}
pthread_exit(NULL);
}
int main(int argc, const char *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");
return 0;
}
if(pthread_create(&tid2,NULL,task2,NULL)!=0)
{
printf("tid2 create error\n");
return 0;
}
if(pthread_create(&tid3,NULL,task3,NULL)!=0)
{
printf("tid3 create error\n");
return 0;
}
printf("tid1:%#lx,tid2:%#lx,tid3:%#lx\n",tid1,tid2,tid3);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
puts("");
return 0;
}
运行结果:
程序代码:
#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_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);
}
int main(int argc, char const *argv[])
{
pthread_mutex_init(&mutex, NULL);
pthread_t thread1, thread2, thread3, thread4, thread5, thread6;
if (pthread_create(&thread1, NULL, task1, NULL) != 0 || \
pthread_create(&thread2, NULL, task2, NULL) != 0 || \
pthread_create(&thread3, NULL, task2, NULL) != 0 || \
pthread_create(&thread4, NULL, task2, NULL) != 0 || \
pthread_create(&thread5, NULL, task2, NULL) != 0 || \
pthread_create(&thread6, NULL, task2, NULL) != 0)
{
perror("thread create error");
return -1;
}
printf("thread1:%#lx\nthread2:%#lx\nthread3:%#lx\nthread4:%#lx\nthread5:%#lx\nthread6:%#lx\n",thread1, thread2, thread3, thread4, thread5, thread6);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
pthread_join(thread5, NULL);
pthread_join(thread6, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
运行结果:
程序代码:
#include<myhead.h>
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]);
pid_t pid = fork();
if (pid > 0)
{
close(pipefd[0]);
char wbuf[128] = "";
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[128] = "";
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;
}
运行结果:
程序代码:
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./mkfifo1",0664)==-1)
{
perror("mkfifo error");
return -1;
}
if(mkfifo("./mkfifo2",0664)==-1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm mkfifo1");
system("rm mkfifo2");
return 0;
}
#include <myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid=fork();
if(pid>0)
{
//父进程
//向管道1中写入数据
int fd=-1;
if((fd=open("./mkfifo1",O_WRONLY))==-1)
{
perror("open error");
return -1;
}
char wbuf[128]="";
while(1)
{
bzero(wbuf,sizeof(wbuf));
// printf("请输入>>>:");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
write(fd,wbuf,sizeof(wbuf));
if(strcmp(wbuf,"quit")==0)
{
break;
}
}
close(fd);
}else if(pid==0)
{
//子进程
//从管道2中读取数据
int fd1=-1;
if((fd1=open("./mkfifo2",O_RDONLY))==-1)
{
perror("open error");
return -1;
}
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
read(fd1,rbuf,sizeof(rbuf));
if(strcmp(rbuf,"quit")==0)
{
break;
}
printf("程序B发送的消息:%s\n",rbuf);
}
close(fd1);
}else
{
perror("fork error");
return -1;
}
return 0;
}
#include <myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid=fork();
if(pid>0)
{
//父进程
//从管道1中读取数据
int fd=-1;
if((fd=open("./mkfifo1",O_RDONLY))==-1)
{
perror("open error");
return -1;
}
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
// printf("请输入>>>:");
// fgets(wbuf,sizeof(wbuf),stdin);
// wbuf[strlen(wbuf)-1]=0;
read(fd,rbuf,sizeof(rbuf));
if(strcmp(rbuf,"quit")==0)
{
break;
}
printf("从程序A中读取的数据:%s\n",rbuf);
}
close(fd);
}else if(pid==0)
{
//子进程
//向管道2中写入数据
int fd1=-1;
if((fd1=open("./mkfifo2",O_WRONLY))==-1)
{
perror("open error");
return -1;
}
char wbuf[128]="";
while(1)
{
bzero(wbuf,sizeof(wbuf));
// printf("请输入>>>:");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
write(fd1,wbuf,sizeof(wbuf));
if(strcmp(wbuf,"quit")==0)
{
break;
}
// printf("程序B发送的消息:%s\n",rbuf);
}
close(fd1);
}else
{
perror("fork error");
return -1;
}
return 0;
}
程序代码:
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./mkfifo1",0664)==-1)
{
perror("mkfifo error");
return -1;
}
if(mkfifo("./mkfifo2",0664)==-1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm mkfifo1");
system("rm mkfifo2");
return 0;
}
#include<myhead.h>
void *send_message(void *arg)
{
int wfd=-1;
if((wfd=open("./fasong",O_WRONLY))==-1)
{
perror("open error");
return NULL;
}
char wbuf[128]="";
while(1)
{
printf("请输入>>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
write(wfd,wbuf,strlen(wbuf));
if(strcmp(wbuf,"quit")==0)
break;
}
close(wfd);
pthread_exit(NULL);
}
void *receive_message(void *arg)
{
int rfd=-1;
if((rfd=open("./jieshou",O_RDONLY))==-1)
{
perror("open error");
return NULL;
}
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
read(rfd,rbuf,sizeof(rbuf));
printf("收到信息:%s\n",rbuf);
if(strcmp(rbuf,"quit")==0)
break;
}
close(rfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;
if(pthread_create(&tid1,NULL,send_message,NULL)!=0)
{
puts("tid1 create error");
return -1;
}
if(pthread_create(&tid2,NULL,receive_message,NULL)!=0)
{
puts("tid2 create error");
return -1;
}
if(pthread_join(tid1,NULL)==0 && pthread_join(tid2,NULL)==0)
{
puts("user2 closed send&receive");
}
return 0;
}
#include<myhead.h>
void *send_message(void *arg)//发送信息
{
int wfd=-1;
if((wfd=open("./fasong",O_WRONLY))==-1)
{
perror("open error");
return NULL;
}
char wbuf[128]="";
while(1)
{
printf("请输入>>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
write(wfd,wbuf,strlen(wbuf));
if(strcmp(wbuf,"quit")==0)
break;
}
close(wfd);
pthread_exit(NULL);
}
void *receive_message(void *arg)//接收消息
{
int rfd=-1;
if((rfd=open("./jieshou",O_RDONLY))==-1)
{
perror("open error");
return NULL;
}
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
read(rfd,rbuf,sizeof(rbuf));
printf("收到信息:%s\n",rbuf);
if(strcmp(rbuf,"quit")==0)
break;
}
close(rfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1,tid2;//创建两个线程
//发送
if(pthread_create(&tid1,NULL,send_message,NULL)!=0)
{
puts("tid1 create error");
return -1;
}
//接收
if(pthread_create(&tid2,NULL,receive_message,NULL)!=0)
{
puts("tid2 create error");
return -1;
}
if(pthread_join(tid1,NULL)==0 && pthread_join(tid2,NULL)==0)
{
puts("user1 closed send&receive");
}
return 0;
}