ºìÁªLinuxÃÅ»§
Linux°ïÖú

Çë½ÌLINUX ÐźŴ¦Àíº¯ÊýÎÊÌâ

·¢²¼Ê±¼ä:2012-12-24 22:05:53À´Ô´:ºìÁª×÷Õß:linux_rookie123
ѧϰLINUXÐźŴ¦Àíº¯Êý·½ÃæµÄ֪ʶ£¬Ð´Á˸öС³ÌÐò£¬Ä¿µÄÊǸ¸½ø³Ì´´½¨Ò»¸ö×Ó½ø³Ì¡£×Ó½ø³ÌÁ¬Ðø·¢ËÍ5¸öSIGSUR2ÐźŸø¸¸½ø³Ì£¬¸¸½ø³Ì×¢²áÐźŴ¦Àíº¯Êýhandler£¬handlerÿ´Î¼ÆÊý¡£ÎªÁ˱ÜÃ⸸½ø³Ì×èÈûÐźţ¬ÎÒʹÓÃÁËͬ²½ÐźÅÁ¿£¬³õʼ»¯Îª¿ÉÓã¬×Ó½ø³Ì¿ÉÒÔµÚ1´Î»ñȡͬ²½ÐźÅÁ¿£¬Ö®ºóÿ´Î»ñÈ¡ÐźÅÁ¿±ØÐëÔÚhandler´¦ÀíÖ®ºóÊÍ·Å¡£
µ«Êǽá¹ûÖ»´òÓ¡Ò»´Î¡°sent SIGUSR2 to parent 0 time¡± ¾Í¹ÒסÁË£¬ºÃÏñÊÇ×Ó½ø³ÌµÚ2´Î²»ÄÜ»ñÈ¡ÐźÅÁ¿Ò»Ö±¹Ò×Å£¬¸¸½ø³Ìµ÷ÓÃwaitº¯ÊýÒ²Ò»Ö±¹Ò×Å¡£ÎÒ²»ÖªµÀÊÇÐźÅÁ¿Ê¹Óò»¶Ô£¬»¹ÊÇÐźŴ¦Àíº¯ÊýhandlerÓÐÎÊÌ⣬ÇëÄÇλ´óÏÀ°ïСµÜ¿´¿´£¬Ð»Ð»£¡

#include
#include
#include
#include

int counter = 0;
sem_t sem;

void handler(int sig)
{
counter++;
if(0 != sem_post(&sem))
{
printf("failed to release sem\n");
}
else
{
printf("suceeded to release sem\n");
}

return;
}

int main()
{
int i;

signal(SIGUSR2, handler);

sem_init(&sem, 1, 1);

if(fork() == 0)
{
for(i=0;i<5;i++)
{
sem_wait(&sem);
kill(getppid(), SIGUSR2);
printf("sent SIGUSR2 to parent %d time\n", i);
}

exit(0);
}

wait(NULL);

printf("counter = %d \n", counter);
exit(0);
}
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. ¹â½Å³à²² ÓÚ 2013-03-06 16:52:07·¢±í:

    òËÆÁ½¸ö½ø³ÌûÓÐÕæÕýµÄʵÏÖ¶ÔsemµÄ¹²Ïí¡£¾ßÌå½â¾ö·½·¨£¬°³ÔÝʱÎÞ½â

  2. linux_rookie123 ÓÚ 2012-12-25 21:39:30·¢±í:

    int sem_init(sem_t *sem, int pshared, unsigned int value)

    µÚ2¸ö²ÎÊýpsharedΪ1ʱ²»ÊǾÍÊǽø³Ì¼ä¹²ÏíµÄÐźÅÁ¿ÁË£¬ÄÇsem¾ÍÊǹ²ÏíµÄÐźÅÁ¿£¬×Ó½ø³ÌºÍ¸¸½ø³Ì¶¼¶Ôͬһ¸öÐźŲÙ×÷¡£ÄѵÀÕâÓÐÎÊÌ⣿