【代码1】test_alarm.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
int main(int argc, char *argv[])
{
alarm(1);
pause();
exit(0);
}
【代码2】test_kill_raise.c
#include "header.h"
void handler(int sig)
{
printf("%d\n", sig);
}
int main(int argc, char *argv[])
{
pid_t pid;
if (0 > (pid = fork()))
err_exit("fork");
else if (pid == 0) {
sleep(1);
//kill(getppid(), 11);
raise(11);
} else {
signal(17, SIG_IGN);
sleep(15);
}
exit(0);
}
【代码3】test_mysleep.c
#include "header.h"
void handler(int sig)
{
printf("in handler...\n");
}
int main(int argc, char *argv[])
{
signal(14, handler);
if (argc < 2) {
fprintf(stderr, "Usage : %s + seconds\n", argv[0]);
exit(1);
}
int sec;
if (0 >= (sec = atoi(argv[1]))) {
fprintf(stderr, "the seconds is invalid!\n");
exit(1);
}
alarm(sec);
pause();
printf("after pause...\n");
exit(0);
}
【代码4】test_pipe.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd[2] = {-1, -1};
if (0 > pipe(fd)) {
fprintf(stderr, "pipe : %s\n", strerror(errno));
exit(1);
}
//printf("fd0, fd1 is %d %d\n", fd[0], fd[1]);
pid_t pid;
if (0 > (pid = fork())) {
fprintf(stderr, "fork : %s\n", strerror(errno));
exit(1);
} else if (pid == 0) {
close(fd[0]);
char buf[128];
while(1) {
fgets(buf, sizeof(buf), stdin);
write(fd[1], buf, strlen(buf) + 1);
}
} else {
close(fd[1]);
char buf[128];
int ret;
while(1) {
ret = read(fd[0],buf, sizeof(buf));
printf("read : %d -> %s", ret, buf);
}
}
exit(0);
}
【代码5】test_pipe_size.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd[2];
pid_t pid;
if (0 > pipe(fd)) {
fprintf(stderr, "pipe : %s\n", strerror(errno));
exit(1);
}
char buf[1024];
int ret;
int sum = 0;
while(1) {
ret = write(fd[1], buf, sizeof(buf));
sum+=ret;
printf("sum is %d\n", sum);
}
exit(0);
}
【代码6】test_read_fifo.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MYFIFO "myfifo"
int main(int argc, char *argv[])
{
if (0 > mkfifo(MYFIFO, 0666)) {
if (EEXIST == errno)
fprintf(stderr, "fifo exist!\n");
else {
fprintf(stderr, "mkfifo : %s\n", strerror(errno));
exit(1);
}
}
int fd = -1;
if (0 > (fd = open(MYFIFO, O_RDONLY))) {
fprintf(stderr, "open : %s\n", strerror(errno));
exit(1);
}
char buf[128];
int ret;
while(1) {
ret = read(fd, buf, sizeof(buf));
if (ret == 0) {
printf("write terminal close!\n");
break;
}
printf("read : %s", buf);
}
close(fd);
exit(0);
}
【代码7】test_write_fifo.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define MYFIFO "myfifo"
int main(int argc, char *argv[])
{
if (0 > mkfifo(MYFIFO, 0666)) {
if (EEXIST == errno)
fprintf(stderr, "fifo exist!\n");
else {
fprintf(stderr, "mkfifo : %s\n", strerror(errno));
exit(1);
}
}
int fd = -1;
if (0 > (fd = open(MYFIFO, O_WRONLY))) {
fprintf(stderr, "open : %s\n", strerror(errno));
exit(1);
}
char buf[128];
while(1) {
fgets(buf, sizeof(buf), stdin);
write(fd, buf, strlen(buf) + 1);
}
close(fd);
exit(0);
}