µ«Êǽá¹ûÖ»´òÓ¡Ò»´Î¡°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);
}
¹â½Å³à²² ÓÚ 2013-03-06 16:52:07·¢±í:
òËÆÁ½¸ö½ø³ÌûÓÐÕæÕýµÄʵÏÖ¶ÔsemµÄ¹²Ïí¡£¾ßÌå½â¾ö·½·¨£¬°³ÔÝʱÎÞ½â
linux_rookie123 ÓÚ 2012-12-25 21:39:30·¢±í:
int sem_init(sem_t *sem, int pshared, unsigned int value)
µÚ2¸ö²ÎÊýpsharedΪ1ʱ²»ÊǾÍÊǽø³Ì¼ä¹²ÏíµÄÐźÅÁ¿ÁË£¬ÄÇsem¾ÍÊǹ²ÏíµÄÐźÅÁ¿£¬×Ó½ø³ÌºÍ¸¸½ø³Ì¶¼¶Ôͬһ¸öÐźŲÙ×÷¡£ÄѵÀÕâÓÐÎÊÌ⣿