UNIX系统编程2<C代码>2

UNIX系统编程2<C代码>2

    配合UNIX系统编程2<笔记>使用。

01tst_fork.c:
#include <stdio.h>
#include <unistd.h>

int main()
{
	pid_t pid = fork();

	switch (pid) {
		case -1:
			perror("fork");
			goto err_fork;
		case 0: //由子进程执行
			printf("child: pid=%d, ppid=%d\n", getpid(), getppid());
			sleep(1); //故意让父进程先退出,此时子进程失去父亲,成为孤儿,然后被1号进程领养。1号进程在子进程代码执行完毕后回收该进程资源
			printf("child: pid=%d, ppid=%d\n", getpid(), getppid());
			break;
		default: //由父进程执行
			printf("parent: pid=%d\n", getpid());
			break;
	}

	return 0;

err_fork:
	return -1;
}
02tst_wait.c
#include <stdio.h>
#include <unistd.h>

int main()
{
	pid_t pid = fork();

	switch (pid) {
		case -1:
			perror("fork");
			goto err_fork;
		case 0: //由子进程执行
			printf("child: pid=%d, ppid=%d\n", getpid(), getppid());
			sleep(1);
			printf("child: pid=%d, ppid=%d\n", getpid(), getppid());
			break;
		default: //由父进程执行
			printf("parent: pid=%d\n", getpid());
			wait(NULL); //父进程在此挂起,等待子进程的退出,然后将子进程的资源回收
			printf("wait success\n");
			break;
	}
	return 0;

err_fork:
	return -1;
}
03tst_waitpid.c
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
int main()
{
	pid_t pid = fork();
	int status = 0;

	switch (pid) {
		case -1:
			perror("fork");
			goto err_fork;
		case 0: //由子进程执行
			printf("child: pid=%d, ppid=%d\n", getpid(), getppid());
			sleep(1);
			printf("child: pid=%d, ppid=%d\n", getpid(), getppid());
			return 123;
		default: //由父进程执行
			printf("parent: pid=%d\n", getpid());
			wait(&status); //父进程在此挂起,等待子进程的退出,然后将子进程的资源回收
			printf("wait success\n");
			printf("child return %d\n", WEXITSTATUS(status));
			break;
	}

	return 0;

err_fork:
	return -1;
}
04tst_system.c
#include <stdio.h>
#include <stdlib.h>

int main()
{
	system("ls -l -a"); //开一个子进程,加载shell环境,运行shell命令,此时,主进程处于挂起状态。执行完以后,会回到主进程,继续执行主进程的内容。一般很少在正式代码中使用system,因为system开销过大,但是调试代码时很好用

	printf("system succeed\n");

	return 0;
}
05tst_exec.c
#include <stdio.h>
#include <unistd.h>

int main()
{
#if 0
	//最后一个参数为哨兵参数,告诉函数,参数列表到此结束
	//后缀l表示以可变参数的方式来传递参数
	int ret = execl("/bin/ls", "ls", "-a", "-l", NULL);
	if (ret < 0) {
		perror("execl");
	}
#endif
	//后缀p表示引入PATH环境变量来辅助查找可执行文件路径,在这里我们就不需要列出已经在PATH定义过的路径
#if 0	
	int ret = execlp("ls", "ls", "-a", "-l", NULL);
	if (ret < 0) {
		perror("execlp");
	}
#endif
	//后缀v表示用字符串数组来传递参数,哨兵参数仍然需要
	char *arg[] = {"ls", "-l", "-a", NULL};
	int ret = execvp("lsl", arg);
	if (ret < 0) {
		perror("execvp");
	}

	printf("exec error\n"); //当exec执行成功后,整个进程被擦除覆盖为新进程的内容,所以也不可能再执行原进程的代码。如果exec调用失败,那么还继续执行原进程的代码
	return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
	int pipefd[2] = {0};

	//pipe创建两个文件描述符,并传递进数组里
	//这两个文件描述符是一对管道
	if (pipe(pipefd) < 0) {
		perror("pipe");
		goto err_pipe;
	}
	printf("%d, %d\n", pipefd[0], pipefd[1]);
#define BUF_SIZE 12	
	unsigned char wbuf[BUF_SIZE] = {0};
	unsigned char rbuf[BUF_SIZE] = {0};

	strncpy(wbuf, "helloworld", sizeof(wbuf));

	if (write(pipefd[1], wbuf, sizeof(wbuf)) < 0) {
		perror("write");
	}	
	if (read(pipefd[0], rbuf, sizeof(rbuf)) < 0) {
		perror("read");
	}
	printf("%s\n", rbuf);

	close(pipefd[0]);
	close(pipefd[1]);
	memset(rbuf, 0 ,sizeof(rbuf));

	while (read(STDIN_FILENO, rbuf, sizeof(rbuf))) {
		write(STDOUT_FILENO, rbuf, sizeof(rbuf));
		memset(rbuf, 0 ,sizeof(rbuf));
	}
	return 0;
err_pipe:
	return -1;
}

/*-----------------------------------------------end ---------------------------------------------------*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值