学习到了 常用函数 system

This commit is contained in:
lzy
2024-05-26 08:24:54 +08:00
parent 6f754aa23d
commit 6f28a3384a
16 changed files with 1066 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
# 方便起见一般都会先定义编译器链接器
CC = gcc
LD = gcc
# 正则表达式表示目录下所有.c文件相当于SRCS = main.c a.c b.c
SRCS = $(wildcard *.c)
# OBJS表示SRCS中把列表中的.c全部替换为.o相当于OBJS = main.o a.o b.o
OBJS = $(patsubst %c, %o, $(SRCS))
# 可执行文件的名字
TARGET = main
# .PHONE伪目标具体含义百度一下一大堆介绍
.PHONY:all clean
# 要生成的目标文件
all: $(TARGET)
LDFLAGS=-L/usr/local/lib
# LDLIBS=-lqueue -lllist
# 第一行依赖关系冒号后面为依赖的文件相当于Hello: main.o a.o b.o
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
$(TARGET): $(OBJS)
$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS)
# 上一句目标文件依赖一大堆.o文件这句表示所有.o都由相应名字的.c文件自动生成
%.o:%.c
$(CC) -c $^
# make clean删除所有.o和目标文件
clean:
rm -f $(OBJS) $(TARGET)

View File

@@ -0,0 +1,47 @@
#ifndef __ANYTIMER__H__
#define __ANYTIMER__H__
#define JOB_MAX 1024
typedef void at_jobfunc_t(void *);
/********************************************************************
* @brief 创建定时器
* @details
* @param sec
* @param jobp
* @param arg
* @return int
* >= 0 成功返回定时器ID
* == -EINVAL 失败,参数错误
* == -ENOMEM 失败,内存不足
* == -ENOSPC 失败,数组满
********************************************************************/
int at_addjob(int sec, at_jobfunc_t *jobp, void *arg);
/********************************************************************
* @brief 取消定时器
* @details
* @param id
* @return int
* == 0 成功,定时器已取消
* == -EINVAL 失败,参数错误
* == -EBUSY 失败,指定任务已完成
* == -ECANCELED 失败,定时器重复取消
********************************************************************/
int at_canceljob(int id);
/*********************************************************************
* @brief 回收任务
* @details
* @param id
* @return int
* == 0 成功,任务已回收
* == -EINVAL 失败,参数错误
********************************************************************/
int at_waitjob(int id);
int at_pausejob(int id);
int at_resumejob(int id);
#endif //!__ANYTIMER__H__

View File

@@ -0,0 +1,40 @@
#include "anytimer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void f1(void *p)
{
printf("f1():%s\n", (char *)p);
}
static void f2(void *p)
{
printf("f1():%s\n", (char *)p);
}
int main(int argc, char **argv)
{
int job1;
puts("Begin!");
job1 = at_addjob(5, f1, "aaa");
if (job1 < 0)
{
fprintf(stderr, "at_addjob() failed!:%s\n", strerror(-job1));
exit(1);
}
puts("End!");
while (1)
{
write(1, ".", 1);
sleep(1);
}
exit(0);
}