首页 > 系统相关 >父进程有多个线程,fork()只会复制一个线程

父进程有多个线程,fork()只会复制一个线程

时间:2022-09-22 16:15:49浏览次数:47  
标签:fork thread int lock 复制 线程 pthread NULL my

 

fork()前的父进程有多个线程,子进程只复制fork所在线程为主进程,其他线程不复制

#include <pthread.h>
#include <sys/prctl.h>

pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER;

static void *my_thread1(void *args)
{
    prctl(PR_SET_NAME, "thread_1");
    sleep(3*60);

    return NULL;
}

static void *my_thread2(void *args)
{
    prctl(PR_SET_NAME, "thread_2");
    sleep(3*60);

    return NULL;
}

int main(int argc, char * argv[])
{

    int count = 0;
    int ret = 0;
    int rc= 0;

    pthread_t thread_fd_1;
    pthread_t thread_fd_2;

    prctl(PR_SET_NAME, "thread_main");

    rc |= pthread_create(&thread_fd_1, NULL, my_thread1, NULL);
    rc |= pthread_create(&thread_fd_2, NULL, my_thread2, NULL);


    pthread_mutex_lock(&my_lock);
    count = 0;

    ret = fork();
    if (0 == ret) {
        
        printf("child will lock\r\n");
        pthread_mutex_lock(&my_lock);
        printf("child lock OK\r\n");

        count = 1;

        printf("child will unlock\r\n");
        pthread_mutex_unlock(&my_lock);
        printf("child unlock OK\r\n");
sleep(3*60);

    } else {
        printf("father will sleep(3*60)\r\n");
sleep(3*60);
    }

    return 0;
}

 

 

 

#include <pthread.h>
#include <sys/prctl.h>

pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER;
static int count = 0;

static void *my_thread1(void *args)
{
    prctl(PR_SET_NAME, "thread_1");
    sleep(3*60);

    return NULL;
}

static void *my_thread2(void *args)
{
    int ret = 0;

    prctl(PR_SET_NAME, "thread_2");
    
    ret = fork();
    if (0 == ret) {
        
        printf("child will lock\r\n");
        pthread_mutex_lock(&my_lock);
        printf("child lock OK\r\n");

        count = 1;

        printf("child will unlock\r\n");
        pthread_mutex_unlock(&my_lock);
        printf("child unlock OK\r\n");
sleep(3*60);

    } else {
        printf("father will sleep(3*60)\r\n");
sleep(3*60);
    }
    return NULL;
}

int main(int argc, char * argv[])
{

    int ret = 0;
    int rc= 0;

    pthread_t thread_fd_1;
    pthread_t thread_fd_2;

    prctl(PR_SET_NAME, "thread_main");

    rc |= pthread_create(&thread_fd_1, NULL, my_thread1, NULL);
    rc |= pthread_create(&thread_fd_2, NULL, my_thread2, NULL);


    pthread_mutex_lock(&my_lock);
    count = 0;

sleep(3*60);

    return 0;
}

 

标签:fork,thread,int,lock,复制,线程,pthread,NULL,my
From: https://www.cnblogs.com/LiuYanYGZ/p/16719638.html

相关文章

  • NodePad++一次复制多行粘贴到对应位置
    有时候要写sql,但是里面有很多字段要对应上,如果要自己一个字段一个字段的去写是在有点麻烦,是不是有更好的方法做到这件事呢?要做这件事,首先分析下我们已有数据的情况,每个字......
  • 多线程创建connection连接hbase
    publicclassGetconnection{//声明一个静态属性publicstaticConnectionconnection=null;static{//创建连接try{......
  • 多线程
    进程每个应用程序在运行期间,操作系统为应用程序分配一个独立的内存空间,称为进程;多个进程之间的数据是相互隔离的;windows查看后台进程命令tasklistlinux查看后台进程......
  • Java中使用Hutool的ExecutorBuilder实现自定义线程池
    场景Java中ExecutorService线程池的使用(Runnable和Callable多线程实现):https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/126242904SpringBoot+Lombok+Bui......
  • 【Linux】多线程中fork与互斥锁---子进程会复制继承父进程里锁的状态
    摘自:https://blog.csdn.net/xiaoxiaoguailou/article/details/121617142问题提出:我们有这样一个问题:在一个多线程程序中创建子进程并且让子线程和子进程去获取一把全局变......
  • jmeter 登录跨线程组解决方案
    使用python批量插入用户数据,手机号随机生成,密码统一插入123456的加密字符importpymysqldb=pymysql.connect(host="124.70.xxx.xxx",user="root",pa......
  • jmeter使用Ultimate Thread Group线程组
    一、UltimateThreadGroup字段解释   解释:StartThreadsCount:启动多少线程InitialDelay,sec:延迟多少秒开始启动线程StartupTime,sec:启用{StartThreadsCount......
  • javascript: 复制数组时的深拷贝及浅拷贝(chrome 105.0.5195.125)
    一,js代码:<html><head><metacharset="utf-8"/><title>测试</title></head><body><buttononclick="assignCopy()">无效:变量直接赋值</button><br/><br......
  • JAVA多线程-学习笔记
    1.1概述程序:程序是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念。进程(Porcess):是执行程序的一次执行过程,是一个动态的概念,是系统资源分配的单位。线......
  • 多线程
    一.Java构建线程的方式继承Thread实现Runnable实现Callable线程池方式推荐手动创建线程池二.线程池的7个参数publicThreadPoolExecutor(intcorePoolSiz......