守护进程创建、linux线程

 守护进程定义:

        周期性的执行某项任务或等待某个事件发生的进程,他的运行不依赖shell终端,他的生存周期较长,从开机开始运行直到关机结束。

守护进程的创建步骤:

        1.创建一个子进程,让父进程退出

                fork

        2.创建新的会话期(在子进程) 

                setsid();

        3.改变子进程的工作目录

                chdir("/")

        4.取消文件权限掩码

                umask(0)

        5.关闭所有文件描述符

                getdtablesize

                opendir readdir chdir

线程相关

        定义:线程是一种轻量级的进程用task_struct来标识它,它没有自己的独立内存空间,它共 享创建它的进程的地址空间。

        线程创建:

                使用第三方线程库提供的API

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);
        作用:创建一个线程
         *thread:指向线程ID的指针 
        *attr:线程属性,通常给NULL
        参数三:指向线程执行函数的函数指针
        *arg:传给线程执行函数的入参
        返回值:0 成功;非0 失败
    int pthread_join(pthread_t thread, void **retval);
        作用:阻塞等待线程结束,并回收状态值
         thread:线程ID
         **retval:指向线程退出状态值的指针
    void pthread_exit(void *retval);
        作用:线程退出函数
        *retval:线程退出状态值
     int pthread_cancel(pthread_t thread);
        作用:取消线程
         thread:线程ID
      unubtu=Linux内核+各种工具+桌面管理器+各种库+各种应用程序
  多线程的通信 同步 互斥
     在一个进程中可以创建多个线程,那么多个线程间会存在数据通信问题,
    会存在两个线程相互配合完成一件事情,会存在两个线程对公共资源的竞态访问问题
    多线程的数据通信-->使用全局变量
        ./pthead_com1  hello
    多线程的同步-->无名信号量  sem_t sem;
        同步:两个线程按照一定的顺序相互配合完成一件事情
         pthread1-->hello   1S   sem_t  sema;-->P/V操作函数控制sema和semb
         pthread2-->world  1s   sem_t semb;
        P操作:sem_wait(&sema)
            含义:sem_wait会检测信号量sema的值是否大于0,若大于0,将sema减一
                    同时函数返回。若等于0,阻塞当前线程。
        V操作:sem_post(&sema)--》非阻塞函数
            含义:sem_post只会给检测的信号量sema加1        

多线程的互斥

互斥的引入:多个线程可能会使用同一段公共资源,若是同时访问这段公共资源,就会造成多个线程对公共资源的竞争,从而造成访问结果混乱。

如何解决:可以使用互斥锁。

(1)多个线程能否使用同一个线程执行函数

#include<stdio.h>
#include<pthread.h>

void *ThreadFunc(void *arg)
{
    printf("into thread func---------\r\n");
}
int main()
{
    pthread_t tID1 = -1;
    pthread_t tID2 = -1;
    if(0 != pthread_create(&tID1,NULL,ThreadFunc,NULL))
    {
        return -1;
    }
    if(0 != pthread_create(&tID2,NULL,ThreadFunc,NULL))
    {
        return -1;
    }
    pthread_join(tID1,NULL);
    pthread_join(tID2,NULL);
}

(2)两个线程给线程执行函数传入不同的参数,最后执行结果是否发生混乱?

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

#define BUF_SIZE 11

void *ThreadFunc(void *arg)
{
    int i = 0;
    printf("into thread func---------\r\n");
    if(NULL == arg)
    {
        return (void *)NULLL;
    }
    char *pArr = (char *)arg;
    static char buf[BUF_SIZE] = {0};
    for(;i < 10; i++)
    {
        buf[i] = pArr[i];
        usleep(5000);
    }
    printf("buf:%s\r\n",buf);
}
int main()
{
    pthread_t tID1 = -1;
    pthread_t tID2 = -1;
    char arr1[] = "123456789";
    char arr2[] = "abcdefghi";
    if(0 != pthread_create(&tID1,NULL,ThreadFunc,(void *)arr1))
    {
        return -1;
    }
    if(0 != pthread_create(&tID2,NULL,ThreadFunc,(void *)arr2))
    {
        return -1;
    }
    pthread_join(tID1,NULL);
    pthread_join(tID2,NULL);
}

(3)使用互斥锁解决两线程互斥访问公共资源

pthread_mutex_t mutex;//线程互斥锁

pthread_mutex_init(&mutex,NULL)

pthread_mutex_lock(mutex);

pthread_mutex_unlock(mutex);

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>

#define BUF_SIZE 11
pthread_mutex_t mutex;

void *ThreadFunc(void *arg)
{
    int i = 0;
    printf("into thread func---------\r\n");
    if(NULL == arg)
    {
        return (void *)NULLL;
    }
    pthread_mutex_lock(&mutex);
    char *pArr = (char *)arg;
    static char buf[BUF_SIZE] = {0};
    for(;i < 10; i++)
    {
        buf[i] = pArr[i];
        usleep(5000);
    }
    printf("buf:%s\r\n",buf);
    pthread_mutex_unlock(&mutex);
}
int main()
{
    pthread_t tID1 = -1;
    pthread_t tID2 = -1;
    char arr1[] = "123456789";
    char arr2[] = "abcdefghi";
    if(-1 == pthread_mutex_init(&mutex,NULL))
    {
        return -1;
    }
    if(0 != pthread_create(&tID1,NULL,ThreadFunc,(void *)arr1))
    {
        return -1;
    }
    if(0 != pthread_create(&tID2,NULL,ThreadFunc,(void *)arr2))
    {
        return -1;
    }
    pthread_join(tID1,NULL);
    pthread_join(tID2,NULL);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值