嵌入式学习代码总结-进程(三)

#include<stdio.h>
int main(int argc,char *argv[])
{
	FILE *fd=NULL;
	fd=fopen(argv[1],"r");
	char ch;
	if(NULL==fd)
	{
		perror("open error\n");
		return -1;
	}
	while(-1!=(ch=fgetc(fd)))
	{
		fputc(ch,stdout);
	}
	fclose(fd);
	return 0;
}
/*************************************************************************
    > File Name: while.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 03:32:02 PM CST
 ************************************************************************/

#include<stdio.h>


int main()
{
	while(1);
}

/*************************************************************************
    > File Name: pthread_mutext.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 10:10:09 AM CST
 ************************************************************************/

#include<stdio.h>
#include <pthread.h>
#include <unistd.h>

#define BUF_SIZE 11
pthread_mutex_t mutex;

void *ThreadFunc(void *arg)
{
	int i=0;
	printf("into thread func--------\r\n");
	if(NULL==arg)
	{
		return (void *)NULL;
	}
	pthread_mutex_lock(&mutex);
	char *pArr=(char *)arg;
	static char buf[BUF_SIZE]={0};
	for(;i<10;i++)
	{
		buf[i]=pArr[i];
		usleep(5000);
	}
	printf("buf:%s\r\n",buf);
	pthread_mutex_unlock(&mutex);
}

int main()
{
	pthread_t tID1=-1;
	pthread_t tID2=-1;
	char arr1[]="123456789";
	char arr2[]="abcdefghi";
	if(-1==pthread_mutex_init(&mutex,NULL))
	{
		return -1;
	}
	if(0!=pthread_create(&tID1,NULL,ThreadFunc,(void *)arr1))
	{
		return -1;
	}
	if(0!=pthread_create(&tID2,NULL,ThreadFunc,(void *)arr2))
	{
		return -1;
	}
	pthread_join(tID1,NULL);
	pthread_join(tID2,NULL);
	return 0;
}

/*************************************************************************
    > File Name: piped.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 12:25:16 PM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>
#include <string.h>

#define BUF_SIZE 20

int main()
{
	//1 pipe
	int fd[2]={0};
	if(0!=pipe(fd))
	{
		printf("create pipe error!\r\n");
		return -1;
	}
	printf("1:create pipe ok!\r\n");
	//2 fork
	pid_t pid=-1;
	pid=fork();
	if(-1==pid)
	{
		return -1;
	}
	char buf[BUF_SIZE]={0};
	if(0==pid)
	{
		//child write
		close(fd[0]);
		printf("child process!\r\n");
		strcpy(buf,"child to parent");
		write(fd[1],buf,BUF_SIZE);
	}
	else
	{
		//parent read
		close(fd[1]);
		printf("parent process!\r\n");
		read(fd[0],buf,BUF_SIZE);
		printf("parent read:%s\r\n",buf);
	}
}
/*************************************************************************
    > File Name: mkfifo_a.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 01:25:46 PM CST
 ************************************************************************/

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

#define BUF_SIZE 20

int main()
{
	//1 mkfifo
	int ret=-1;
	ret=mkfifo("fifo",0666);
	if(ret<0&&EEXIST!=errno)
	{
		return -1;
	}
	printf("create fifo ok!----\r\n");
	//2 open
	int fd=open("fifo",O_RDONLY,0666);
	if(-1==fd)
	{
		return -1;
	}
	printf("2:open fifo ok!-----------\r\n");
	//3 read
	char buf[BUF_SIZE]={0};
	read(fd,buf,BUF_SIZE);
	printf("read ok!buf=%s\r\n",buf);
	//4 close
	close(fd);
	return 0;
}
/*************************************************************************
    > File Name: mkfifo_a.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 01:25:46 PM CST
 ************************************************************************/

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

#define BUF_SIZE 20

