1.标准IO函数得到当前时间并在终端循环打印当前时间,在终端输入exit后,结束进程
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#define ERR_MSG(msg) {\
fprintf(stderr,"line: %d\n",__LINE__);\
perror(msg);\
}
void *timefun(void *type){
time_t timedata;
struct tm *timelist;
while(1){
time(&timedata);
timelist = localtime(&timedata);
fprintf(stdout,"%4d-%02d-%02d %02d:%02d:%02d\n",\
timelist->tm_year+1900,timelist->tm_mon+1,\
timelist->tm_mday,timelist->tm_hour,\
timelist->tm_min,timelist->tm_sec);
sleep(1);
}
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t pressid;
if(pthread_create(&pressid, NULL, timefun, NULL) != 0){
fprintf(stderr,"pthread_create is failed line: %d \n",__LINE__);
return -1;
}
char str[10] = "\0";
while(1){
scanf("%s",str);
getchar();
if(strcmp(str,"exit") == 0){
break;
}
}
return 0;
}
结果如下:
2.要求定义一个全局变量char buf[] = "1234567",创建2个进程,不考虑退出条件
>A线程循环打印buf字符串
>B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321,不打印
>倒置不允许使用辅助数组
>要求A线程打印出来的结果只能为1234567 或 7654321
>不允许使用sleep函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#define ERR_MSG(msg) {\
fprintf(stderr,"line: %d\n",__LINE__);\
perror(msg);\
}
char buf[] = "1234567";
void *pthreadBfun(void *dataB){
int len = strlen(buf);
int left = 0;
int right = len - 1;
while(left < right){
char mid = buf[left];
buf[left] = buf[right];
buf[right] = mid;
left ++;
right --;
}
pthread_exit(NULL);
}
void *pthreadAfun(void *dataA){
while(1){
pthread_t pthreadidB;
if(pthread_create(&pthreadidB, NULL, pthreadBfun, NULL) != 0){
fprintf(stderr,"pthreadB is failed line: %d \n",__LINE__);
pthread_exit(NULL);
}
printf("A : %s\n",buf);
pthread_join(pthreadidB,NULL);
}
}
int main(int argc, const char *argv[])
{
pthread_t pthreadidA;
if(pthread_create(&pthreadidA, NULL, pthreadAfun, NULL) != 0){
fprintf(stderr,"pthreadA is failed line: %d \n",__LINE__);
return -1;
}
pthread_join(pthreadidA,NULL);
return 0;
}
结果如下:
3.要求使用2个线程拷贝一张图片,A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#define ERR_MSG(msg) {\
fprintf(stderr,"line: %d\n",__LINE__);\
perror(msg);\
}
void *pthreadBfun(void *dataB){
int picidB = open((char *)dataB,O_RDONLY);
if(picidB < 0){
ERR_MSG("openB");
pthread_exit(NULL);
}
int picidcpyB = open("./4.png",O_WRONLY|O_CREAT|O_APPEND,0664);
if(picidcpyB < 0){
ERR_MSG("opencpyB");
pthread_exit(NULL);
}
off_t size = lseek(picidB,0,SEEK_END);
off_t i = size/2;
lseek(picidB,i,SEEK_SET);
char datapicB[2] = "\0";
while(i < size){
bzero(datapicB,sizeof(datapicB));
ssize_t len = read(picidB,datapicB,1);
if(len == 0){
break;
}
else if(len < 0){
ERR_MSG("read");
pthread_exit(NULL);
}
write(picidcpyB,datapicB,len);
i++;
}
pthread_exit(NULL);
}
void *pthreadAfun(void *dataA){
int picid = open((char *)dataA,O_RDONLY);
if(picid < 0){
ERR_MSG("open");
pthread_exit(NULL);
}
int picidcpy = open("./4.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(picidcpy < 0){
ERR_MSG("opencpy");
pthread_exit(NULL);
}
off_t size = lseek(picid,0,SEEK_END);
off_t i = 0;
char datapic[2] = "\0";
lseek(picidcpy,0,SEEK_SET);
lseek(picid,0,SEEK_SET);
while(i < size/2){
bzero(datapic,sizeof(datapic));
ssize_t len = read(picid,datapic,1);
if(len == 0){
break;
}
else if(len < 0){
ERR_MSG("read");
pthread_exit(NULL);
}
write(picidcpy,datapic,len);
i++;
}
pthread_t pthreadidB;
if(pthread_create(&pthreadidB,NULL,pthreadBfun,(char *)dataA) != 0){
fprintf(stderr,"pthreadBfun is failed line: %d\n",__LINE__);
pthread_exit(NULL);
}
pthread_join(pthreadidB,NULL);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t pthreadidA;
char lujin[20];
strcpy(lujin,argv[1]);
if(pthread_create(&pthreadidA,NULL,pthreadAfun,lujin) != 0){
fprintf(stderr,"pthreadAfun is failed line: %d\n",__LINE__);
return -1;
}
pthread_join(pthreadidA,NULL);
return 0;
}
结果如下:
我在终端自己输入想要拷贝的路径,然后拷贝到当前路径,重命名为4.png