进程和线程编程之三

【代码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);
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百里杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值