作业一、
标准IO函数时候讲解的时钟代码,要求输入quit字符串后,结束进程
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
void *callBack(void *arg)
{
int i = 0, n = 0;
time_t t;
time(&t);
struct tm *info = NULL;
info = localtime(&t);
while (1)
{
fprintf(stderr, "[%d]%d-%02d-%02d %02d:%02d:%02d\n",
n++, info->tm_year + 1900, info->tm_mon + 1, info->tm_mday,
info->tm_hour, info->tm_min, info->tm_sec);
i++;
sleep(1);
system("clear");
}
}
int main(int argc, const char *argv[])
{
pthread_t tid;
if (pthread_create(&tid, NULL, callBack, NULL) != 0)
{
fprintf(stderr, "pthread_create failed__%d__\n", __LINE__);
return -1;
}
while (1)
{
char str[20] = "";
scanf("%s", str);
if (strcmp(str, "quit") == 0)
{
return 0;
}
bzero(str,sizeof(str));
}
while (1)
sleep(1);
return 0;
}
作业二、
要求定义一个全局变量 char buf[] = "1234567",创建两个线程,不考虑退出条件。
A线程循环打印buf字符串,
B线程循环倒置buf字符串,即buf中本来存储1234567,倒置后buf中存储7654321. 不打印!!
倒置不允许使用辅助数组。
要求A线程打印出来的结果只能为 1234567 或者 7654321 不允许出现7634521 7234567
不允许使用sleep函数
互斥法
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdlib.h>
char buf[] = "123";
pthread_mutex_t lock;
void *my_printf(void *arg)
{
while (1)
{
pthread_mutex_lock(&lock);
//sleep(5);
printf("%s\n", buf);
pthread_mutex_unlock(&lock);
}
pthread_exit(NULL);
}
void *swop(void *arg)
{
int len = strlen(buf);
while (1)
{
int i = 0, j = 0;
j = len - 1;
pthread_mutex_lock(&lock);
while (i < j)
{
char t = buf[i];
buf[i] = buf[j];
buf[j] = t;
i++;
j--;
}
pthread_mutex_unlock(&lock);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
pthread_mutex_init(&lock, NULL);
if (pthread_create(&tid1, NULL, my_printf, NULL) != 0)
{
printf("pthread_create failed__%d__\n", __LINE__);
return -1;
}
if (pthread_create(&tid2, NULL, swop, NULL) != 0)
{
printf("pthread_create failed__%d__\n", __LINE__);
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_mutex_destroy(&lock);
return 0;
}
信号法
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
char buf[] = "123";
sem_t sem1,sem2;
void *my_printf(void *arg)
{
while (1)
{
sem_wait(&sem1);
printf("%s\n", buf);
sem_post(&sem2);
}
pthread_exit(NULL);
}
void *swop(void *arg)
{
int len = strlen(buf);
while (1)
{
sem_wait(&sem2);
int i = 0, j = 0;
j = len - 1;
while (i < j)
{
char t = buf[i];
buf[i] = buf[j];
buf[j] = t;
i++;
j--;
}
sem_post(&sem1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
sem_init(&sem1,0,1);
sem_init(&sem2,0,0);
if (pthread_create(&tid1, NULL, my_printf, NULL) != 0)
{
printf("pthread_create failed__%d__\n", __LINE__);
return -1;
}
if (pthread_create(&tid2, NULL, swop, NULL) != 0)
{
printf("pthread_create failed__%d__\n", __LINE__);
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
return 0;
}
作业三、
要求用两个线程拷贝一张图片。A线程拷贝前半部分,B线程拷贝后半部分,不允许使用sleep函数
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
int i = 0, j = 0;
void *cpy1(void *arg)
{
int fd_r = open("./1.jpg", O_RDONLY);
int fd_w = open("./2.jpg", O_WRONLY | O_TRUNC);
if (fd_r < 0)
{
perror("open");
printf("fd_r failed__%d__\n", __LINE__);
return NULL;
}
if (fd_w < 0)
{
perror("open");
printf("fd_w failed__%d__\n", __LINE__);
return NULL;
}
off_t len = lseek(fd_r, 0, SEEK_END);
if (len % 2 != 0)
{
len += 1;
}
int half = len / 2;
//printf("1=%d\n", half);
lseek(fd_r, 0, SEEK_SET);
char c;
for (; i < half; i++)
{
read(fd_r, &c, sizeof(c));
write(fd_w, &c, sizeof(c));
}
printf("退出前半部分\n");
pthread_exit(&arg);
}
void *cpy2(void *arg)
{
pthread_join(*(pthread_t *)arg, NULL);
int fd_r = open("./1.jpg", O_RDONLY);
int fd_w = open("./2.jpg", O_WRONLY | O_APPEND);
if (fd_r < 0)
{
perror("open");
printf("fd_r failed__%d__\n", __LINE__);
return NULL;
}
if (fd_w < 0)
{
perror("open");
printf("fd_w failed__%d__\n", __LINE__);
return NULL;
}
off_t len = lseek(fd_r, 0, SEEK_END);
if (len % 2 != 0)
{
len += 1;
}
int half = len / 2;
//printf("2=%d\n", half);
lseek(fd_r, half, SEEK_SET);
lseek(fd_w, half, SEEK_SET);
char c;
for (; j < half; j++)
{
read(fd_r, &c, sizeof(c));
write(fd_w, &c, sizeof(c));
}
printf("退出后半部分\n");
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
pthread_t tid1, tid2;
if (pthread_create(&tid1, NULL, cpy1, NULL))
{
printf("pthread_create__%d__\n", __LINE__);
return -1;
}
if (pthread_create(&tid2, NULL, cpy2, &tid1))
{
printf("pthread_create__%d__\n", __LINE__);
return -1;
}
pthread_join(tid2, NULL);
printf("结束复制\n");
return 0;
}
文章展示了三个多线程编程的示例:一是使用标准IO函数并结合线程实现时间显示,直到输入quit退出;二是通过互斥锁实现线程安全的字符串倒置和打印,保证输出正确性;三是利用两个线程分别拷贝图片的前半部分和后半部分,不使用sleep函数进行同步。
593

被折叠的 条评论
为什么被折叠?



