linux c++ 多进程 管道

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>

#include "test.h"
//多进程
//匿名管道命名管道
//消息队列
//信号量
//共享内存
//内存映射

//文件文件夹
//网络
//时间定时器


void onexit()
{
	fprintf(stderr, "%s\n", "onexit");
}



const char* buf = "hello ";

int main(int argc, char* argv[])
{
	pid_t pid;
	int fd;

	perror(argv[0]);

	atexit(onexit);
	on_exit(on_exit_cb, (void*)1);

	fd = open("a.txt", O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);

	int _pipe[2] = { 0 };
	int ret = pipe(_pipe);
	if (ret == -1)
	{
		printf("pipe error %d", errno, strerror(errno));

		exit(-1);
	}	

	pid = fork();
	if (pid == -1)
	{		
		printf("%s", "fork error\n");
		exit(EXIT_FAILURE);
	}
	else if (pid == 0)
	{
		close(_pipe[0]);
		write(_pipe[1], "abcde", 5);

		write(fd, buf, strlen(buf));
		close(fd);

		if (-1 == execl("/home/lls/projects/testfile/bin/x64/Debug/testfile.out", "lls", "luls", NULL))
		{
			perror("execl error");
			printf("execl error: %d: %s\n", errno, strerror(errno));
		}

		//execlp("ls", "l", NULL);

		//execle("ls", "l", NULL, "/bin");

		//char* const argv[] = {"ls", NULL};
		//execv("/bin/ls", argv);

		//execvp("ls", argv);

		//char* const envp[] = { "/bin", NULL };
		//execve("ls", argv, envp);


		//printf("i'm child child_pid = %d ppid = % d!\n", getpid, getppid);	
		//exit(110);
	}

	close(_pipe[1]);
	char szbuf[10] = { 0 };
	read(_pipe[0], szbuf, 5);

	fprintf(stderr, "read pipe : %s\n", szbuf);


	int s = access("./temp.txt", 0);
	if (s != 0)
	{
		ret = mkfifo("./temp.txt", 0666 | __S_IFIFO);
		if (ret == -1)
		{
			perror("mkfifo error");
		}
	}

	int fifofd = open("./temp.txt", O_RDONLY);
	char* szb[10] = { 0 };
	read(fifofd, szb, 5);
	close(fifofd);

	fprintf(stderr, "read fifo : %s\n", szb);

	printf("i'm parent pid = %d!\n", getpid());

	int status = 0;
	waitpid(pid, &status, 0);

	lseek(fd, 0, SEEK_SET);

	char buf2[100] = { 0 };
	ssize_t res = read(fd, (void*)buf2, 100);

	close(fd);

	printf("%s\n", buf2);

    return 0;
}

void on_exit_cb(int status, void* arg)
{
	fprintf(stderr, "on_exit_cb status = %d arg = %d \n", status, (int)arg);
}

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
	printf("------------------------\n");
	printf(argv[0]);
	printf("\n");
	printf(argv[1]);
	printf("\n");
    printf("hello from testfile!\n");

	char path[1024] = { 0 };
	getcwd(path, 1024);
	printf(path);
	printf("\n");

	int s = access("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt", 0);
	if (s == 0)
	{
		//unlink("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt");
	}
	else
	{
		int ret = mkfifo("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt", 0666 | __S_IFIFO);
		if (ret == -1)
		{
			perror("mkfifo error");
		}
	}
	int fifofd = open("./temp.txt", O_WRONLY);
	write(fifofd, "edcba", 5);
	close(fifofd);

	printf("------------------------\n");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值