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

/*************************************************************************
    > 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;
}

/*************************************************************************
    > File Name: testb_kill.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Mon 05 Dec 2022 10:41:21 AM CST
 ************************************************************************/

#include<stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
	if(2!=argc||NULL==argv[1])
	{
		return -1;
	}
	int pid=atoi(argv[1]);
	if(pid<=0)
	{
		return -1;
	}
	kill(pid,SIGUSR1);
	kill(pid,SIGUSR2);
	return 0;
}
/*************************************************************************
    > File Name: testa_sig.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Mon 05 Dec 2022 11:06:51 AM CST
 ************************************************************************/

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

void SigFunc(int signo)
{
	printf("signo=%d\r\n",signo);
	if(SIGUSR1==signo)
	{
		printf("sig user1--------\r\n");
		printf("hello\r\n");
	}
	else
	{
		printf("sig user2--------\r\n");
		printf("world\r\n");
	}
}

int main()
{
	printf("pid=%d\r\n",getpid());
	signal(SIGUSR1,SigFunc);
	signal(SIGUSR2,SigFunc);
	while(1)
	{
		pause();
	}
}
#include <stdio.h>
#include <sys/types.h> 
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

#define SHM_SIZE  256

int main()
{
	key_t key=-1;
	//1 ftok
	key=ftok(".",10);
	if(key<0)
	{
		printf("ftok error\r\n");
		return -1;
	}
	printf("1:ftok ok!key=%d\r\n",key);
	//2 shmget
	int shmid=0;
	shmid=shmget(key,SHM_SIZE,0666);
	if(shmid<0)
	{
		printf("shmget error\r\n");
		return -1;
	}
	printf("2:shmget ok,shmid=%d\r\n",shmid);
	//3 shmat
	void *addr=NULL;
	addr=shmat(shmid,NULL,0);
	if((void *)-1==addr)
	{
		printf("shmat error!-----------\r\n");
		return -1;
	}
	printf("3:shmat ok!\r\n");
	//4 printf
	char *p=(char *)addr;
	printf("4:read data:%s\r\n",p);

	shmdt(addr);
	printf("unshmat ok!----------\r\n");
	return 0;
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdio.h>

#define SHM_SIZE  256

int main()
{
	key_t key = 0;
	int shmid = 0;
	char *p = NULL;
	char str[256] = {0};
	//1 ftok
	key = ftok(".", 10);
	if(-1==key)
	{
		return -1;
	}
	printf("1:ftok ok,key=%d\r\n",key);
	//2 shmget
	shmid = shmget(key, SHM_SIZE, IPC_CREAT|0666);
	if(-1==shmid)
	{
		return -1;
	}
	printf("2:shmget ok,shmid=%d\r\n",shmid);
	//3 shmat
	p = (char *)shmat(shmid, NULL, 0);
	//4 memcpy
	memset(p, 'a', SHM_SIZE);
	memcpy((void *)str, (void *)p, 256);
	printf("%s\r\n", str);

	shmdt(p);
//	shmctl(shmid,IPC_RMID,NULL);
	return 0;
}
/*************************************************************************
    > File Name: msg_a.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Mon 05 Dec 2022 04:24:07 PM CST
 ************************************************************************/

#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define BUF_SIZE  50

struct msgbuf
{
	long m_type;
	char m_buf[BUF_SIZE];
};

int main()
{
	//1 ftok
	key_t key=ftok(".",18);
	if(-1==key)
	{
		printf("ftok error!-----------\r\n");
		return -1;
	}
	printf("1:ftok ok!------------\r\n");
	//2 msgget
	int msgID=msgget(key,0666);
	if(-1==msgID)
	{
		printf("msgget error!--------------\r\n");
		return -1;
	}
	printf("2:msgget ok!-----------\r\n");
	//3 struct msgbuf
	struct msgbuf stBuf;
	memset(&stBuf,0,sizeof(struct msgbuf));
	//4 msgrcv
	int ret=msgrcv(msgID,(void *)&stBuf,BUF_SIZE,0,0);
	if(ret>0)
	{
		printf("recvice message ok!ret=%d buf:%s\r\n",ret,stBuf.m_buf);
	}
	else
	{
		printf("message recv error!\r\n");
	}
	return 0;
}

/*************************************************************************
    > File Name: msg_a.c
    > Author: shxh
    > Mail: shxh@hqyj.com 
    > Created Time: Mon 05 Dec 2022 04:24:07 PM CST
 ************************************************************************/

#include<stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

#define BUF_SIZE  50

struct msgbuf
{
	long m_type;
	char m_buf[BUF_SIZE];
};

int main()
{
	//1 ftok
	key_t key=ftok(".",18);
	if(-1==key)
	{
		printf("ftok error!-----------\r\n");
		return -1;
	}
	printf("1:ftok ok!------------\r\n");
	//2 msgget
	int msgID=msgget(key,IPC_CREAT|0666);
	if(-1==msgID)
	{
		printf("msgget error!--------------\r\n");
		return -1;
	}
	printf("2:msgget ok!-----------\r\n");
	//3 struct msgbuf
	struct msgbuf stBuf;
	memset(&stBuf,0,sizeof(struct msgbuf));
	stBuf.m_type=2;
	strcpy(stBuf.m_buf,"hello world");
	//4 msgsnd
	if(0==msgsnd(msgID,(void *)&stBuf,BUF_SIZE,0))
	{
		printf("send message ok!------\r\n");
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值