linux C创建进程

1、原生 linux 下,如何用C创建进程

【正确使用pthread_create,防止内存泄漏_whuzm08的专栏-CSDN博客_pthread_create 内存泄漏】
https://blog.csdn.net/whuzm08/article/details/53537602

#include <unistd.h>
void thread_1(){
    while(1)
    {
        printf("in thread_1...\n");
    }
}
void thread_2(void *args){
    while(1)
    {
        printf("in thread_2...\n");
    }
}

int main()
{
    pthread_t tid_1;
    pthread_t tid_2;
    char *pArg = (char *)malloc(1024);
    if ((pthread_create(&tid_1, NULL, thread_1, NULL)) == -1)
    {
        printf("thread_1 create failed!\n");
        return 1;
    }
   /*第二个进程是带有入参的*/
   if ((pthread_create(&tid_2, NULL, thread_2, pArg)) == -1)
    {
        printf("thread_2 create failed!\n");
        return 1;
    }
    if(pthread_join(&tid_1, NULL)){
        printf("error join thread_1\n);
    }
    if(pthread_join(&tid_2, NULL)){
        printf("error join thread_2\n);
    }
    /*最后,主进程也要进入休眠状态才行,否则立即退出*/
    while(1)
    {
        sleep(1);
    }
    return 0;
}

发表评论