first commit

This commit is contained in:
corey 2024-01-12 11:35:17 -06:00
commit 3f86007857
7 changed files with 163 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/async/async
/readspeed/readspeed
*.o

4
async/Makefile Normal file
View File

@ -0,0 +1,4 @@
CC= tcc
all: async
clean:
rm async

52
async/async.c Normal file
View File

@ -0,0 +1,52 @@
/*****
*
* Print start and end
* time after sleeping
* a random interval
*
*****/
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdint.h>
#include<string.h>
#define id (name)?(name):(argv[1])
char*tmstr(time_t t)
{
struct tm*ptm=localtime(&t);
char buffer[32]={0};
strftime(buffer,32,"%H:%M:%S",ptm);
return buffer;
}
int main(int argc,char**argv)
{
time_t spawn=time(NULL);
time_t now=0;
char spawn_str[32]={0};
char now_str[32]={0};
uint32_t seed=0;
char*name=NULL;
if(argc<2)return 1;
if(argc>2)name=argv[2];
for(size_t i=0;argv[1][i];++i)
seed+=argv[1][i];
strcpy(spawn_str,tmstr(spawn));
//printf("[%s]:seed:%lu\n",argv[1],seed);
srand(seed);
sleep(rand()%5);
now=time(NULL);
strcpy(now_str,tmstr(now));
printf("[%s] %s %s\n",id,spawn_str,now_str);
//printf("spawn:%lu, now:%lu\n",spawn,now);
}

11
async/run Executable file
View File

@ -0,0 +1,11 @@
#!/bin/sh
main()
{
[ -e ./async ] || make || return 1
for x in $(seq 1 5); do
./async "$(dd if=/dev/urandom of=/dev/stdout bs=1 count=8 status=none)" $x &
done
}
main $*

4
readspeed/Makefile Normal file
View File

@ -0,0 +1,4 @@
CC= tcc
all: readspeed
clean:
rm readspeed foo

74
readspeed/readspeed.c Normal file
View File

@ -0,0 +1,74 @@
/*****
*
* Test time of reading
* file into buffer with
* different block sizes
*
*****/
#include<fcntl.h>
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<unistd.h>
void bufferread(const int fd,const size_t bs)
{
uint8_t buffer[bs];
if(fd<=2)
{
printf("error: file descriptor cannot be 0, 1, or 2\n");
return;
}
if(bs<1)
{
printf("error: bs cannot be zero\n");
return;
}
while(read(fd,buffer,bs)>0)
{
}
}
int main(const int argc,const char*argv[])
{
if(argc<2)
{
printf("error: input file not specified\n");
return 1;
}
if(argc<3)
{
printf("error: bs not specified\n");
return 1;
}
const size_t bs=atoi(argv[2]);
if(bs==0)
{
printf("error: invalid bs\n");
return 1;
}
//printf("bs: %lu\n",bs);
const char*const infilename=argv[1];
const int infilefd=open(infilename,O_RDONLY);
if(infilefd<0)
{
printf("error: failed to open file '%s'\n",infilename);
return 1;
}
bufferread(infilefd,bs);
}

15
readspeed/run Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
main()
{
local FILENAME=foo
i=1
[ -e ./readspeed ] || make || return 1
[ -e ./foo ] || dd if=/dev/zero of=foo bs=1M count=100 status=none
for x in 32 64 128 256 512 1024; do
time -qf '%C %E' ./readspeed $FILENAME $x "[$i]" &
let i++
done
}
main $*