time:
进程的时钟时间,用户时间和系统时间:只要执行命令time(1),
其参数是要试题其执行时间的命令。
Example:
$cd /usr/include
$time grep _POSIX_SOURCE */*.h > /dev/null
time命令的输出格式也所使用的shell有关。
Makefile:
mycp:mycp.c
gcc -o $@ $<
clean:
rm -f *.o mycp
myls.c:
#include
int main(int argc, char *argv[])
{
DIR *dp = NULL;
//struct dirent *dirp = NULL;
struct dirent* dirp;
if(argc != 2)
err_quit("a single argument (the directory name) is required\n");
if((dp = opendir(argv[1])) == NULL)
err_sys("can't open %s\n", argv[1]);
while((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
closedir(dp);
exit(0);
}
mycp.c:
#define BUFFSIZE 4096
int main()
{
int n;
char buf[BUFFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
{
if (write(STDOUT_FILENO, buf, n) != n)
{
err_sys("write error");
}
}
if (n < 0)
{
err_sys("read error");
}
exit(0);
}
mycp2.c:
int main()
{
int c;
while ((c = getc(stdin)) != EOF)
{
if (putc(c, stdout) == EOF)
{
err_sys("output error");
}
}
if (ferror(stdin))
{
err_sys("input error");
}
exit(0);
}
id.c:
int main()
{
printf("uid = %d, gid = %d\n", getuid(), getgid());
exit(0);
}
pid.c:
#include "apue.h"
int main()
{
printf("Hello world from process ID %d\n", getpid());
exit(0);
}
err.c:
#include
int main(int argc, char *argv[])
{
fprintf(stderr, "EACCES: %s\n", strerror(EACCES));
errno = ENOENT;
perror(argv[0]);
exit(0);
}
exec.c:
#include
int main()
{
char buf[MAXLINE];
pid_t pid;
int status;
printf("%% ");
while (fgets(buf, MAXLINE, stdin) != NULL)
{
if (buf[strlen(buf) - 1] == '\n')
{
buf[strlen(buf) - 1] = 0; /*替换最后一行的回车为NULL*/
}
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
/*child*/
else if (pid == 0)
{
execlp(buf, buf, (char *)0);
err_ret("couldn't execute: %s", buf);
exit(127);
}
/* parent */
if ((pid = waitpid(pid, &status, 0)) < 0)
{
err_sys("waitpid error");
}
printf("%% ");
}
exit(0);
}
singal.c
#include
static void sig_int(int); /* our signal -catching function*/
int main()
{
char buf[MAXLINE]; /* from apu.h */
pid_t pid;
int status;
if (signal(SIGINT, sig_int) == SIG_ERR)
{
err_sys("signal error");
}
printf("%% ");
while (fgets(buf, MAXLINE, stdin) != NULL)
{
if (buf[strlen(buf) - 1] = 0)
{
buf[strlen(buf) - 1] = 0;
}
if ((pid = fork()) < 0)
{
err_sys("fork error");
}
/* child */
else if (pid == 0)
{
execlp(buf, buf, (char *)0);
err_ret("couldn't execute: %s", buf);
exit(127);
}
/* parent */
if ((pid = waitpid(pid, &status, 0)) < 0)
{
err_sys("waitpid error");
}
printf("%% ");
}
exit(0);
}
void sig_int(int signo)
{
printf("interrupt\n%% ");
}
阅读(947) | 评论(1) | 转发(0) |