僵尸进程的避免 守护进程的创建 线程的创建,阻塞,数据传递 5.15

这篇具有很好参考价值的文章主要介绍了僵尸进程的避免 守护进程的创建 线程的创建,阻塞,数据传递 5.15。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

父子进程相关知识:

1.子进程结束时,系统 会立即自动刷新行缓存

2.手动结束进程:

exit()

exit(int status):结束当前调用的进程,自动刷新缓存

标准库函数

头文件:#include <stdlib.h>

_exit()

_exit(int status) : 结束当前调用的进程,不刷新缓存

系统调用函数

头文件:#include <unistd.h>

3.僵尸进程

子进程先于父进程结束,父进程没有回收子进程的剩余资源

4.如何避免僵尸进程?

1、子进程结束,通知父进程回收资源,使用wait或者waitpid函数

wait()

​ pid_t wait(int *status)

功能:阻塞父进程,等待任一子进程结束,回收该子进程资源

在父进程中调用

如果,该进程没有子进程或者子进程已经结束,则函数立即返回

头文件:

​ #include <sys/types.h>

​ #include <sys/wait.h>

参数:

​ status : 整型指针,用来保存子进程退出时的状态

​ 如果status设置为NULL,不保存子进程退出时的状态

​ 必须通过Linux的宏来检测 退出状态

返回值:

​ 成功:退出的子进程的pid

​ 失败:-1,并设置错误信息

5.fork创建子进程时:

if结构 将会决定 子进程拷贝父进程的 哪些内容:

if()

