×¢Ïú£¬È»ºó°ÑSIFPIPEÐźŲ¶×½ÏÂÀ´²¢Í¨¹ý new_op£¨£©º¯Êý°ÑÕâ¸öÐźŵıêºÅ´òÓ¡³öÀ´£¬ÇëÎÊÕâµÀÌâÒªÔõ
ô×ö£¿£¿ÎұȽϲ»¶®µÄÊÇÒªÈçºÎ²¶×½Õâ¸öÐźţ¿£¿£¿
£¨Ïò¹ÜµÀÖÐдÈëÊý¾Ýʱ£¬linux½«²»±£Ö¤Ð´ÈëµÄÔ×ÓÐÔ£¬¹ÜµÀ»º³åÇøÒ»ÓпÕÏÐÇøÓò£¬Ð´½ø³Ì¾Í»áÊÔͼÏò¹Ü
µÀдÈëÊý¾Ý¡£Èç¹û¶Á½ø³Ì²»¶Á×߹ܵÀ»º³åÇøÖеÄÊý¾Ý£¬ÄÇôд²Ù×÷½«Ò»Ö±×èÈû¡£
×¢£ºÖ»ÓÐÔڹܵÀµÄ¶Á¶Ë´æÔÚʱ£¬Ïò¹ÜµÀÖÐдÈëÊý¾Ý²ÅÓÐÒâÒå¡£·ñÔò£¬Ïò¹ÜµÀÖÐдÈëÊý¾ÝµÄ½ø³Ì½«ÊÕµ½ÄÚºË
´«À´µÄSIFPIPEÐźţ¬Ó¦ÓóÌÐò¿ÉÒÔ´¦Àí¸ÃÐźţ¬Ò²¿ÉÒÔºöÂÔ£¨Ä¬È϶¯×÷ÔòÊÇÓ¦ÓóÌÐòÖÕÖ¹£©¡££©
1£º
#include
#include
#include
#include
#include
void new_op(int signum, siginfo_t *info, void *myact)
{
printf("receive signal %d\n", signum);
sleep(5);
}
int main(int argc,char**argv)
{
struct sigaction act;
int sig;
sig=atoi(argv[1]); //×Ö·ûÐÍת»¯³ÉÕûÐΡ£
sigemptyset(&act.sa_mask);
act.sa_flags=SA_SIGINFO; // ???
act.sa_sigaction=new_op; //ÔõôͻȻ¼äÅܳö¸önew_op ?? //ÊǺ¯ÊýÖ¸Õë
if(sigaction(sig,&act,NULL) < 0)
{
printf("install sigal error\n");
}
while(1)
{
sleep(2);
printf("wait for the signal\n");
}
}
1£º
#include
#include
#include
#include
#include
#include
int main(void)
{
int pipe_fd[2];
pid_t pid;
char r_buf[100];
char w_buf[4];
char* p_wbuf;
int r_num;
int cmd;
//void *memset(void *s, int c, size_t n);
memset(r_buf,0,sizeof(r_buf)); //¸øÊý×é³õʼ»¯ÎªNULL£»NULLµÄASCIIΪ0£»
memset(w_buf,0,sizeof(r_buf));
p_wbuf=w_buf;
if(pipe(pipe_fd)<0)
{
printf("pipe create error\n");
return -1;
}
if((pid=fork())==0)
{
printf("child\n");
printf("\n");
close(pipe_fd[1]);//Á½¸ö¹ÜµÀÃèÊö·ûºÃÏñ¹Ø²»¹Ø±Õ¶¼Ò»Ñù£»
sleep(2);//È·±£¸¸½ø³Ì¹Ø±Õд¶Ë
r_num=read(pipe_fd[0],r_buf,100); //¶Á¶Ë»áµÈ´ýд¶Ë£»
printf("read num is %d the data read from the pipe is %d\n", r_num, atoi(r_buf));
//printf("read num is %d the data read from the pipe is %s\n", r_num, r_buf);
close(pipe_fd[0]);
printf("child exit\n");
}
else if(pid>0)
{
//sleep(10);
printf("parent\n");
close(pipe_fd[0]);//read
strcpy(w_buf,"111");
//sleep(5);
if(write(pipe_fd[1],w_buf,4)!=-1)
printf("parent write over\n");
sleep(8);
close(pipe_fd[1]);//write
printf("parent close fd[1] over\n");
printf("parent exit\n");
}
exit(0);
}
miftoe ÓÚ 2011-02-23 08:42:15·¢±í:
°ïÂ¥Ö÷¶¥¸ö