关于线程

1.linux查看修改线程默认栈空间大小 ulimit -s

a、通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M

b、通过命令 ulimit -s 设置大小值 临时改变栈空间大小:ulimit -s 102400, 即修改为100M

c、可以在/etc/rc.local 内 加入 ulimit -s 102400 则可以开机就设置栈空间大小

d、在/etc/security/limits.conf 中也可以改变栈空间大小:

#<domain> <type> <item> <value>

* soft stack 102400

重新登录,执行ulimit -s 即可看到改为102400 即100M

 

2.为啥csh没有命令ulimit。
freebsd的默认shell csh的对应内部命令是limit不是ulimit,freebsd还有一个外部命令limits,freebsd中需要改变这些资源使用限制只能用外部命令,内部命令改不了,如果你一定要用ulimit的话,就只能安装有ulimit内部命令的shell,比如bash,sh

3.ulimit -s限定了用户应用启动默认的栈控制大小(仅针对应用主线程;子线程创建时不会像主线程一样从一个相对固定地址(0X7fffff...)开始,而是需要从堆中切割一块内存,所以必须指定大小,否则无法获知要给他分配多大的内存,它的栈内存有主线程创建时可以指定)。

4.为啥linux要限制用户进程的栈内存大小。

Why does Linux have a default stack size soft limit of 8 MB?

The point is to protect the OS.

Programs that have a legitimate reason to need more stack are rare. On the other hand, programmer mistakes are common, and sometimes said mistakes lead to code that gets stuck in an infinite loop. And if that infinite loop happens to contain a recursive function call, the stack would quickly eat all the available memory. The soft limit on the stack size prevents this: the program will crash but the rest of the OS will be unaffected.

Note that as this is only a soft limit, you can actually modify it from within your program (see setrlimit(2): get/set resource limits) if you really need to.

5.ulimit -s ulimited是啥意思.
ulimited就是不限制
因为主线程内存其实地址固定(0X7fffff...),实际上也就可以看到,主线程地址根本无需限定栈空间大小,它甚至可以将整块的虚拟内存作为它的栈。

6.cshell中通过unlimit可以将ulimit一些限制取消。

### 回答1: 下面是一个关于创建线程的 Python 代码示例: ``` import threading class MyThread(threading.Thread): def __init__(self, thread_id, name, counter): threading.Thread.__init__(self) self.thread_id = thread_id self.name = name self.counter = counter def run(self): print("开始线程:" + self.name) print_time(self.name, self.counter, 5) print("退出线程:" + self.name) def print_time(thread_name, delay, counter): while counter: time.sleep(delay) print("%s: %s" % (thread_name, time.ctime(time.time()))) counter -= 1 thread1 = MyThread(1, "Thread-1", 1) thread2 = MyThread(2, "Thread-2", 2) thread1.start() thread2.start() thread1.join() thread2.join() print("退出线程") ``` 在这段代码中,我们定义了一个 `MyThread` 类,继承自 Python 中的 `threading.Thread` 类。在这个类中,我们重写了 `run` 方法,来执行线程的任务。接着,我们创建了两个线程对象,并启动它们。最后,使用 `join` 方法等待两个线程执行完毕。 ### 回答2: 在Python中,可以使用`threading`模块来创建和管理线程。下面是一个简单的示例代码,使用Python的类来实现线程: ```python import threading import time class MyThread(threading.Thread): def __init__(self, name): threading.Thread.__init__(self) self.name = name def run(self): print(f"线程 {self.name} 启动") # 执行线程任务 for i in range(5): print(f"线程 {self.name}: 执行任务 {i}") time.sleep(1) print(f"线程 {self.name} 结束") # 创建两个线程对象 thread1 = MyThread("线程1") thread2 = MyThread("线程2") # 启动线程 thread1.start() thread2.start() # 等待线程完成 thread1.join() thread2.join() print("线程结束") ``` 这个示例代码定义了一个`MyThread`类,继承自`threading.Thread`类,重写了`run`方法,该方法是线程的入口点。在`run`方法中,我们定义了线程要执行的任务。 然后,我们创建了两个`MyThread`对象,分别命名为"线程1"和"线程2"。接着,我们分别启动这两个线程,并使用`join`方法等待线程执行完毕。 最后,线程打印出 "线程结束",表示线程的执行也结束了。 这段代码演示了如何使用Python的类来创建和管理线程。每个线程执行自己的任务,而不会干扰其他线程的执行。使用类可以方便地管理线程,并提供更好的代码组织和可维护性。 ### 回答3: 下面是一个使用Python类编写的关于线程的代码示例: ```python import threading import time # 创建一个继承自Thread类的自定义线程类 class MyThread(threading.Thread): def __init__(self, thread_id, name): threading.Thread.__init__(self) self.thread_id = thread_id self.name = name # 重写run方法,线程启动后执行该方法 def run(self): print("线程 " + self.name + " 正在运行...") time.sleep(2) # 线程休眠2秒 print("线程 " + self.name + " 运行结束.") # 创建线程对象 thread1 = MyThread(1, "Thread 1") thread2 = MyThread(2, "Thread 2") # 启动线程 thread1.start() thread2.start() # 等待线程执行完毕 thread1.join() thread2.join() print("所有线程执行完毕.") ``` 以上代码定义了一个自定义的线程类`MyThread`,该类继承自`Thread`类,并重写了`run`方法,用于线程的实际操作。在`run`方法中,我们模拟了线程运行2秒的操作。然后,创建了两个线程对象`thread1`和`thread2`,并通过调用`start`方法启动它们。最后,调用`join`方法等待线程执行完毕,并输出"所有线程执行完毕"的提示。 这段代码展示了如何使用Python的类编写多线程程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值