openwrt

本文档通过多个实例展示了如何使用pthread_join函数来确保所有创建的线程正确退出,并总结了多线程编程中常见mutex锁的使用方法。此外,还提供了Mutex锁在实际编程中的应用案例,包括线程同步的实现。

1 openwrt开发环境配置
http://forgotfun.org/2018/05/openwrt-lesson-03.html

2 查看当前系统所使用的的线程库:
getconf GNU_LIBPTHREAD_VERSION

3 多线程
https://www.cnblogs.com/thinkinglife/p/5153723.html
pthread_cond_wait用于等待某个特定的条件为真,pthread_cond_signal用于通知阻塞的线程某个特定的条件为真了。在调用者两个函数之前需要声明一个pthread_cond_t类型的变量,用于这两个函数的参数

pthread_join用于等待某个线程退出,成功返回0,否则返回Exxx(为正数)

./Documentation/scheduler/sched-deadline.txt: pthread_join(thread, NULL);
./tools/usb/ffs-test.c: int ret = pthread_join(t->id, NULL);
./tools/usb/testusb.c: if (pthread_join (entry->thread, &retval))
./tools/usb/testusb.c: perror (“pthread_join”);
./tools/perf/bench/numa.c: ret = pthread_join(pthreads[t], NULL);
./tools/perf/bench/futex-wake.c: ret = pthread_join(worker[i], NULL);
./tools/perf/bench/futex-wake.c: err(EXIT_FAILURE, “pthread_join”);
./tools/perf/bench/futex-lock-pi.c: ret = pthread_join(worker[i].thread, NULL);
./tools/perf/bench/futex-lock-pi.c: err(EXIT_FAILURE, “pthread_join”);
./tools/perf/bench/futex-wake-parallel.c: if (pthread_join(td[i].worker, NULL))
./tools/perf/bench/futex-wake-parallel.c: err(EXIT_FAILURE, “pthread_join”);
./tools/perf/bench/futex-wake-parallel.c: ret = pthread_join(blocked_worker[i], NULL);
./tools/perf/bench/futex-wake-parallel.c: err(EXIT_FAILURE, “pthread_join”);
./tools/perf/bench/sched-pipe.c: ret = pthread_join(td->pthread, NULL);
./tools/perf/bench/futex-hash.c: ret = pthread_join(worker[i].thread, NULL);
./tools/perf/bench/futex-hash.c: err(EXIT_FAILURE, “pthread_join”);
./tools/perf/bench/futex-requeue.c: ret = pthread_join(worker[i], NULL);
./tools/perf/bench/futex-requeue.c: err(EXIT_FAILURE, “pthread_join”);
./tools/perf/bench/sched-messaging.c: pthread_join(id, &thread_status);
./tools/perf/util/event.c: pthread_join(synthesize_threads[i], NULL);
./tools/perf/tests/mmap-thread-lookup.c: err = pthread_join(threads[i].pt, NULL);
./tools/perf/builtin-top.c: pthread_join(thread, NULL);
./tools/lib/lockdep/tests/ABBA_2threads.c: pthread_join(t, NULL);
./tools/virtio/ringtest/main.c: ret = pthread_join(guest, &tret);
./tools/virtio/ringtest/main.c: ret = pthread_join(host, &tret);
./tools/virtio/virtio-trace/trace-agent.c: ret = pthread_join(rw_thread_per_cpu[cpu], NULL);
./tools/virtio/virtio-trace/trace-agent.c: pr_err(“pthread_join() error:%d (cpu %d)\n”, ret, cpu);
./tools/testing/selftests/timers/threadtest.c: pthread_join(pth[i], &tret);
./tools/testing/selftests/futex/functional/futex_requeue_pi.c: pthread_join(waiter[i],
./tools/testing/selftests/futex/functional/futex_requeue_pi.c: pthread_join(blocker, NULL);
./tools/testing/selftests/futex/functional/futex_requeue_pi.c: pthread_join(waker, NULL);
./tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c: pthread_join(waiter, NULL);
./tools/testing/selftests/futex/functional/futex_requeue_pi_mismatched_ops.c: pthread_join(child, NULL);
./tools/testing/selftests/futex/functional/futex_wait_private_mapped_file.c: pthread_join(thr, NULL);
./tools/testing/selftests/rcutorture/formal/srcu-cbmc/tests/store_buffering/test.c: if (pthread_join(tu, NULL))
./tools/testing/selftests/rcutorture/formal/srcu-cbmc/tests/store_buffering/test.c: if (pthread_join(tpr, NULL))
./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(background_threads[cpu], NULL))
./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_threads[cpu], &_userfaults[cpu]))
./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_threads[cpu], NULL))
./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(locking_threads[cpu], NULL))
./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_mon, (void **)&userfaults))
./tools/testing/selftests/vm/userfaultfd.c: if (pthread_join(uffd_mon, (void **)&userfaults))
./tools/testing/selftests/powerpc/tm/tm-unavailable.c: rc = pthread_join(t0, &ret_value);
./tools/testing/selftests/powerpc/tm/tm-unavailable.c: pr_err(rc, “pthread_join”);
./tools/testing/selftests/powerpc/tm/tm-trap.c: rc = pthread_join(t0_ping, NULL);
./tools/testing/selftests/powerpc/tm/tm-trap.c: pr_error(rc, “pthread_join()”);
./tools/testing/selftests/powerpc/tm/tm-trap.c: rc = pthread_join(t1_pong, NULL);
./tools/testing/selftests/powerpc/tm/tm-trap.c: pr_error(rc, “pthread_join()”);
./tools/testing/selftests/powerpc/tm/tm-vmx-unavail.c: pthread_join(thread[i], NULL);
./tools/testing/selftests/powerpc/tm/tm-tmspr.c: if (pthread_join(thread, NULL) != 0)
./tools/testing/selftests/powerpc/tm/tm-tmspr.c: if (pthread_join(thread, NULL) != 0)
./tools/testing/selftests/powerpc/dscr/dscr_default_test.c: if (pthread_join(threads[i], (void **)&(status[i]))) {
./tools/testing/selftests/powerpc/dscr/dscr_default_test.c: perror(“pthread_join() failed”);
./tools/testing/selftests/powerpc/math/fpu_preempt.c: pthread_join(tids[i], &rc_p);
./tools/testing/selftests/powerpc/math/fpu_signal.c: pthread_join(tids[i], &rc_p);
./tools/testing/selftests/powerpc/math/vmx_signal.c: pthread_join(tids[i], &rc_p);
./tools/testing/selftests/powerpc/math/vsx_preempt.c: pthread_join(tids[i], &rc_p);
./tools/testing/selftests/powerpc/math/vmx_preempt.c: pthread_join(tids[i], &rc_p);
./tools/testing/selftests/powerpc/benchmarks/fork.c: rc = pthread_join(tid, NULL);
./tools/testing/selftests/powerpc/benchmarks/fork.c: perror(“pthread_join”);
./tools/testing/selftests/mqueue/mq_perf_tests.c: pthread_join(cpu_threads[i], NULL);
./tools/testing/selftests/mqueue/mq_perf_tests.c: pthread_join(cpu_threads[0], &retval);
./tools/testing/selftests/sync/sync_stress_consumer.c: pthread_join(threads[i], NULL);
./tools/testing/selftests/sync/sync_stress_parallelism.c: pthread_join(a, NULL);
./tools/testing/selftests/sync/sync_stress_parallelism.c: pthread_join(b, NULL);
./tools/testing/selftests/seccomp/seccomp_bpf.c: ASSERT_EQ(0, pthread_join(thread, &status));
./tools/testing/selftests/seccomp/seccomp_bpf.c: ASSERT_EQ(0, pthread_join(thread, &status));
./tools/testing/selftests/seccomp/seccomp_bpf.c: int _rc = pthread_join(tid, status); \
./tools/testing/selftests/seccomp/seccomp_bpf.c: TH_LOG(“pthread_join of tid %u failed: %d\n”, \
./tools/testing/selftests/seccomp/seccomp_bpf.c: /* Poll for actual task death. pthread_join doesn’t guarantee it. */
./tools/testing/selftests/seccomp/seccomp_bpf.c: /* Poll for actual task death. pthread_join doesn’t guarantee it. */
./tools/testing/selftests/x86/ldt_gdt.c: if (pthread_join(thread, NULL) != 0)
./tools/testing/selftests/x86/ldt_gdt.c: err(1, “pthread_join”);
./tools/testing/selftests/x86/fsgsbase.c: if (pthread_join(thread, NULL) != 0)
./tools/testing/selftests/x86/fsgsbase.c: err(1, “pthread_join”);
./tools/testing/selftests/bpf/test_lpm_map.c: assert(pthread_join(thread_id[i], &ret) == 0 && ret == (void *)&info[i]);
./tools/testing/radix-tree/iteration_check.c: if (pthread_join(threads[i], NULL)) {
./tools/testing/radix-tree/iteration_check.c: perror(“pthread_join”);
./tools/testing/radix-tree/multiorder.c: pthread_join(worker_thread[i], NULL);
./tools/testing/radix-tree/idr-test.c: pthread_join(threads[i], NULL);
./tools/testing/radix-tree/regression1.c: if (pthread_join(threads[i], NULL)) {
./tools/testing/radix-tree/regression1.c: perror(“pthread_join”);
guang@guang-net:~/develop/linux$

// mutex.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREAD 100

void *thread_inc (void *arg);
void *thread_des (void *arg);

long long num = 0;
pthread_mutex_t mutex;

int main (int argc, char *argv)
{
    pthread_t thread_id[NUM_THREAD];
    int i;

    pthread_mutex_init (&mutex, NULL);

    for (i = 0; i < NUM_THREAD; i++)
    {
        if (i%2)
        {
            pthread_create (&(thread_id[i]), NULL, thread_inc, NULL);
        }
        else
        {
            pthread_create (&(thread_id[i]), NULL, thread_des, NULL);
        }
    }

    for (i = 0; i < NUM_THREAD; i++)
    {
        pthread_join (thread_id[i], NULL);
    }

    printf ("result: %lld \n", num);
    pthread_mutex_destroy (&mutex);
    return 0;
}

void *thread_inc (void *arg)
{
    int i;

    pthread_mutex_lock (&mutex);
    for (i = 0; i < 500; i++)
    {
        num += 1;
    }
    pthread_mutex_unlock (&mutex);
    return NULL;
}

void *thread_des (void *arg)
{
    int i;

    for (i = 0; i < 500; i++)
    {
        pthread_mutex_lock (&mutex);
        num -= 1;
        pthread_mutex_unlock (&mutex);
    }
    return NULL;
}
#include <stdio.h>

int main (void)
{
    int i;
    for (i = 0; i < 100; i ++)
    {
        printf ("%2d:%d ",i, i%2);
        if (!(i % 10))
        {
            printf ("\n");
        }

    }
    printf ("\n");

    return 0;
}
04-25
### OpenWRT 介绍 OpenWRT 是一种基于 Linux 的开源固件,专为嵌入式设备设计,尤其是无线路由器。它的目标是提供一个高度可定制的操作系统平台,允许用户通过软件扩展其功能[^1]。相比传统的路由器固件,OpenWRT 提供了更大的灵活性和更强的功能支持。 #### 特点 - **模块化架构**:用户可以根据需求安装特定的应用程序和服务。 - **强大的社区支持**:拥有活跃的开发者社区,持续更新和改进。 - **广泛的硬件兼容性**:支持多种型号的路由器和其他嵌入式设备。 - **易用性和稳定性**:提供了友好的图形界面以及可靠的命令行工具来完成复杂的配置任务。 --- ### OpenWRT 使用方法 为了充分利用 OpenWRT 功能,可以从以下几个方面入手: #### 基本操作 1. **登录到设备** - 默认情况下可以通过 SSH 登录到运行 OpenWRT 的设备,默认用户名为 `root`,密码为空或者需要自行设置。 2. **基本命令** - 查看当前网络接口的状态可以使用 `ifconfig` 或者更现代的 `ip addr` 命令。 - 更新包管理系统的索引文件以获取最新版本的软件包列表: ```bash opkg update ``` 3. **安装新软件** - 安装额外的服务或应用程序非常简单,只需执行如下命令即可: ```bash opkg install <package_name> ``` - 例如,如果想安装 LuCI(Web 界面),则输入: ```bash opkg install luci ``` --- ### OpenWRT 配置详解 #### UCI 配置系统简介 UCI (Unified Configuration Interface) 是 OpenWRT 中的核心组件之一,负责管理和存储所有的系统配置数据[^1]。它采用了一种类似于 INI 文件格式的方式来表示配置项,并且能够轻松实现跨多个服务之间的共享参数传递。 ##### 修改网络设置实例 假设我们需要更改 LAN 口的 IP 地址及相关属性,则按照以下流程进行调整[^2]: ```bash uci set network.lan.ipaddr='192.168.1.1' uci set network.lan.netmask='255.255.255.0' uci set network.lan.gateway='192.168.1.254' uci set network.lan.dns='8.8.8.8' # 应用修改后的配置 uci commit network /etc/init.d/network restart ``` 以上脚本会将本地局域网部分重新定义成指定的新数值范围;同时重启整个网络栈让改动即时生效。 #### DHCP 和 RA 设置 对于 IPv4/IPv6 自动分配机制来说,odhcpd 扮演着重要角色[^3]。它可以作为独立的服务运行也可以与其他外部实体协作工作比如 relay agent 。下面是如何启用简单的 dhcp server 实例演示: 编辑 `/etc/config/dhcp`, 添加类似这样的条目: ```ini config dhcp 'lan' option interface 'lan' option start '100' option limit '150' option leasetime '12h' ``` 之后记得再次提交变动并启动对应的服务单元: ```bash service odhcpd restart ``` --- ### 总结 综上所述,无论是初学者还是高级玩家都可以借助于 OpenWRT 来满足各自不同的应用场景下的特殊需求——从基础的家庭联网环境搭建直至复杂的企业级解决方案部署皆有可能达成!
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值