{//出错}

else if()

{//子进程}

else

{//父进程}

注意:else中的父进程区域内容不会被拷贝到子进程中

if()

{//出错}

else if()

{//子进程}

//父进程区域(没有else)

注意:父进程的内容,将会被子进程 拷贝所有内容(父进程号除外)

waitpid()

​ pid_t waitpid(pid_t pid,int *status,int options)

waitpid(-1,NULL,0) == wait(NULL)

功能:

​ 回收指定 pid的子进程 资源

参数:

​ pid:要回收的子进程的pid号;

​ status:保存指定pid的子进程退出的状态

​ option:WNOHANG:非阻塞模式;子进程未结束,立即返回0值;当指定的子进程PID结束立即回收资源

​ 0:阻塞模式,等同于wait

返回值:

​ 成功:返回退出的子进程PID号;

​ 失败:返回-1;

5.孤儿进程

父进程先于子进程结束,子进程被init.d托管,init进程的PID号为1

僵尸进程的避免 守护进程的创建 线程的创建,阻塞,数据传递 5.15

缺点:

​ 孤儿进程无法受当前终端控制。除非使用kill命令杀死或者系统结束,否则一直存在。

应用:用来制作守护进程

6.创建守护进程

ps -ef 全部进程

ps -axj 查看守护进程

ps -aux 全部进程

1、先创建子进程,父进程结束,形成孤儿进程

2、创建新的会话期,使进程完全独立,摆脱当前进程的控制

setsid函数用于创建一个新的会话期,kill -9杀不死

3、改变进程的工作目录为根目录:chdir

“/”或者“/tmp”作为守护

4、重设文件权限掩码:umask(0)

目的:增加守护进程的灵活性

5、关闭其他不相关的文件描述符。

运行之后,可以用kill -9 强制杀死守护进程,因为kill -9是在内核层面上杀死,而守护进程开辟是在用户层面的进程可以被其他杀死。

hqyj@ubuntu:~/5.15$ cat guer1_demo.c 
/*===============================================
*   文件名称:fork_demo.c
*   创 建 者:memories 
*   创建日期:2023年05月15日
*   描    述:
================================================*/
    
    创建守护进程
    
    
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>

void process();

int main(int argc, char *argv[])
{ 
    pid_t pid=fork();
    if(pid==-1)
    {
        perror("fork");
        return -1;
    }
    else if(pid==0)
    {
        //2.创建新的会话期
        if(setsid()<0)
        {
            perror("setsid");
            return -1;
        }
        //3.设置进程的工作目录为"/tmp"
        if(chdir("../5.15")<0)
        {
            perror("chidr");
            return -1;
        }
        //4.重设文件权限掩码
        umask(0);

        //5.关闭不相关的文件描述符(文件描述符来自于父进程)
        //getdtablesize():返回当前的文件描述符个数
        for(int i=0;i<getdtablesize();i++)
        {
            close(i);
        }
        //调用接口函数,实现记录功能,每隔一秒钟向文件下入记数
        process();
    }
    else
    {
        //1.父进程推出 孤儿进程创建
        exit(0);
    }
    return 0;
} 


void process()
{
    int fd = open("file1.txt",O_RDWR|O_CREAT,0640);
    int count = 0;

    while(1)
    {
        char buf[5]={0};
        sprintf(buf,"%d ",count++);
        write(fd,buf,strlen(buf));
        sleep(1);
    }

}
hqyj@ubuntu:~/5.15$ cat file1.txt
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 
hqyj@ubuntu:~/5.15$ cat file1.txt 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 

线程

  1. 每个进程的地址空间都是私有的,都拥有自己的task_struct结构体,每个task_struct都会指向自己映射的memory map虚拟地址映射表

  2. 当进程间进行任务切换时,都需要访问不同的task_struct结构体,因此需要不断刷新cache高速缓存器和TLB页表,导致系统开销大。因此,引入线程的概念,减小任务切换时系统开销较大的问题,提升系统性能

  3. 线程是轻量级的进程。

    同一个进程中开启的多个线程,共享同一个进程地址空间

    多个线程共同使用一个task_struct结构体

  4. 使用线程的好处

								1、提高了线程间任务切换的效率

								2、避免了额外的TLB和cache缓存的刷新
  1. posix线程库操作函数接口:

pthread

1、int pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *( *routine)(void *),void *arg);

功能:

创建新的子线程,指定线程的属性,指定线程的执行函数

头文件:

​ #include <pthread.h>

参数:

​ tid:保存创建成功的线程ID号

​ attr:指定线程的属性,默认为NULL缺省属性

​ routine:指针函数指针,指向线程的执行函数(void *函数名(void *);指针函数)

​ arg:传递到线程执行函数的参数

返回值:

​ 成功:0

​ 失败:posix库中的错误码

***注意:程序编译时必须指定链接的动态库 -lpthread

gcc *.c -lpthread;

/*===============================================
*   文件名称:pthread_demo.c
*   创 建 者:memories 
*   创建日期:2023年05月15日
*   描    述:
================================================*/

创建线程12,打印相应的内容
并不关闭线程


#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //第三方线程库的头文件

void *thread1(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程的代码区域
    while(1)//阻止子线程的结束
    {
        printf("child thread1\n");
        sleep(1);
    }
}
void *thread2(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程的代码区域
    while(1)//阻止子线程的结束
    {
        printf("child thread2\n");
        sleep(1);
    }
}
int main(int argc, char *argv[])
{ 
    //创建线程:pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(* routine)(void *),void *arg);
    pthread_t tid;
    if(pthread_create(&tid,NULL,thread1,NULL) <0 )
    {
        printf("create thread1 filed\n");
        return -1;
    }
    if(pthread_create(&tid,NULL,thread2,NULL) <0 )
    {
        printf("create thread2 filed\n");
        return -1;
    }
    while(1)
    {
        printf("main process\n");
        sleep(1);
    }
        return 0;
} 

hqyj@ubuntu:~/5.15$ gcc pthread_demo.c -lpthread
hqyj@ubuntu:~/5.15$ ./a.out 
main process
child thread1
child thread2
main process
child thread1
child thread2
main process
child thread1
child thread2
main process
child thread1
child thread2
^C

pthread_exit(void *str);

2、void pthread_exit(void *str);

功能:

​ 手动退出当前线程,传递数据。

参数:

​ str:要传递的数据的首地址

返回值:

​ 无

/*===============================================
*   文件名称:pthread_demo.c
*   创 建 者:memories 
*   创建日期:2023年05月15日
*   描    述:
================================================*/

1.创建子线程12
2.阻塞等待,指定的子线程12退出,回收资源子线程中的资源,
    其中的资源thread 1 exit,thread 2 exit


#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //第三方线程库的头文件

void *thread1(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程的代码区域
        printf("child thread1\n");
        sleep(2);
        pthread_exit("thread 1 exit");
}
void *thread2(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程的代码区域
        printf("child thread2\n");
        sleep(5);
        pthread_exit("thread 2 exit");
}
int main(int argc, char *argv[])
{ 
    //创建线程:pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(* routine)(void *),void *arg);
    pthread_t tid,tid2;
    if(pthread_create(&tid,NULL,thread1,NULL) <0 )
    {
        printf("create thread1 filed\n");
        return -1;
    }
    if(pthread_create(&tid2,NULL,thread2,NULL) <0 )
    {
        printf("create thread2 filed\n");
        return -1;
    }

    char *res;
        pthread_join(tid,(void **)&res);//阻塞等待,指定的子线程退出,回收资源
        printf("main process=%s\n",res);
        pthread_join(tid2,(void **)&res);
        printf("main process=%s\n",res);
        while(1);

        return 0;
} 

hqyj@ubuntu:~/5.15$ gcc pthread_exit.c -lpthread 
hqyj@ubuntu:~/5.15$ ./a.out 
child thread2
child thread1
main process=thread 1 exit
main process=thread 2 exit
^c

/*===============================================
*   文件名称:pthread_demo.c
*   创 建 者:memories 
*   创建日期:2023年05月15日
*   描    述:
================================================*/


1.创建子线程12
2.阻塞等待,指定的子线程12退出,回收资源子线程中的资源,
    其中的资源thread 1 exit,thread 2 exit

#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //第三方线程库的头文件

void *thread1(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程的代码区域
        printf("child thread1\n");
        sleep(2);
        pthread_exit("thread 1 exit");
}
void *thread2(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程的代码区域
        printf("child thread2\n");
        sleep(5);
        pthread_exit("thread 2 exit");
}
int main(int argc, char *argv[])
{ 
    //创建线程:pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(* routine)(void *),void *arg);
    pthread_t tid,tid2;
    if(pthread_create(&tid,NULL,thread1,NULL) <0 )
    {
        printf("create thread1 filed\n");
        return -1;
    }
    if(pthread_create(&tid2,NULL,thread2,NULL) <0 )
    {
        printf("create thread2 filed\n");
        return -1;
    }
        pthread_detach(tid);//非阻塞等待
        pthread_detach(tid2);
        printf("main-------\n");
        while(1);
        return 0;
} 

qyj@ubuntu:~/5.15$ gcc pthread_exit.c -lpthread 
hqyj@ubuntu:~/5.15$ ./a.out 
main-------
child thread2
child thread1
^c

pthread_join(pthread_t tid,void **str);

3、 int pthread_join(pthread_t tid,void **str);

功能:阻塞主进程,等待指定线程的退出,回收资源,并保存子线程退出时传递的数据

参数:

​ tid:线程ID

​ str:二级指针,用来保存线程退出时传递的数据

返回值:

​ 成功:0

​ 失败:线程库的错误码文章来源地址https://www.toymoban.com/news/detail-447848.html

/*===============================================
 *   文件名称:pthread_demo.c
 *   创 建 者:memories 
 *   创建日期:2023年05月15日
 *   描    述:
 ================================================*/

1.创建线程12
2.等待线程1关闭之后打印pthread1 quit
3.等待线程2关闭之后打印main process

#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //第三方线程库的头文件

void *thread1(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程1的代码区域
    printf("child thread1\n");
    sleep(3);
}
void *thread2(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //线程2的代码区域
    printf("child thread2\n");
    sleep(5);
}
int main(int argc, char *argv[])
{ 
    //创建线程:pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(* routine)(void *),void *arg);
    pthread_t tid,tid2;
    if(pthread_create(&tid,NULL,thread1,NULL) <0 )
    {
        printf("create thread1 filed\n");
        return -1;
    }
    if(pthread_create(&tid2,NULL,thread2,NULL) <0 )
    {
        printf("create thread2 filed\n");
        return -1;
    }
    pthread_join(tid,NULL);//阻塞等待 指定的子线程退出 回收资源
    printf("thread1 quit\n");
    pthread_join(tid2,NULL);
    printf("main process\n");
    return 0;
} 

hqyj@ubuntu:~/5.15$ gcc pthread1_demo.c -lpthread
hqyj@ubuntu:~/5.15$ ./a.out 
child thread1
child thread2
thread1 quit
main process

同一进程中的多个线程之间进行数据传递必须使用全局变量。

/*===============================================
*   文件名称:pthread_demo.c
*   创 建 者:memories 
*   创建日期:2023年05月15日
*   描    述:
================================================*/

1.定义一个全局变量a,将a在两个线程之间进行数据传递
2.也可以在进程中定义一个变量b,但只能以函数传递参数的方式,使得b在两个线程之间进行数据传递


#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //第三方线程库的头文件

int a=10;

void *thread1(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //int b=*(int *)arg;
    //线程的代码区域
    while(1)
    {
        printf("child thread1=%d,b=%d\n",a++,(*(int *)arg)++);
        sleep(1);
    }
}
void *thread2(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //int b=*(int *)arg;
    //线程的代码区域
    while(1)
    {
        printf("child thread2=%d,b=%d\n",a,*(int *)arg);
        sleep(1);
    }
}
int main(int argc, char *argv[])
{ 
    int b=10;
    //创建线程:pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(* routine)(void *),void *arg);
    pthread_t tid,tid2;
    if(pthread_create(&tid,NULL,thread1,(void *)&b) <0 )
    {
        printf("create thread1 filed\n");
        return -1;
    }
    if(pthread_create(&tid2,NULL,thread2,(void *)&b) <0 )
    {
        printf("create thread2 filed\n");
        return -1;
    }
    while(1);
        return 0;
} 

hqyj@ubuntu:~/5.15$ gcc pthread_data.c -lpthread
hqyj@ubuntu:~/5.15$ ./a.out 
child thread1=10,b=10
child thread2=11,b=11
child thread1=11,b=11
child thread2=12,b=12
child thread1=12,b=12
child thread2=13,b=13
child thread1=13,b=13
child thread2=14,b=14
child thread1=14,b=14
child thread2=15,b=15
^C

/*===============================================
*   文件名称:pthread_demo.c
*   创 建 者:memories 
*   创建日期:2023年05月15日
*   描    述:
================================================*/

a为全局变量则可以使得a现在线程12之间进行数据传递
    
但b这种不同于上种类型  当int b=*(int *)arg; 在线程2定义过一次之后,此时b在线程2为局部变量,无法进行线程间的数据传递


#include <stdio.h>
#include <unistd.h>
#include <pthread.h> //第三方线程库的头文件

int a=10;

void *thread1(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    //int b=*(int *)arg;
    //线程的代码区域
    while(1)
    {
        printf("child thread1=%d,b=%d\n",a++,(*(int *)arg)++);
        sleep(1);
    }
}
void *thread2(void *arg)//定义一个指针函数,用作一个线程的执行函数
{
    int b=*(int *)arg;
    //线程的代码区域
    while(1)
    {
        printf("child thread2=%d,b=%d\n",a,b);
        sleep(1);
    }
}
int main(int argc, char *argv[])
{ 
    int b=10;
    //创建线程:pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(* routine)(void *),void *arg);
    pthread_t tid,tid2;
    if(pthread_create(&tid,NULL,thread1,(void *)&b) <0 )
    {
        printf("create thread1 filed\n");
        return -1;
    }
    if(pthread_create(&tid2,NULL,thread2,(void *)&b) <0 )
    {
        printf("create thread2 filed\n");
        return -1;
    }
    while(1);
        return 0;
} 



hqyj@ubuntu:~/5.15$ gcc pthread_data.c -lpthread
hqyj@ubuntu:~/5.15$ ./a.out 
child thread1=10,b=10
child thread2=11,b=11
child thread1=11,b=11
child thread2=12,b=11
child thread1=12,b=12
child thread2=13,b=11
child thread1=13,b=13
child thread2=14,b=11
child thread1=14,b=14
child thread2=15,b=11
child thread1=15,b=15
child thread2=16,b=11
child thread1=16,b=16
child thread2=17,b=11
^C

到了这里,关于僵尸进程的避免 守护进程的创建 线程的创建,阻塞,数据传递 5.15的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • redis是单线程的,那么他是怎么样避免阻塞的

    Redis 实例在运行时,要和许多对象进行交互,这些不同的交互就会涉及不同的操作,下 面我们来看看和 Redis 实例交互的对象,以及交互时会发生的操作。 客户端 :网络 IO,键值对增删改查操作,数据库操作; 磁盘 :生成 RDB 快照,记录 AOF 日志,AOF 日志重写; 主从节点

    2024年02月13日
    浏览(28)
  • 进程控制相关 API-创建进程、进程分离、进程退出、进程阻塞

    目录 进程控制相关 API 父进程创建子进程 fork() 进程分离 exec 族函数 进程的退出 return/exit() 进程的阻塞 wait() 其它 API 进程控制相关 API p.s 进程控制中的状态转换 相关 API,用户很少用到,在此不提。 一般来说,这些内核标准 API,在执行出错(可能是资源不够、权限不够等等

    2024年02月10日
    浏览(30)
  • 利用线程池多线程并发实现TCP两端通信交互,并将服务端设为守护进程

    利用线程池多线程并发实现基于TCP通信的多个客户端与服务端之间的交互,客户端发送数据,服务端接收后处理数据并返回。服务端为守护进程 封装一个记录日志的类,将程序运行的信息保存到文件 封装线程类、服务端处理任务类以及将锁进行封装,为方便实现线程池 实现

    2024年02月14日
    浏览(29)
  • IO进程线程第五天(8.2)进程函数+XMind(守护进程(幽灵进程),输出一个时钟,终端输入quit时退出时钟)

    1.守护进程(幽灵进程) 2.输出一个时钟,终端输入quit时退出时钟        

    2024年02月14日
    浏览(35)
  • 【网络编程】详解UDP/TCP套接字的创建流程+守护进程

    需要云服务器等云产品来学习Linux的同学可以移步/--腾讯云--/--阿里云--/--华为云--/官网,轻量型云服务器低至112元/年,新用户首次下单享超低折扣。   目录 一、网络编程套接字 1、一些概念 1.1源IP地址和目的IP地址 1.2端口号port 1.3TCP和UDP的性质 1.4网络字节序、IP地址类型转换

    2024年02月05日
    浏览(45)
  • 线程的创建和参数传递

    进程有独立的地址空间; Linux为每个进程创建task_struct; 每个进程都参与内核调度,互不影响; 进程在切换时系统开销大; 很多操作系统引入了轻量级进程LWP; 同一进程中的线程共享相同地址空间; Linux不区分进程、线程。 1.特点 通常线程指的是共享相同地址 空间的多个

    2024年02月07日
    浏览(25)
  • Java - JUC(java.util.concurrent)包详解,其下的锁、安全集合类、线程池相关、线程创建相关和线程辅助类、阻塞队列

    JUC是java.util.concurrent包的简称,在Java5.0添加,目的就是为了更好的支持高并发任务。让开发者进行多线程编程时减少竞争条件和死锁的问题 java.lang.Thread.State tools(工具类):又叫信号量三组工具类,包含有 CountDownLatch(闭锁) 是一个同步辅助类,在完成一组正在其他线程中

    2024年02月05日
    浏览(29)
  • 什么是线程?线程和进程的关系?如何创建/查看线程?

    1.1.1 什么是线程 进程进一步细化为线程, 是程序内部的一条执行路径. 一个进程中至少有一个线程. 每个线程之间都可以按照顺讯执行自己的代码. 多个线程之间\\\"同时\\\"执行多份代码. 1.1.2 线程存在的意义 ① “并发编程\\\"成为\\\"刚需” 单核 CPU 的发展遇到了瓶颈. 要想提高算力, 就

    2024年02月15日
    浏览(29)
  • Verilog基础:避免混合使用阻塞和非阻塞赋值

    相关阅读 Verilog基础 https://blog.csdn.net/weixin_45791458/category_12263729.html?spm=1001.2014.3001.5482         “避免在一个always块中混杂阻塞赋值和非阻塞赋值”,这条原则是著名的Verilog专家Cliff Cummings在论文SUNG2000中提出的,这个观点在公众讨论时受到了广泛的质疑。有人认为可以在时钟

    2024年02月05日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包