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

/*************************************************************************
    > File Name: pthread_create.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 02:49:02 PM CST
 ************************************************************************/

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

void *ThreadFunc1(void *arg)
{
	printf("test by func1-------------\r\n");
}

void *ThreadFunc2(void *arg)
{
	printf("test by func2-------------\r\n");
}

int main()
{
	printf("hello world\r\n");
	pthread_t tID1=-1;
	pthread_t tID2=-1;
	if(0!=pthread_create(&tID1,NULL,ThreadFunc1,NULL))
	{
		printf("create  pthread1 error!--------\r\n");
		return -1;
	}
	if(0!=pthread_create(&tID2,NULL,ThreadFunc2,NULL))
	{
		printf("create pthread2 error!\r\n");
		return -1;
	}
	pthread_join(tID1,NULL);
	pthread_join(tID2,NULL);

	printf("hello 22101------------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: pthread_create.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 02:49:02 PM CST
 ************************************************************************/

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

sem_t sema;
sem_t semb;

void *ThreadFunc1(void *arg)
{
	int i=10;
	while(i--)
	{
		sem_wait(&sema);
		printf("hello----\r\n");
		sleep(1);
		sem_post(&semb);
	}
}

void *ThreadFunc2(void *arg)
{
	int i=10;
	while(i--)
	{
		sem_wait(&semb);
		printf("world----------\r\n");
		sleep(1);
		sem_post(&sema);
	}
}

int main()
{
	printf("hello world\r\n");
	pthread_t tID1=-1;
	pthread_t tID2=-1;
	if(-1==sem_init(&sema,0,1)||-1==sem_init(&semb,0,0))
	{
		return -1;
	}
	if(0!=pthread_create(&tID1,NULL,ThreadFunc1,NULL))
	{
		printf("create  pthread1 error!--------\r\n");
		return -1;
	}
	if(0!=pthread_create(&tID2,NULL,ThreadFunc2,NULL))
	{
		printf("create pthread2 error!\r\n");
		return -1;
	}
	pthread_join(tID1,NULL);
	pthread_join(tID2,NULL);

	printf("hello 22101------------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: pthread_create.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 02:49:02 PM CST
 ************************************************************************/

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

void *ThreadFunc(void *arg)
{
	printf("test by all@1201-------------\r\n");
	pthread_exit("thread exit test---");
}

int main()
{
	printf("hello world\r\n");
	pthread_t tID=-1;
	if(0!=pthread_create(&tID,NULL,ThreadFunc,NULL))
	{
		printf("create  pthread error!--------\r\n");
		return -1;
	}
	char *pStr=NULL;
	pthread_join(tID,(void *)&pStr);
	printf("%s\r\n",pStr);

	printf("hello 22101------------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: pthread_create.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 02:49:02 PM CST
 ************************************************************************/

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

void *ThreadFunc(void *arg)
{
	printf("test by all@1201-------------\r\n");
}

int main()
{
	printf("hello world\r\n");
	pthread_t tID=-1;
	if(0!=pthread_create(&tID,NULL,ThreadFunc,NULL))
	{
		printf("create  pthread error!--------\r\n");
		return -1;
	}
	pthread_join(tID,NULL);
	printf("hello 22101------------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: pthread_create.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 02:49:02 PM CST
 ************************************************************************/

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

#define BUF_SIZE 20

char buf[BUF_SIZE]={0};

void *ThreadFunc1(void *arg)
{
	if(NULL==arg)
	{
		return (void *)NULL;
	}
	strcpy(buf,(char *)arg);
	printf("buf:%s\r\n",buf);
}

void *ThreadFunc2(void *arg)
{
	sleep(1);
	printf("test by func2-------------buf=%s\r\n",buf);
}

int main(int argc,char *argv[])
{
	printf("hello world\r\n");
	pthread_t tID1=-1;
	pthread_t tID2=-1;
	if(0!=pthread_create(&tID1,NULL,ThreadFunc1,(void *)argv[1]))
	{
		printf("create  pthread1 error!--------\r\n");
		return -1;
	}
	if(0!=pthread_create(&tID2,NULL,ThreadFunc2,NULL))
	{
		printf("create pthread2 error!\r\n");
		return -1;
	}
	pthread_join(tID1,NULL);
	pthread_join(tID2,NULL);

	printf("hello 22101------------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: execlp.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Wed 30 Nov 2022 02:55:48 PM CST
 ************************************************************************/

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

int main()
{
	printf("hello 22101--------\r\n");
	if(-1==execlp("ps","ps","axj",NULL))
	{
		printf("execlp error!-----------\r\n");
		return -1;
	}
	printf("hello world!---------\r\n");
	return 0;
}

/*************************************************************************
    > File Name: pthread_create.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 02:49:02 PM CST
 ************************************************************************/

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

#define BUF_SIZE 20

char buf[BUF_SIZE]={0};

void *ThreadFunc1(void *arg)
{
	printf("test by func1-------------\r\n");
	strcpy(buf,"aaaaaa");
	printf("buf:%s\r\n",buf);
}

void *ThreadFunc2(void *arg)
{
	sleep(1);
	printf("test by func2-------------buf=%s\r\n",buf);
}

int main()
{
	printf("hello world\r\n");
	pthread_t tID1=-1;
	pthread_t tID2=-1;
	if(0!=pthread_create(&tID1,NULL,ThreadFunc1,NULL))
	{
		printf("create  pthread1 error!--------\r\n");
		return -1;
	}
	if(0!=pthread_create(&tID2,NULL,ThreadFunc2,NULL))
	{
		printf("create pthread2 error!\r\n");
		return -1;
	}
	pthread_join(tID1,NULL);
	pthread_join(tID2,NULL);

	printf("hello 22101------------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: pthread_create.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 02:49:02 PM CST
 ************************************************************************/

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

void *ThreadFunc(void *arg)
{
	if(NULL==arg)
	{
		return (void *)NULL;
	}
	int b=*(int *)arg;
	printf("test by all@1201-------------b=%d\r\n",b);
}

int main()
{
	printf("hello world\r\n");
	pthread_t tID=-1;
	int a=10;
	if(0!=pthread_create(&tID,NULL,ThreadFunc,(void *)&a))
	{
		printf("create  pthread error!--------\r\n");
		return -1;
	}
	pthread_join(tID,NULL);
	printf("hello 22101------------\r\n");
	return 0;
}
/*************************************************************************
    > File Name: deamon.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Thu 01 Dec 2022 11:16:27 AM CST
 ************************************************************************/

#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
	int i=0;
	//1 fork
	pid_t pid=-1;
	pid=fork();
	if(-1==pid)
	{
		return -1;
	}
	if(pid>0)
	{
		exit(0);
	}
	printf("1:fork ok!\r\n");
	//2 set new section
	setsid();
	printf("2:create new section ok!pid=%d\r\n",getpid());
	//3 chdir
	chdir("/");
	//4 umask(0)
	umask(0);
	//5 close all file
	int num=getdtablesize();
	for(;i<num;i++)
	{
		close(i);
	}

	sleep(20);
	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值