Initial commit from https://devstar.cn/lat5211/base.git ( ed411359a0b35dccda674d15913e38a24a6d006b )

This commit is contained in:
2025-10-24 02:54:28 +00:00
commit 093eceb323
14 changed files with 592 additions and 0 deletions

39
src/execlp.c Normal file
View File

@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
(void)argc; // 明确标记未使用
(void)argv; // 消除编译器警告
pid_t pid = fork(); // 使用pid_t类型更规范
if (pid < 0) {
/* 错误处理 */
perror("Fork Failed");
exit(EXIT_FAILURE); // 使用标准失败码
}
else if (pid == 0) {
/* 子进程 */
execlp("/bin/ls", "ls", (char *)NULL); // 确保NULL转换为(char *)
// 如果exec失败才会执行到这里
perror("Exec Failed");
exit(EXIT_FAILURE);
}
else {
/* 父进程 */
int status;
wait(&status); // 获取子进程退出状态
if (WIFEXITED(status)) {
printf("Child Completed with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child terminated abnormally\n");
}
exit(EXIT_SUCCESS); // 使用标准成功码
}
}

15
src/fork.c Normal file
View File

@@ -0,0 +1,15 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
int main ()
{
pid_t pid;
pid=fork();
if (pid < 0)
printf("error in fork!");
else if (pid == 0)
printf("i am the child process, my process id is %d\n",getpid());
else
printf("i am the parent process, my process id is %d\n",getpid());
}

16
src/getSum.c Normal file
View File

@@ -0,0 +1,16 @@
#include <stdio.h>
int getSum(void)
{
int sum = 0;
for (int i = 1; i <= 100; ++i) {
sum += i;
}
return sum;
}
int main(void)
{
printf("%d\n", getSum());
return 0;
}