练习环境Centos6.5 , linux内核版本2.6
p4, 1-3
/*************************************************************************
# > File Name: 1-3.c
#
#
# Created Time: 2017年02月28日 星期二 23时53分36秒
************************************************************************/
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
int main(int argc, char *argv[] )
{
DIR *dp;
struct dirent *dirp;
if(argc != 2)
{
printf("usage:ls directory_name");
return 1;
}
if( (dp = opendir(argv[1]))== NULL )
printf("can't open %s", argv[1]);
while( (dirp = readdir(dp))!= NULL)
printf("%s\n",dirp->d_name);
closedir(dp);
return 0;
}
p7 1-4
/*************************************************************************
# > File Name: 1-4.c
#
#
# Created Time: 2017年03月01日 星期三 00时08分40秒
************************************************************************/
#include<stdio.h>
#include <unistd.h>
#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)
printf(" write error \n");
}
if(n < 0)
{
printf("read error \n");
}
return 0;
}
p8 1-5
/*************************************************************************
# > File Name: 1-5.c
#
#
# Created Time: Wed 01 Mar 2017 12:24:11 AM PST
************************************************************************/
#include<stdio.h>
int main()
{
int c;
while((c = getc(stdin)) != EOF)
if(putc(c, stdout) == EOF)
printf("putc error\n");
if(ferror(stdin))
printf("input error\n");
return 0;
}
p9 1-6
/*************************************************************************
# > File Name: 1-6.c
#
#
# Created Time: Wed 01 Mar 2017 12:29:32 AM PST
************************************************************************/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
printf("hello wrold from process ID %ld \n", (long)getpid());
return 0;
}
p9 1-7
/*************************************************************************
# > File Name: 1-7.c
#
#
# Created Time: Wed 01 Mar 2017 12:33:40 AM PST
************************************************************************/
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>
#define MAXLINE 4096
int main()
{
char buf[MAXLINE];
pid_t pid;
int status;
printf("%%");// printf prompt (printf requires %% to print %)
while (fgets(buf, MAXLINE, stdin) != NULL)
{
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0'; // replace new line witg null
if ((pid = fork()) < 0)
{
printf("fork error\n");
return 1;
}
else if(pid == 0)
{// child
execlp(buf, buf , NULL);
printf("couldn't execute: %s \n", buf);
return 2;
}
if( (pid = waitpid(pid, &status, 0) < 0))
{// father
printf("waitpid error");
return 3;
}
printf("%% ");
}
return 0;
}
p12 1-8
/*************************************************************************
# > File Name: 1-8.c
#
#
# Created Time: Wed 01 Mar 2017 01:08:52 AM PST
************************************************************************/
#include<stdio.h>
#include <errno.h>
int main(int argc, char* argv[])
{
fprintf(stderr, "EACCES:%s\n", strerror(EACCES));
errno = ENOENT;
perror(argv[0]);
return 0;
}
p 14 1-9
/*************************************************************************
# > File Name: 1-9.c
#
#
# Created Time: Wed 01 Mar 2017 01:22:31 AM PST
************************************************************************/
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("uid = %d , gid = %d \n", getuid(), getgid());
return 0;
}
p15 1-10
/*************************************************************************
# > File Name: 1-10.c
#
#
# Created Time: Wed 01 Mar 2017 12:33:40 AM PST
************************************************************************/
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<string.h>
#include<signal.h>
#define MAXLINE 4096
static void sig_int(int); // our signal-catching function
int main()
{
char buf[MAXLINE];
pid_t pid;
int status;
if(signal(SIGINT, sig_int) == SIG_ERR)
{
printf("signal error");
return 4;
}
printf("%%");// printf prompt (printf requires %% to print %)
while (fgets(buf, MAXLINE, stdin) != NULL)
{
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0'; // replace new line witg null
if ((pid = fork()) < 0)
{
printf("fork error\n");
return 1;
}
else if(pid == 0)
{// child
execlp(buf, buf , NULL);
printf("couldn't execute: %s \n", buf);
return 2;
}
if( (pid = waitpid(pid, &status, 0) < 0))
{// father
printf("waitpid error");
return 3;
}
printf("%% ");
}
return 0;
}
void sig_int(int signo)
{
printf("interrupt\n %%");
}