Linux C 标准程序

本文介绍了在Linux环境下使用C语言进行程序开发的关键技术,包括注册信号的处理,如signal和sigaction函数,深入讲解命令行参数解析的getopt和getopt_long函数,线程的创建与同步,如pthread_create和pthread_join,以及进程管理,如fork和vfork。通过这些内容,帮助读者理解Linux C程序设计的核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模块介绍

注册信号

signal

sigaction

命令行解析

getopt

getopt_long

getopt和getopt_long函数参考链接

线程

pthread_create

pthread_join

同步 与 互斥

进程

fork

vfork

附上代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>     /* not strings.h !!! */
#include <stdarg.h>
#include <time.h>

#include <stdint.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/wait.h>       /* WEXITSTATUS */
#include <linux/sockios.h>  /* TIOCOUTQ */
#include <pthread.h>
#include <signal.h>

#define nt_log(fmt, ...) \
    do { \
        fprintf(stderr, "%s %d " fmt, __FUNCTION__, __LINE__,  ##__VA_ARGS__);\
} while (0)

#define PROG_NAME "daemon"

static void usage(void);
static void sig_handle(int sig);
static int prog_exit(int eno);
static void *worker_thread(void *arg);
static void *event_thread(void *arg);

/*********************************************************************************
* Global Variables
*/
int g_debug = 0;
int g_system_done = 0;
/********************************************************************************/

//entry
int main(int argc, char *argv[])
{
    printf("argc is %d\n", argc);

    //signal deal start
    struct sigaction sa;
    sa.sa_flags = 0;

    sa.sa_handler = SIG_IGN;
    sigaction(SIGCHLD, &sa, NULL); /* WIFEXITED and WEXITSTATUS after system need SIGCHLD */
    sigaction(SIGPIPE, &sa, NULL);

    sa.sa_handler = sig_handle;
    sigaction(SIGHUP, &sa, NULL);
    sigaction(SIGQUIT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL); /* killall */
    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTSTP, &sa, NULL);
    //signal deal end

    //parse cmd start
    int c = 0;
    while ((c = getopt(argc, argv, "hd")) != EOF) {
        switch (c) {
            case 'h':
        	    usage();
                exit(1);
       	    case 'd':
           	    g_debug = 1;
                break;
            default:
                usage();
                exit(1);
        }
    }

    argv += optind;
    argc -= optind;
    //parse cmd end

    pthread_t tid_worker_thread;
    pthread_t tid_event_thread;
    pthread_create(&tid_worker_thread, NULL, worker_thread, NULL);
    pthread_create(&tid_event_thread, NULL, event_thread, NULL);

    while (0 == g_system_done) {
        sleep(1);
    }

    //等待线程结束并回收资源
    pthread_join(tid_worker_thread, NULL);
    pthread_join(tid_event_thread, NULL);

    prog_exit(0);
    return 0;
}

static void *worker_thread(void *arg)
{
    if (g_debug) {
        printf("worker_thread start ...\n");
    }
    while (0 == g_system_done) {
        sleep(1);
    }
    if (g_debug) {
        printf("worker_thread end ...\n");
    }
    pthread_exit(NULL);
}

static void *event_thread(void *arg)
{
    if (g_debug) {
        printf("event_thread start ...\n");
    }
    while (0 == g_system_done) {
        sleep(1);
    }
    if (g_debug) {
        printf("event_thread end ...\n");
    }
    pthread_exit(NULL);
}

static void usage(void)
{
    fprintf(stderr, "Usage is:\n");
    fprintf(stderr, "------------------------------\n");
    printf("-h: show help message\n");
    printf("-g: open debug mode\n");
    fprintf(stderr, "------------------------------\n");
}

static void sig_handle(int sig)
{
    printf("receive signal %d\n", sig);
    switch (sig) {
        case SIGPIPE:
        case SIGHUP:
        case SIGINT:
        case SIGTSTP:
        case SIGTERM:
        case SIGQUIT:
        	if (g_debug) {
        	    printf(PROG_NAME" end ...");
        	}
            g_system_done = 1;
            break;
    }
}

static int prog_exit(int eno)
{
    exit(eno);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值