linux c 多进程多线程比较

来源:互联网 发布:无花 崔鹏 知乎 编辑:程序博客网 时间:2024/06/08 13:51

多进程

#include <unistd.h>#include <sys/wait.h>int main(){        pid_t pid;        int result;        pid = fork();        if(pid <0)        {        }        else if (pid == 0)        {//子进程                char my_cmd3[]="./gh www.baidu.com > lll";                 system(my_cmd3);        }        else        {//父进程        }}
编译 

gcc -o testj testj.c

多线程

#include <stdio.h>#include <pthread.h>void thread(void){int i; char my_cmd3[]="./gh www.baidu.com >> lll";                 system(my_cmd3);}int main(void){pthread_t id;int i,ret,j;for(j=0;j<3;j++){ret=pthread_create(&id,NULL,(void *) thread,NULL);if(ret!=0){printf ("Create pthread error!\n");return 1;}pthread_join(id,NULL);} printf("This is the main process.\n");return 0;}