管道 信号 消息队列


#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main()
{
	int fields[2];pipe(fields);
	
	int pid;


	if( (pid=fork())<0 )
	{
		perror("fork error\n");
		exit(EXIT_FAILURE);
	}
	else if(pid==0)//son
	{
		char s[]="Hello!\n";
		write(fields[1],s,sizeof (s));
	}
	else//father
	{
		sleep(2);
		char s[80];
		read(fields[0],s,sizeof (s));
		printf("%s",s);
	}

	return 0;
}

[ysk@localhost kaoshi]$ ./pipe.exe 2>error
Hello!


#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>

int main()
{

	int pid;
	if((pid=fork())<0)
	{
		printf("error\n");
	}
	else if(pid==0)
	{
		sleep(5);
		//exit(0);
	}
	else
	{
		int pr;
		while( (pr=waitpid(pid,NULL,WNOHANG) )==0)
		{
			printf("child process is not over!\n");
			sleep(1);
		}
		if(pid==pr)  printf("child process is over!\n");
		//exit(0);
	}
}

[ysk@localhost kaoshi]$ ./waitpid.exe
child process is not over!
child process is not over!
child process is not over!
child process is not over!
child process is not over!
child process is over!
[ysk@localhost kaoshi]$

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


int main()
{

	int pid;
	if( (pid=fork())<0 )
	{
		perror("fork error!\n");
	}
	else if(pid==0)
	{
		sleep(20);
	}
	else
	{
		if(waitpid(pid,NULL,WNOHANG)==0 )
		{
			if(kill(pid,SIGKILL))
			{
				printf("not killed!\n");
				waitpid(pid,NULL ,0);
			}
			else
			{
				printf("%d is killed!\n",pid);
			}	
		}
		else
		{
			printf("child process is over!\n");
		}
	}

	return 0;
}


[ysk@localhost kaoshi]$ ./kill.exe
2618 is killed!



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



void func()
{
	printf("received msg!\n");
}
int main()
{

	int pid;
	signal(20,func );
	if( (pid=fork())<0 )
	{
		perror("fork error!\n");
	}
	else if(pid==0)
	{		
		sleep(2);
		printf("child process starts sleep!\n");
		sleep(3);
		printf("child process finished!\n");
	}
	else
	{
		printf("send!\n");
		kill(pid,20);
		wait(NULL);
		printf("parent process finished!\n");
	}

	return 0;
}

[ysk@localhost kaoshi]$ ./kill2.exe
send!
received msg!
child process starts sleep!
child process finished!
parent process finished!
[ysk@localhost kaoshi]$ 

#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define BUFSIZ 512
struct msg
{
	long type;
	char text[BUFSIZ];
}msg_snd,msg_rcv;

int main()
{
	int qid;
	
	qid=msgget(IPC_PRIVATE,IPC_CREAT|0666);
	if(qid<0)
	{
		perror("msgget :create\n");
		exit(EXIT_FAILURE);
	}

	
	int pid=fork();
	if(pid<0)
	{
		perror("fork \n");
	}
	else if(pid>0)
	{
		char s[]="Hello world!\n";
		strcpy(msg_snd.text,s);

		msg_snd.type=20;
		int len=strlen(s);

		if(msgsnd(qid,&msg_snd,len,IPC_NOWAIT)<0  )
		{
			perror("msgsnd\n");
			printf("snd error\n");
			exit(EXIT_FAILURE);
		}	
		printf("snd success\n");
	}
	else 
	{
		sleep(5);
		int len2=msgrcv(qid,&msg_rcv,BUFSIZ,20,IPC_NOWAIT|MSG_NOERROR);
		if(len2<0)
		{
			perror("msgrcv!\n");
			exit(EXIT_FAILURE);
		}
		printf("msg=%s\n", msg_rcv.text);
		
	}



	return 0;
}


[ysk@localhost ssss]$ ./queue.exe
snd success
[ysk@localhost ssss]$ msg=Hello world!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值