C语言实现操作系统相关操作
进程相关
fork
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
pid_t fork(void);
fork()
函数的使用
fork()
是一个系统调用,用于创建一个和当前进程映像相同的子进程。fork执行一次,返回两次,可能有三种不同的值。如果执行成功,在父进程中返回子进程的ID,在子进程中返回0,如果执行失败返回-1。声明在
<unistd.h>
头文件中
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid = fork();/*从此处开始以不同进程的视角开始执行*/
if (pid < 0) {
// 出错处理
perror("fork failed");
return 1;
} else if (pid == 0) {
// 子进程
printf("I am the child process with PID %d\n", getpid());
} else {
// 父进程
printf("I am the parent process with PID %d\n", getpid());
wait(NULL); // 父进程等待子进程结束
}
return 0;
}
[sanshi@iZ2ze4xrxurbpglmuv68wlZ ~]$ vim os1.c
[sanshi@iZ2ze4xrxurbpglmuv68wlZ ~]$ gcc os1.c -o os1
[sanshi@iZ2ze4xrxurbpglmuv68wlZ ~]$ ./os1
I am the parent process with PID 18702
I am the child process with PID 18703
同vfork()
函数的区别
fork()
创建的子进程会直接拷贝父进程的数据段,且父子进程的执行次序不确定。vfork()
创建的子进程与父进程共享数据段,且子进程先运行,父进程后运行。
wait
wait()
函数的原型如下:
pid_t wait(int *status);
wait()函数 的使用
当 status
参数为 NULL
时,wait()
函数将等待任何子进程结束执行,而不会提供子进程的退出状态信息。这意味着父进程不关心子进程是如何结束的,只是简单地等待它们中的任何一个完成。一旦有子进程结束,wait()
函数就会返回该子进程的 PID
。如果此时有多个子进程都已经结束,wait()
会返回第一个结束的子进程的 PID。
无名管道
#include <unistd.h>
int pipe(int pipefd[2]);
线程相关
创建线程
pthread_create()
库函数用于创建一个线程,线程创建成功时返回0,创建失败时返回出错编号。
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
报错os2.c:(.text+0x7c): undefined reference to 'pthread_create'
要解决这个问题,你需要确保在编译程序时链接了线程库。在大多数 UNIX 和类 UNIX 系统(包括 Linux 和 macOS)上,你可以通过在编译命令中添加
-lpthread
选项来实现这一点。例如,如果你的编译命令是:
gcc os2.c -o os2
你应该修改为:
gcc os2.c -o os2 -lpthread
这里
-lpthread
选项告诉编译器去链接libpthread
库,这样pthread_create
函数和其他 POSIX 线程相关的函数才能被正确地解析。
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void *thread_fun(void *arg) {
printf("My pid is %d, my tid is %lu.\n", getpid(), *((unsigned long int *)arg));
return NULL;
}
int main() {
pthread_t tid;
printf("My pid is %d, my tid is %lu.\n", getpid(), pthread_self()); // pthread_self()用于获取当前线程的线程id
if (pthread_create(&tid, NULL, thread_fun, (void *)(&tid)) != 0) {
perror("pthread_create error");
exit(1);
}
sleep(1);
return 0;
}
My pid is 19287, my tid is 140079949600576.
My pid is 19287, my tid is 140079941207808.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 // 线程的入口函数
7 void* thread_function(void *arg) {
8 // 执行线程任务的代码
9 printf("Hello from the thread!\n");
10 printf("My pid is %d, my tid is %lu.\n", getpid(), *((unsigned long int *)arg));
11 return NULL;
12 }
13
14 int main() {
15 pthread_t thread_id;
16
17 // 创建一个新线程
18 if (pthread_create(&thread_id, NULL,thread_function, (void *)(&thread_id)) != 0) {
19 perror("Failed to create the thread");
20 return EXIT_FAILURE;
21 }
22 sleep(1);
23 printf("Thread has finished execution.\n");
24
25 return 0;
26 }
Hello from the thread!
My pid is 20569, my tid is 140591760320256.
Thread has finished execution.
-
创建线程后,我们调用
pthread_join()
函数来等待线程结束。这个函数确保主线程(创建线程的线程)在继续执行之前等待指定的线程完成其执行。 -
sleep(1)
可以实现同样的效果。