在Linux下面,创建进程是一件十分有意思的事情。我们都知道,进程是操作系统下面享有资源的基本单位。那么,在linux下面应该怎么创建进程呢?其实非常简单,一个fork函数就可以搞定了。但是,我们需要清楚的是子进程与父进程之间除了代码是共享的之外,堆栈数据和全局数据均是独立的,主要是学习标准的H文件的包含。
linux下的C语言开发(创建进程)
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main()
- {
- pid_t pid;
- if(-1 == (pid = fork()))
- {
- printf("Error happened in fork function! ");
- return 0;
- }
- if(0 == pid)
- {
- printf("This is child process: %d ", getpid());
- }
- else
- {
- printf("This is parent process: %d ", getpid());
- }
- return 0;
- }
多线程和多进程还是有很多区别的。其中之一就是,多进程是Linux内核本身所支持的,而多线程则需要相应的动态库进行支持。对于进程而言,数据之间都是相互隔离的,而多线程则不同,不同的线程除了堆栈空间之外所有的数据都是共享的。说了这么多,我们还是自己编写一个多线程程序看看结果究竟是怎么样的。
- #include <stdio.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- void func_1(void* args)
- {
- while(1){
- sleep(1);
- printf("this is func_1! ");
- }
- }
- void func_2(void* args)
- {
- while(1){
- sleep(2);
- printf("this is func_2! ");
- }
- }
- int main()
- {
- pthread_t pid1, pid2;
- if(pthread_create(&pid1, NULL, func_1, NULL))
- {
- return -1;
- }
- if(pthread_create(&pid2, NULL, func_2, NULL))
- {
- return -1;
- }
- while(1){
- sleep(3);
- }
- return 0;
- }
和我们以前编写的程序有所不同,多线程代码需要这样编译,输入gcc thread.c -o thread -lpthread,编译之后你就可以看到thread可执行文件,输入./thread即可。
- [test@localhost Desktop]$ ./thread
- this is func_1!
- this is func_2!
- this is func_1!
- this is func_1!
- this is func_2!
- this is func_1!
- this is func_1!
- this is func_2!
- this is func_1!
- this is func_1!
=================================================================
linux下的C语言开发(管道通信)
Linux系统本身为进程间通信提供了很多的方式,比如说管道、共享内存、socket通信等。管道的使用十分简单,在创建了匿名管道之后,我们只需要从一个管道发送数据,再从另外一个管道接受数据即可。
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- int pipe_default[2];
- int main()
- {
- pid_t pid;
- char buffer[32];
- memset(buffer, 0, 32);
- if(pipe(pipe_default) < 0)
- {
- printf("Failed to create pipe! ");
- return 0;
- }
- if(0 == (pid = fork()))
- {
- close(pipe_default[1]);
- sleep(5);
- if(read(pipe_default[0], buffer, 32) > 0)
- {
- printf("Receive data from server, %s! ", buffer);
- }
- close(pipe_default[0]);
- }
- else
- {
- close(pipe_default[0]);
- if(-1 != write(pipe_default[1], "hello", strlen("hello")))
- {
- printf("Send data to client, hello! ");
- }
- close(pipe_default[1]);
- waitpid(pid, NULL, 0);
- }
- return 1;
- }
- [test@localhost pipe]$ ./pipe
- Send data to client, hello!
- Receive data from server, hello!
=====================================================================
信号处理是Linux程序的一个特色。用信号处理来模拟操作系统的中断功能,对于我们这些系统程序员来说是最好的一个选择了。要想使用信号处理功能,你要做的就是填写一个信号处理函数即可。一旦进程有待处理的信号处理,那么进程就会立即进行处理。
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- int value = 0;
- void func(int sig)
- {
- printf("I get a signal! ");
- value = 1;
- }
- int main()
- {
- signal(SIGINT, func);
- while(0 == value)
- sleep(1);
- return 0;
- }
为了显示linux对signal的处理流程,我们需要进行两个步骤。第一,输入gcc sig.c -o sig, 然后输入./sig即可;第二则重启一个console窗口,输入ps -aux | grep sig, 在获取sig的pid之后然后输入kill -INT 2082, 我们即可得到如下的输出。
- [root@localhost fork]#./sig
- I get a signal!
- [root@localhost fork]#
===========================================================