1.时间输入quit退出
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
//时间循环
void * Time(void* arg){
time_t t ;
struct tm *info = NULL;
while(1){
time (&t);
info = localtime (&t);
fprintf(stderr,"%d-%02d-%02d %02d:%02d:%02d\r",
info->tm_year + 1900, info->tm_mon + 1, info->tm_mday,
info->tm_hour, info->tm_min, info->tm_sec);
sleep(1);
}
}
int main(int argc,const char * argv[])
{
pthread_t tid, tid1;
char str[10] = "";
if(pthread_create(&tid,NULL,Time,NULL) != 0){
printf("error\n");
return -1;
}
while(1){
scanf("%s",str);
//printf("%s",str);
if(0 == strcmp(str,"quit")){
return 0;
}
memset(str,0,sizeof(str));
}
return 0;
}
2.逆置打印
//普通的最优解
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <semaphore.h>
char str[10] = "1234567";
int flag = 0;
void* inversion(void* arg){
while(1){
while(flag != 0){
continue;
}
int i = 0, j = strlen(str)-1;
while(i < j){//逆置
*(str+i) = *(str+i) + *(str+j);
*(str+j) = *(str+i) - *(str+j);
*(str+i) = *(str+i) - *(str+j);
i++;
j--;
}
flag = 1;
}
pthread_exit(NULL);
}
void* show(void* arg){
while(1){
while(0 == flag){
continue;
}
printf("%s\n",str);
flag = 0;
}
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
//创建两个线程
pthread_t tid,tid1;
//创建第一个线程
if(pthread_create(&tid,NULL,inversion,NULL) != 0){
printf("pthread_creat error");
return -1;
}
//创建第二个线程
if(pthread_create(&tid1,NULL,show,NULL) != 0){
printf("pthread_creat error");
return -1;
}
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
return 0;
}
//
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
char str[] = "1234567";
void *told(void* arg){//一号线程逆置
//交换str
//printf("%s\n",str);
int i = 0, j = strlen(str)-1;
while(i < j){//一次相加一次相减
str[i] = str[i] +str[j];
str[j] = str[i] - str[j];
str[i] = str[i] - str[j];
i++;j--;
}
printf("%s\n",str);
pthread_exit(NULL);
}
void *show(void* arg){//二号线程
printf("%s\n",str);
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
pthread_t tid, tid1;
while(1){//创建进程并循环
if(pthread_create(&tid,NULL,told,NULL) != 0){
printf("error");
return -1;
}
pthread_join(tid,NULL); //阻塞逆置,防止乱码
if(pthread_create(&tid1,NULL,show,NULL) != 0){
printf("error");
return -1;
}
pthread_join(tid1,NULL);//阻塞输出,防止输出错误
}
return 0;
}
//第二种
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
void *inverson(void* arg){//逆置
char *str = (char*)arg;
int i = 0,j = strlen(str)-1;
while(i<j){
*(str+i) = *(str+i) + *(str+j);
*(str+j) = *(str+i) - *(str+j);
*(str+i) = *(str+i) - *(str+j);
i++;
j--;
}
//调试printf("%s__%d__\n",str,__LINE__);
pthread_exit(NULL);
}
void *show(void* arg){//输出
char * str = (char*)arg;
printf("%s\n",str);
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
char str[] = "1234567";
pthread_t tid = 0, tid1 = 0;
while(1){
if(pthread_create(&tid,NULL,inverson,(void *)str) != 0){//循环创建线程1
printf("error");
return 1;
}
pthread_join(tid,NULL);
//调试printf("%s__%d__\n",str,__LINE__);
if(pthread_create(&tid1,NULL,show,(void *)str) != 0){//循环创建线程2
printf("error");
return 1;
}
pthread_join(tid1,NULL);
}
return 0;
}
//互斥锁的解法
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
//定义一个全局变量
char str[10] = "1234567";
//创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* inversion(void* arg){
while(1){
//printf("%s __%d__\n",str,__LINE__);测试
pthread_mutex_lock(&mutex);//上锁
int i = 0, j = strlen(str)-1;
while(i < j){//逆置
*(str+i) = *(str+i) + *(str+j);
*(str+j) = *(str+i) - *(str+j);
*(str+i) = *(str+i) - *(str+j);
i++;
j--;
}
pthread_mutex_unlock(&mutex);//关锁
}
pthread_exit(NULL);
}
void* show(void* arg){
while(1){
pthread_mutex_lock(&mutex);
printf("%s\n",str);
pthread_mutex_unlock(&mutex);//关锁,从上锁开始到关锁是一个循环
}
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
//创建两个线程
pthread_t tid,tid1;
//创建第一个线程
if(pthread_create(&tid,NULL,inversion,NULL) != 0){
printf("pthread_creat error");
return -1;
}
//创建第二个线程
if(pthread_create(&tid1,NULL,show,NULL) != 0){
printf("pthread_creat error");
return -1;
}
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
pthread_mutex_destroy(&mutex);//关闭互斥锁
return 0;;
}
//最优解依次输出1234567 7654321
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <semaphore.h>
char str[10] = "1234567";
//创建无名信号量
sem_t sem;
sem_t sem1;
//逆置
void* inverson(void* arg){
while(1){
//sleep(1);
sem_wait(&sem1);//等待锁二给信号
int i = 0, j = strlen(str)-1;
while(i < j){//逆置
*(str+i) = *(str+i) + *(str+j);
*(str+j) = *(str+i) - *(str+j);
*(str+i) = *(str+i) - *(str+j);
i++;
j--;
}
sem_post(&sem);
}
pthread_exit(NULL);
}
//展示
void* show(void* arg){
while(1){
sem_wait(&sem);//等待锁一信号
printf("%s\n",str);
sem_post(&sem1);
}
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
//插入无名信号量
sem_init(&sem,0,1);
sem_init(&sem1,0,0);
//创建线程
pthread_t tid,tid1;
if(pthread_create(&tid,NULL,inverson,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
if(pthread_create(&tid1,NULL,show,NULL) != 0){
printf("pthread_create error\n");
return -1;
}
//接收线程结束
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
sem_destroy(&sem);
return 0;
}
3.打印图片
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
int size = 5685030;//全局变量是文件大小
void* tophalf(void* arg){
//打开图片
int fd = open("/home/ubuntu/xwf/IOday4/labixiaoxin.bmp",O_RDONLY);
if(fd < 0){
perror("open");
return NULL;
}
//打开写入文件
int fd1 = open("labixiaoxincopy.bmp",O_WRONLY|O_CREAT|O_TRUNC,0666);
if(fd1 < 0){
perror("open");
return NULL;
}
char buf = '\0';
for(int i = 0; i <size/2; i++){
read(fd,&buf,sizeof(buf));
write(fd1,&buf,sizeof(buf));
}
printf("上半部分完成\n");
pthread_exit(NULL);
}
void* upperhalf(void* arg){
//打开图片
int fd = open("/home/ubuntu/xwf/IOday4/labixiaoxin.bmp",O_RDONLY);
if(fd < 0){
perror("open");
return NULL;
}
//创建打开写入的图片
int fd1 = open("/home/ubuntu/xwf/IOday4/labixiaoxincopy.bmp",O_WRONLY|O_CREAT,0666);
if(fd1 < 0){
perror("open");
return NULL;
}
char buf = '\0';
lseek(fd,size/2,SEEK_SET);
lseek(fd1,size/2,SEEK_SET);
for(int i = size/2; i <size; i++){//循环图的一半次数
read(fd,&buf,sizeof(buf));//读取
write(fd1,&buf,sizeof(buf));//写入
}
printf("下半部分完成\n");
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
pthread_t tid = 0,tid1 = 0;
//创建第一个线程
if(pthread_create(&tid,NULL,tophalf,NULL) != 0){
printf("error\n");
return -1;
}
//创建第二个线程
if(pthread_create(&tid1,NULL,upperhalf,NULL) != 0){
printf("error\n");
return -1;
}
//等待线程结束
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
return 0;
}
打开后的图
打开前的图