#include <stdio.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
//开子进程
pid_t pid = fork();
if(pid > 0)
{
execl("./f_execl", "./f_execl", NULL);
printf("****\n");
}
else if(0 == pid)
{
sleep(5);
execl("./s_execl", "./s_execl", NULL);
}
else
{
perror("fork");
return -1;
}
return 0;
}
#include <stdio.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./1.png", "r");
if(NULL == fp)
{
perror("fopen1");
return -1;
}
printf("fp is open \n");
FILE* fo = fopen("./2.png", "w");
if(NULL == fo)
{
perror("fopen2");
return -1;
}
printf("fo is open \n");
struct stat buf;
int sta = stat("./1.png", &buf);
if(sta < 0)
{
perror("stat");
return -1;
}
off_t off = buf.st_size;
printf("off_f = %ld\n",off);
int size = 0;
char a = 0;
for(size; size < off / 2; size++)
{
fscanf(fp, "%c", &a);
fprintf(fo, "%c", a);
}
fclose(fp);
fclose(fo);
return 0;
}
#include <stdio.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./1.png", "r");
if(NULL == fp)
{
perror("fopen1");
return -1;
}
FILE* fo = fopen("./2.png", "a");
if(NULL == fo)
{
perror("fopen2");
return -1;
}
//求原图大小
struct stat buf;
int sta = stat("./1.png", &buf);
if(sta < 0)
{
perror("stat");
return -1;
}
off_t off = buf.st_size;
int size = 0;
char a = 0;
//修改指针
fseek(fp, off/2, SEEK_SET);
fseek(fo, off/2, SEEK_SET);
//复制
for(size = off / 2; size < off ; size++)
{
fscanf(fp, "%c", &a);
fprintf(fo, "%c", a);
}
fclose(fp);
fclose(fo);
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
char buf[] = "1234567";
void* Breadth_Fun_A(void* arg_a)
{
for(int i = 0; i < strlen(buf); i++)
printf("buf_A_%d = %s\n",i,buf);
return NULL;
}
void* Breadth_Fun_B(void* arg_b)
{
int len = strlen(buf);
for(int i = 0; i < strlen(buf) / 2; i++)
{
buf[i] = buf[i] ^ buf[len-1-i];
buf[len-1-i] = buf[i] ^ buf[len-1-i];
buf[i] = buf[i] ^ buf[len-1-i];
}
}
int main(int argc, const char *argv[])
{
pthread_t thread_b;
pthread_t thread_a;
int ptc_b = pthread_create(&thread_b, NULL, Breadth_Fun_B , NULL );
if(ptc_b != 0)
{
fprintf(stderr, "B is fail\n");
return -1;
}
int ptc_a = pthread_create(&thread_a, NULL, Breadth_Fun_A, NULL);
if(ptc_a != 0)
{
fprintf(stderr, "A is fail\n");
return -1;
}
return 0;
}
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
void* Breath_Fun_A(void* arg_a)
{
//开文件
FILE*fp = fopen("./1.png", "r");
if(NULL == fp)
{
perror("fp");
return NULL;
}
FILE* fo = fopen("./3.png", "w");
if(NULL == fo)
{
perror("fo");
return NULL;
}
//计算fp文件大小
struct stat buf;
int st = stat("./1.png", &buf);
if(st < 0)
{
perror("stat");
return NULL;
}
int size = buf.st_size;
printf("size = %d\n",size);
fseek(fp, 0, SEEK_SET);
fseek(fo, 0, SEEK_SET);
char str = 0;
for(int i = 0; i < size / 2; i++)
{
fscanf(fp, "%c", &str);
fprintf(fo, "%c", str);
}
printf("前半部打印完毕\n");
fclose(fp);
fclose(fo);
return NULL;
}
void* Breath_Fun_B(void* arg_b)
{
sleep(1);
//开文件
FILE*fp = fopen("./1.png", "r");
if(NULL == fp)
{
perror("fp");
return NULL;
}
FILE* fo = fopen("./3.png", "a");
if(NULL == fo)
{
perror("fo");
return NULL;
}
//计算fp文件大小
struct stat buf;
int st = stat("./1.png", &buf);
if(st < 0)
{
perror("stat");
return NULL;
}
int size = buf.st_size;
printf("size = %d\n",size);
fseek(fp, size / 2, SEEK_SET);
fseek(fo, 0, SEEK_END);
char str = 0;
for(int i = 0; i < size / 2; i++)
{
fscanf(fp, "%c", &str);
fprintf(fo, "%c", str);
}
printf("后半部打印完毕\n");
fclose(fp);
fclose(fo);
return NULL;
}
int main(int argc, const char *argv[])
{
pthread_t thread_a;
int ptc_a = pthread_create(&thread_a, NULL, Breath_Fun_A, NULL);
if(ptc_a != 0)
{
fprintf(stderr, "A is fail\n");
return -1;
}
pthread_t thread_b;
int ptc_b = pthread_create(&thread_b, NULL, Breath_Fun_B, NULL);
if(ptc_b != 0)
{
fprintf(stderr, "B is fail\n");
return -1;
}
sleep(3);
return 0;
}