int main()
{
	//1 mkfifo
	int ret=-1;
	ret=mkfifo("fifo",0666);
	if(ret<0&&EEXIST!=errno)
	{
		return -1;
	}
	printf("create fifo ok!----\r\n");
	//2 open
	int fd=open("fifo",O_WRONLY,0666);
	if(-1==fd)
	{
		return -1;
	}
	printf("2:open fifo ok!-----------\r\n");
	//3 write
	char buf[BUF_SIZE]={0};
	strcpy(buf,"hello 22101");
	write(fd,buf,BUF_SIZE);
	//4 close
	close(fd);
	return 0;
}
/*************************************************************************
    > File Name: mkfifo_a.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 01:25:46 PM CST
 ************************************************************************/

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

#define BUF_SIZE 20

int main()
{
	//1 mkfifoa
	int ret=-1;
	ret=mkfifo("fifoa",0666);
	if(ret<0&&EEXIST!=errno)
	{
		return -1;
	}
	printf("1:create fifoa ok!----\r\n");
	//2 mkfifob
	ret=mkfifo("fifob",0666);
	if(ret<0&&EEXIST!=errno)
	{
		return -1;
	}
	printf("2:create fifob ok!\r\n");
	//3 open fifoa
	int fdr=open("fifoa",O_RDONLY,0666);
	if(-1==fdr)
	{
		return -1;
	}
	printf("3:open fifoa ok!-----------\r\n");
	//4 open fifob
	int fdw=open("fifob",O_WRONLY,0666);
	if(-1==fdw)
	{
		return -1;
	}
	printf("4:open fifob ok!---------\r\n");
	char buf[BUF_SIZE]={0};
	while(1)
	{
		//read
		memset(buf,0,BUF_SIZE);
		read(fdr,buf,BUF_SIZE);
		printf("a->b:%s\r\n",buf);

		//write
		memset(buf,0,BUF_SIZE);
		fgets(buf,BUF_SIZE,stdin);
		write(fdw,buf,BUF_SIZE);
	}
	//4 close
	close(fdw);
	close(fdr);
	return 0;
}
/*************************************************************************
    > File Name: mkfifo_a.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 01:25:46 PM CST
 ************************************************************************/

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

#define BUF_SIZE 20

int main()
{
	//1 mkfifoa
	int ret=-1;
	ret=mkfifo("fifoa",0666);
	if(ret<0&&EEXIST!=errno)
	{
		return -1;
	}
	printf("1:create fifoa ok!----\r\n");
	//2 mkfifob
	ret=mkfifo("fifob",0666);
	if(ret<0&&EEXIST!=errno)
	{
		return -1;
	}
	printf("2:create fifob ok!\r\n");
	//3 open fifoa
	int fdw=open("fifoa",O_WRONLY,0666);
	if(-1==fdw)
	{
		return -1;
	}
	printf("3:open fifoa ok!-----------\r\n");
	//4 open fifob
	int fdr=open("fifob",O_RDONLY,0666);
	if(-1==fdr)
	{
		return -1;
	}
	printf("4:open fifob ok!---------\r\n");
	char buf[BUF_SIZE]={0};
	while(1)
	{
		//write
		memset(buf,0,BUF_SIZE);
		fgets(buf,BUF_SIZE,stdin);
		write(fdw,buf,BUF_SIZE);

		//read
		memset(buf,0,BUF_SIZE);
		read(fdr,buf,BUF_SIZE);
		printf("b->a:%s\r\n",buf);
	}
	//4 close
	close(fdw);
	close(fdr);
	return 0;
}
/*************************************************************************
    > File Name: alarm.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 03:44:09 PM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>
#include <signal.h>

void Func(int signo)
{
	printf("into Func:signo=%d\r\n",signo);
	alarm(2);
}

int main()
{
	signal(SIGALRM,Func);
	alarm(2);
	printf("hello\r\n");
	while(1)
	{
		pause();
	}
	printf("world\r\n");
	return 0;
}

/*************************************************************************
    > File Name: alarm.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Fri 02 Dec 2022 03:44:09 PM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>

int main()
{
	alarm(2);
	printf("hello\r\n");
	pause();
	printf("world\r\n");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值