aio.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <libaio.h>
int main(void)
{
int output_fd;
struct iocb io, *p=&io;
struct io_event e;
struct timespec timeout;
io_context_t ctx;
const char *content="hello world!";
// 1. init the io context.
memset(&ctx, 0, sizeof(ctx));
if(io_setup(10, &ctx)){
printf("io_setup error\n");
return -1;
}
// 2. try to open a file.
if((output_fd=open("foobar.txt", O_CREAT|O_WRONLY, 0644)) < 0) {
perror("open error");
io_destroy(ctx);
return -1;
}
// 3. prepare the data.
io_prep_pwrite(&io, output_fd, (void*)content, strlen(content), 0);
//io.data = content; // set or not
if(io_submit(ctx, 1, &p) < 0){
io_destroy(ctx);
printf("io_submit error\n");
return -1;
}
// 4. wait IO finish.
while(1) {
timeout.tv_sec = 0;
timeout.tv_nsec = 500000000; // 0.5s
if(io_getevents(ctx, 0, 1, &e, &timeout) == 1) {
close(output_fd);
break;
}
printf("haven't done\n");
sleep(1);
}
io_destroy(ctx);
return 0;
}
asm1.c
void main()
{
asm(
"mov $3,%eax"
);
return;
}
// gcc asm1.c
// ./a.out
// echo $?
asm2.c
int main()
{
asm(
"movl $10,%ebx"
);
asm(
"movl %ebx,%ecx"
);
int var;
asm(
"movl %%ecx,%0;" : "=m"(var)
);
// register int var1 asm("ecx");
// var = var1;
asm(
"movl %0, %%eax\n\t"
"add $1, %%eax\n\t"
:
: "r" (var)
: "%eax"
);
int ret;
asm(
"movl %%eax,%0;" : "=m"(ret)
);
return ret;
}
// gcc asm2.c
// ./a.out
// echo $?
directio.c
#define _GNU_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc,char** argv)
{
if (argc<2)
{
printf("need file param\n");
return -1;
}
char *filePath = argv[1];
int fd = open(filePath,O_RDONLY | O_DIRECT | O_LARGEFILE);
if (fd<0)
{
printf("open file error:%s",filePath);
return -1;
}
void *buffer;
posix_memalign(&buffer, 4096, 4096);
int ret = read(fd,buffer,4096);
printf("ret:%d\n",ret);
free(buffer);
close(fd);
return 0;
}
futex.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/futex.h>
#include <sys/syscall.h>
void* worker(void *ptr);
int main()
{
int thread_num = 2;
pthread_t *thread_ids = malloc(sizeof(pthread_t)*thread_num);
int *futex_atom = malloc(sizeof(int));
*futex_atom = 0;
for (int i=0;i<thread_num;i++)
{
pthread_create(&thread_ids[i],NULL,&worker,futex_atom);
}
for (int j=0;j<10;j++)
{
sleep(2);
syscall(SYS_futex, (int*)futex_atom, FUTEX_WAKE, 1, 0, 0, 0);
}
for (int i=0;i<thread_num;i++)
{
pthread_join(thread_ids[i],NULL);
}
free(thread_ids);
free(futex_atom);
return 0;
}
void* worker(void* ptr)
{
int count = 0;
unsigned int id = pthread_self();
long int pid = syscall(SYS_gettid);
printf("start worker %u %lu\n",id,pid);
while (1)
{
syscall(SYS_futex, (int*)ptr, FUTEX_WAIT, 0, 0, 0, 0);
count++;
printf("%lu %d\n",pid,count);
}
}
io_bench.c
#define _GNU_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include<getopt.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
struct benchparam
{
char* file;
int fd;
int size;
int worker;
int block;
int sec;
int counter;
};
void io(void*);
int main(int argc,char** argv)
{
int opt;
char *string = "f:w:b:s:";
struct benchparam bp;
bp.file = NULL;
bp.worker = 1;
bp.block = 4096;
bp.sec = 5;
while ((opt = getopt(argc, argv, string))!= -1)
{
switch (opt) {
case 'f':
bp.file = optarg;
break;
case 'w':
bp.worker = atoi(optarg);
break;
case 'b':
bp.block = atoi(optarg);
break;
case 's':
bp.sec = atoi(optarg);
break;
}
}
printf("benchmark:\n");
printf(" f %s\n",bp.file);
printf(" w %d\n",bp.worker);
printf(" b %d\n",bp.block);
printf(" s %d\n",bp.sec);
int fd = open(bp.file,O_RDONLY | O_DIRECT | O_LARGEFILE);
if (fd<0)
{
printf("open file error:%s",bp.file);
return -1;
}
bp.fd = fd;
struct stat fileStat;
fstat(fd, &fileStat);
bp.size = fileStat.st_size;
printf(" size %d\n",bp.size);
srand(time(NULL));
pthread_t threads[100];
for (int i=0;i<bp.worker;i++)
{
pthread_t thread;
int r = pthread_create(&thread,NULL,(void*)&io,&bp);
if (r<0)
{
printf("creat thread failed:%d\n",r);
return -1;
}
threads[i] = thread;
}
sleep(bp.sec);
printf("result:\n");
printf(" counter %d\n",bp.counter);
close(fd);
return 0;
}
void io(void *ptr)
{
struct benchparam *bp = (struct benchparam *)ptr;
void *buffer;
posix_memalign(&buffer, 4096, bp->block);
while (1)
{
int r = rand();
int offset = (r%bp->size-bp->block)/4096*4096;
//printf("offset:%d %d\n",r,offset);
lseek(bp->fd,offset,SEEK_SET);
int ret = read(bp->fd,buffer,bp->block);
if (ret<0)
{
printf("read file err:%d\n",ret);
break;
}
__sync_fetch_and_add(&bp->counter,1);
}
free(buffer);
}
syscall.c
#include <unistd.h>
#include <sys/syscall.h>
int main()
{
syscall(SYS_write,1,"hello world!\n",13);
return 0;
}
threadpool.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct job {
void *(*process) (void * arg);
void * arg;
struct job *next;
} async_job;
typedef struct {
pthread_mutex_t queue_lock;
pthread_cond_t queue_ready;
async_job *queue_head;
int shutdown;
pthread_t *threadid;
int max_thread_num;
int cur_queue_size;
} thread_pool;
int pool_add_job(void* (*process) (void* arg),void* arg);
void* worker(void *);
static thread_pool *pool = NULL;
void pool_init(int max_thread_num)
{
pool = (thread_pool*)malloc(sizeof(thread_pool));
pthread_mutex_init(&pool->queue_lock,NULL);
pthread_cond_init(&pool->queue_ready,NULL);
pool->shutdown = 0;
pool->threadid = (pthread_t*)malloc(max_thread_num*sizeof(pthread_t *));
pool->max_thread_num = max_thread_num;
pool->cur_queue_size = 0;
for (int i=0;i<max_thread_num;i++)
{
pthread_create(&(pool->threadid[i]),NULL,&worker,NULL);
}
}
int pool_add_job(void* (*process) (void* arg),void* arg)
{
async_job *job = (async_job*)malloc(sizeof(async_job));
job->process = process;
job->arg = arg;
job->next = NULL;
pthread_mutex_lock(&pool->queue_lock);
async_job *member = pool->queue_head;
if (member != NULL)
{
while (member->next != NULL)
member = member->next;
member->next = job;
} else {
pool->queue_head = job;
}
pool->cur_queue_size++;
pthread_mutex_unlock(&pool->queue_lock);
pthread_cond_signal(&pool->queue_ready);
return 0;
}
int pool_destroy()
{
if (pool->shutdown)
return -1;
pool->shutdown =1;
pthread_cond_broadcast(&(pool->queue_ready));
for (int i=0;i<pool->max_thread_num;i++)
{
pthread_join(pool->threadid[i],NULL);
}
free(pool->threadid);
async_job *head = NULL;
while (pool->queue_head != NULL)
{
head = pool->queue_head;
pool->queue_head = head->next;
free(head);
}
pthread_mutex_destroy(&(pool->queue_lock));
pthread_cond_destroy(&(pool->queue_ready));
free(pool);
pool = NULL;
}
void * worker(void* arg)
{
printf("start worker %ld\n",pthread_self());
while (1)
{
pthread_mutex_lock(&(pool->queue_lock));
while (pool->cur_queue_size==0 && !pool->shutdown)
{
pthread_cond_wait(&(pool->queue_ready),&(pool->queue_lock));
}
if (pool->shutdown)
{
pthread_mutex_unlock(&(pool->queue_lock));
pthread_exit(NULL);
}
pool->cur_queue_size--;
async_job *job = pool->queue_head;
pool->queue_head = job->next;
pthread_mutex_unlock(&(pool->queue_lock));
(*(job->process))(job->arg);
free(job);
}
pthread_exit(NULL);
}
void* my_job(void *arg)
{
int *id = (int *)arg;
printf("hello %d\n",*id);
}
int main(int argc, char** argv)
{
pool_init(3);
int job_num = 10;
int *job_arg = (int *)malloc(sizeof(int)*job_num);
for (int i=0;i<job_num;i++)
{
job_arg[i] = i;
pool_add_job(&my_job,&(job_arg[i]));
}
sleep(5);
pool_destroy();
free(job_arg);
return 0;
}
嗅探命令行参数
#include <stdio.h>
int main(int argc,char** argv)
{
asm(
"movl %0, %%eax\n\t"
:
: "r" (argc)
: "%eax"
);
int argc_find;
asm(
"movl %%eax,%0;" : "=m"(argc_find)
);
printf("%d\n",argc_find);
asm(
"movl %0, %%eax\n\t"
:
: "r" (argv)
: "%eax"
);
char** argv_find;
asm(
"movl %%eax,%0;" : "=m"(argv_find)
);
for (int i=0;i<argc_find;i++)
{
printf("%s ",argv_find[i]);
}
printf("\n");
return 0;
}
嗅探命令行参数2
#include <stdio.h>
int main()
{
int argc_find;
asm(
"movl 8(%%ebp),%%eax\n\t"
"movl %%eax,%0;" : "=m"(argc_find)
);
printf("%d\n",argc_find);
char** argv_find;
asm(
"movl 12(%%ebp),%%eax\n\t"
"movl %%eax,%0;" : "=m"(argv_find)
);
for (int i=0;i<argc_find;i++)
{
printf("%s ",argv_find[i]);
}
printf("\n");
return 0;
}
网友评论