|
|
写了2个小程序,相互间靠信号量通信,原意是a输出一行行+, b在收到a发送的信号后输出随机个数的*,但目前发现一个怪现象,有时候b能输出,有时候又不见b的输出,奇怪之余,发到论坛上请个位兄弟看看,帮我解开这个迷题,谢谢!
另外我想问一下,exec系列函数到底该用那一个?我怀疑问题在于$PATH变量,哪个exec函数可以带路径的呢?比如 execlp("./b", "b", NULL),可以指定b所在的路径吗?
[php]
a.c
#include <stdio.h>
#include <signal.h>
int main()
{
int pid, i, j;
pid = fork();
if (pid == 0)
execlp("b", "b", NULL);
else
{
for (i = 0; i < 10; i++) {
for (j = 0; j < i; j++)
putchar('+');
putchar('\n');
kill(pid, SIGUSR1);
sleep( 2 );
}
kill( pid, 9 );
}
}
[/php]
[php]
b.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
int main()
{
void proc(int);
sigset(SIGUSR1, proc);
while(1) {
putchar('%');
sleep( 2 );
}
}
void proc(int signo)
{
int i, r;
srand((unsigned)time(NULL));
r = rand() % 80;
putchar('\n');
for (i = 0; i < r; i++)
putchar('*');
putchar('\n');
sigset( SIGUSR1, proc );
}
[/php] |
|