diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cdf9caf --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "types.h": "c", + "stat.h": "c", + "unistd.h": "c" + } +} \ No newline at end of file diff --git a/Code/copy.txt b/Code/copy.txt new file mode 100644 index 0000000..e69de29 diff --git a/Code/io.c b/Code/io.c new file mode 100644 index 0000000..b2f01ca --- /dev/null +++ b/Code/io.c @@ -0,0 +1,67 @@ +/**************************************************************** +题目: +实现文件的拷贝,将一个有数据的文件拷贝它的数据到另一个空白的文件里面。 +***************************************************************** +先在/home/xk/目录下创建一个origin.txt文件和一个copy.txt文件 +然后在origin.txt文件中写入一些数据 +*****************************************************************/ + +#include +#include +#include +#include +#include +#include + +int main() +{ + int open_status; + int read_status; + int write_status; + int close_status; + char buf[256] = {0}; + + //1、打开源文件 + open_status = open("/home/xk/origin.txt", O_RDWR); + if(open_status == -1) + printf("打开源文件失败\n"); + else + printf("打开源文件成功\n"); + + //2、拷贝源文件数据 + read_status = read(open_status, buf, sizeof(buf) - 1); + if(read_status > 0) + printf("已复制源文件内容为:%s", buf); + else + return -1; + + //3、关闭源文件 + close_status = close(open_status); + if(close_status == 0) + printf("关闭源文件成功\n\n"); + else + printf("关闭源文件失败\n\n"); + + //4、打开目标文件 + open_status = open("/home/xk/copy.txt", O_RDWR); + if(open_status == -1) + printf("打开目标文件失败\n"); + else + printf("打开目标文件成功\n"); + + //5、写入数据到目标文件 + write_status = write(open_status, buf, strlen(buf)); + if(write_status > 0) + printf("复制成功,有%d个字符\n", write_status); + else + return -1; + + //6、关闭目标文件 + close_status = close(open_status); + if(close_status == 0) + printf("关闭目标文件成功\n"); + else + printf("关闭目标文件失败\n"); + + return 0; +} \ No newline at end of file diff --git a/Code/io2.c b/Code/io2.c new file mode 100644 index 0000000..7843990 --- /dev/null +++ b/Code/io2.c @@ -0,0 +1,56 @@ +/************************************************ +使用标准IO完成文件拷贝 +************************************************/ +#include + +int main() +{ + FILE *fp1, *fp2; + char buf[256] = {0}; + int read_status, write_status, close_status; + + //1、打开源文件 + fp1 = fopen("./origin.txt", "r"); + if(fp1 == NULL) + printf("打开源文件失败\n"); + else + printf("打开源文件成功\n"); + + //2、拷贝源文件数据 + read_status = fread(buf, sizeof(char), sizeof(buf) - 1, fp1); + if(read_status > 0) + printf("已复制源文件内容为:%s", buf); + else + return -1; + + //3、关闭源文件 + close_status = fclose(fp1); + if(close_status == 0) + printf("关闭源文件成功\n\n"); + else + printf("关闭源文件失败\n\n"); + + //4、打开目标文件 + fp2 = fopen("./copy.txt", "w"); + if(fp2 == NULL) + printf("打开目标文件失败\n"); + else + printf("打开目标文件成功\n"); + + //5、写入数据到目标文件 + write_status = fwrite(buf, sizeof(char), read_status, fp2); + if(write_status > 0) + printf("复制成功,有%d个字符\n", write_status); + else + return -1; + + //6、关闭目标文件 + close_status = fclose(fp2); + if(close_status == 0) + printf("关闭目标文件成功\n"); + else + printf("关闭目标文件失败\n"); + + + return 0; +} \ No newline at end of file diff --git a/Code/jincheng.c b/Code/jincheng.c new file mode 100644 index 0000000..114f924 --- /dev/null +++ b/Code/jincheng.c @@ -0,0 +1,39 @@ +/*************************************************** +子进程每隔1秒打印一次apple,父进程每隔2秒打印一次hello。 +****************************************************/ +#include +#include +#include + +int main() +{ + // 创建子进程 + pid_t pid = fork(); + + // 错误处理 + if (pid < 0) + { + printf("创建子进程失败\n"); + return -1; + } + + // 子进程 + else if (pid == 0) + { + while (1) { + printf("apple\n"); + sleep(1); // 每隔1秒打印一次apple + } + } + + // 父进程 + else + { + while (1) { + printf("hello\n"); + sleep(2); // 每隔2秒打印一次hello + } + } + + return 0; // 程序结束 +} \ No newline at end of file diff --git a/Code/jincheng2.c b/Code/jincheng2.c new file mode 100644 index 0000000..d863791 --- /dev/null +++ b/Code/jincheng2.c @@ -0,0 +1,52 @@ +/********************************************** +创建一个子进程,打印hello后父进程打印world,之后父进程再创建两条进程,一个进程打印end,另一个进程打印... + **********************************************/ + +#include +#include +#include +#include + +int main() +{ + // 创建子进程 + pid_t pid = fork(); + + // 错误处理 + if (pid < 0) + { + printf("创建子进程失败\n"); + return -1; + } + + // 子进程 + else if (pid == 0) + { + printf("hello\n"); + } + + // 父进程 + else + { + wait(NULL); // 等待子进程结束 + + printf("world\n"); + + // 创建两个新的子进程 + pid_t pid1 = fork(); + if (pid1 == 0) + { + printf("end\n"); + } + else + { + pid_t pid2 = fork(); + if (pid2 == 0) + { + printf("...\n"); + } + } + } + + return 0; // 程序结束 +} \ No newline at end of file diff --git a/Code/origin.txt b/Code/origin.txt new file mode 100644 index 0000000..90bfcb5 --- /dev/null +++ b/Code/origin.txt @@ -0,0 +1 @@ +this is a test diff --git a/Code/output/io b/Code/output/io new file mode 100644 index 0000000..4176a63 Binary files /dev/null and b/Code/output/io differ diff --git a/Code/output/io2 b/Code/output/io2 new file mode 100644 index 0000000..74ec280 Binary files /dev/null and b/Code/output/io2 differ diff --git a/Code/output/jincheng b/Code/output/jincheng new file mode 100644 index 0000000..a8e51bd Binary files /dev/null and b/Code/output/jincheng differ diff --git a/Code/output/jincheng2 b/Code/output/jincheng2 new file mode 100644 index 0000000..b1a361a Binary files /dev/null and b/Code/output/jincheng2 differ diff --git a/Code/output/xiancheng b/Code/output/xiancheng new file mode 100644 index 0000000..2111323 Binary files /dev/null and b/Code/output/xiancheng differ diff --git a/Code/xiancheng.c b/Code/xiancheng.c new file mode 100644 index 0000000..b6b74e6 --- /dev/null +++ b/Code/xiancheng.c @@ -0,0 +1,14 @@ +#include +#include + +void* start_routine(void* arg) +{ + printf("Thread is running...\n"); +} + +int main() +{ + + + return 0; +} \ No newline at end of file